Spring bean annotation example

Ads
 

Spring bean annotation example

Hi,

How to configure Bean using annotation in Spring Framework?

Thank

View Answers

April 25, 2017 at 10:17 PM

Hi,

I will explain you how to configure it. If you have a bean with following definition:

package net.roseindia;

public class HelloWorld {

    public void sayHello(){
        System.out.println("Hello");
    }

}

Now to configure it as Bean you can use the @Bean annotation as given below:

package net.roseindia;

import org.springframework.context.annotation.*;

@Configuration
public class BeanConfig {
    @Bean
    public HelloWorld helloWorld() {
        return new HelloWorld();
    }
}

@Configuration annotated class is used by the Spring IoC container as a source of bean definition.

Thanks


April 25, 2017 at 10:21 PM

Hi,

Above @Configuration for the helloWorld bean is equivalent to following xml configuation:

<beans>
   <bean id = "helloWorld" class = "net.roseindia.HelloWorld" />
</beans>

Thanks

Ads









Related Tutorials/Questions & Answers:
Spring bean annotation example
Spring bean annotation example  Hi, How to configure Bean using annotation in Spring Framework? Thank   Hi, I will explain you how...(); } } @Configuration annotated class is used by the Spring IoC container as a source of bean
spring mvc3 and hibernate with annotation example
spring mvc3 and hibernate with annotation example  please send an example which is used to store the form data into database using spring with hibernate using annotations
Advertisements
The @configuration annotation example in Spring 3.0 Framework
The @configuration annotation example in Spring 3.0 Framework  I was going through the tutorial on Spring for "The @configuration annotation example in Spring 3.0 Framework" but I am getting following error when I Run
Spring Annotation
Spring Annotation  Hi Sire please tell me that how to write Spring Annotation Based Controller ?   Spring Annotation Based Controller
@Controller Annotation Example in Spring 3
@Controller Annotation Example in Spring 3: In this section we will see how we can create annoted controller class in Spring 3. You can @Controller annotation in the Java class to make it controller. In this tutorial we are creating
The @configuration annotation example in Spring 3.0 Framework
@configuration annotation in Spring 3  ... to configure Spring IOC container a) xml file: Developer can define the bean... to infrastructure providers. b) Annotation: The @configuration spring annotation can also
@configuration annotation in Spring 3
@configuration annotation in Spring 3  doesn't make code tight coupling..? if we keep that in IOC in XML form, its deployment free for some kind of change management right?.   Please visit the following link: http
Bean life cycle in spring
Bean life cycle in spring       This example gives you an idea on how to Initialize bean in the program and also explains the lifecycle of bean in spring. Run the given bean example
Bean life cycle in spring
Bean life cycle in spring       This example gives you an idea on how to Initialize bean in the program and also explains the lifecycle of bean in spring. Run the given bean example
How to configure Spring using Annotation
How to configure Spring using Annotation  Hi, How to configure Spring using Annotation Thanks
What is Bean lifecycle in Spring framework?
What is Bean lifecycle in Spring framework?  HI, What is Bean lifecycle in Spring framework? Thanks
How to add a bean in spring application?
How to add a bean in spring application?  Hi, How to add a bean in spring application? Thanks
What are Bean scopes in Spring Framework
What are Bean scopes in Spring Framework  Hi, What are Bean scopes in Spring Framework Thanks
A Message-Driven Bean Example
A Message-Driven Bean Example   ... of a simple message-driven bean application. In this example, we are going to implement... the annotation to a class, the annotation declares a resource that the bean will look
message driven bean example
message driven bean example  can any one tell me how to develop and execute ---------- j2ee message driven bean example in weblogic server...://www.roseindia.net/c-tutorials/example-of-messageBean.shtml
Spring Web Annotation Classes
Spring Web Annotation Annotation is introduced since java 5. It is a new kind... and it is not a part of your program. Spring framework also provides a wide range of annotation, some of them which is commonly is used in Spring MVC is explained
An Entity Bean Example
and a database. For example, consider a bank entity bean that is used... property of this entity bean. @GeneratedValue annotation indicates... bean class:  In the Book catalog example, we define a Book entity bean
Session Bean Example
Session Bean Example  I want to know that how to run ejb module by jboss 4.2.1 GA (session bean example by jboss configuration )?   Please visit the following link: http://www.roseindia.net/javabeans
Spring Annotation Tutorial
Spring Annotation Tutorial In Spring Framework you can use annotations to write to java classes such as Controller easily. For example making any java class... annotation before any method or class declartion to handle particular URL
Bean life cycle in spring
Bean life cycle in spring       This example gives you an idea on how to Initialize bean in the program and also explains the lifecycle of bean in spring. Run the given bean example
Spring Bean Post Processor
Spring Bean Post Processor The interface BeanPostProcessor allows custom modification of all new bean instance like for example making for marker interfaces...: Initialized Bean : student Download this example code
Spring Injection Example
Spring Injection Example       XML Bean-Injection, The given example below gives the brief..., this.email);  } } Output of the Spring Injection Example
Spring Bean Scope Default
Spring Bean Scope There are five different types of bean scopes (i.e..... The singleton return a single bean instance per spring IoC container and the prototype return a new bean instance each time when requested. In this example
Spring Bean Scope Prototype
Spring Bean Scope There are five different types of bean scopes (i.e..... The singleton return a single bean instance per spring IoC container and the prototype return a new bean instance each time when requested. In this example
Issue in Stateless session bean example
Issue in Stateless session bean example  Hi Team, I tried your Stateless session bean example in the path http://www.roseindia.net/ejb/examples-of-StatelessBean.shtml. I got the below exception can you plese guide to rectify
@Component Annotation in Spring, Spring Autoscan
@Component Annotation in Spring In Spring normally if there is bean we need to define in the bean configuration file. If you have lot many bean file... can scan all your bean through spring auto scan feature. The @Component
Hibernate Annotation Example
Hibernate Annotation Example In this section we will read about how to create a hibernate application using annotation. Here we will see an example... using annotation. In this example I will create a POJO class that contains some
Spring Bean Configuration
Spring Bean Configuration The support of inheritance is present in the Spring... bean inherits the properties and configuration of the parent bean or base bean. Teacher.java package bean.configuration.inheritance; public class
Spring 3 MVC Login Form Example
Spring 3 MVC Login Form Example In this tutorials we are showing you and example to create LoginForm in Spring 3.0. In this tutorial we are using annotation based Controller and other required configuration files. In the example
Hibernate Spring Example
In this section, you will learn how to Integration Hibernate with Spring with an example
spring basic example
spring basic example  hi,, i cannot understand the flow of the spring mvc example. can you explain execution flow of the spring example project?? thanks

Ads