Spring bean annotation example

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
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
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
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
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
@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
How to configure Spring using Annotation
How to configure Spring using Annotation  Hi, How to configure Spring using Annotation Thanks
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
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
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
Stateless Session Bean Example Error
Stateless Session Bean Example Error  Dear sir, I'm getting following error while running StatelessSessionBean example on Jboss. Please help me...)   Please visit the following link: Stateless Session Bean Example
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
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
Spring Injection Example
Spring Injection Example       XML Bean-Injection, The given example below gives the brief..., this.email);  } } Output of the Spring Injection 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
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
Java bean example in JSP
Java bean example in JSP     ... that help in understanding Java bean example in JSP.This code illustrates... of Java bean. Understand with Example In this example we want to describe you
Rose India Stateless Session Bean Example Question
Rose India Stateless Session Bean Example Question  Hi Team, Is this example can be compiled using NetBeans and Glass Fish ? Actually, in descriptor .xml file it is shown that it can be run by using JBoss and Apache Ant. If I
consider defining a bean of type in your configuration. spring boot
consider defining a bean of type in your configuration. spring boot  Hi, I am auto wiring a class in my Spring Boot application but it throws following error at runtime: consider defining a bean of type in your configuration
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
How to write Example code for CRUD application in hibernate annotation?
How to write Example code for CRUD application in hibernate annotation? .... How to write Example code for CRUD application in hibernate annotation? Share... in hibernate annotation. This tutorial is the the example code. Thanks

Ads