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









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 Bean Example, Spring Bean Creation
Basic Bean Creation The Spring bean can typically be POJO(Plain Old Java... java bean file having two methods getMessage() and setMessage() and a default...;} } The context.xml connects every bean to every other bean
Spring Annotation
Spring Annotation  Hi Sire please tell me that how to write Spring Annotation Based Controller ?   Spring Annotation Based Controller
Spring @Required Annotation
Spring @required Annotation In this tutorial you will see about spring @required annotation with an example. The @Required when written on top of setStudent... an RequiredAnnotationBeanPostProcessor to aware of the @Required annotation in bean
@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  ... define the bean configuration in xml file and then instruct the Spring to use... to infrastructure providers. b) Annotation: The @configuration spring
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... messages asynchronously,  a Message-driven bean is used. Message driven
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 Inner bean example, Inner Bean in spring framework
Inner Bean in spring framework In the Spring framework an inner bean definition does not required to defined id or name of bean. The reason behind this is that the container ignores these values. Inner bean is used when a bean is used
An Entity Bean Example
and a database. For example, consider a bank entity bean that is used... declares the identifier property of this entity bean. @GeneratedValue annotation...; In the Book catalog example, we define a Book entity bean class. The 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
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
Spring Bean Life Cycle methods, Spring Bean Life Cycle
Spring Bean Life Cycle methods In this example you will know about spring bean life cycle and how a bean is initialized in the program. The bean factory container search for bean definition and also instantiates the bean
@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
bean life cycle methods in spring?
bean life cycle methods in spring?  bean life cycle methods in spring
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... a hibernate application using annotation. In this example I will create a POJO class
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
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 Injection Example
Spring Injection Example       XML Bean-Injection, The given example below gives the brief...;} } Output of the Spring Injection Example programADS_TO_REPLACE_4
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
first entity bean example in eclipse europa - EJB
first entity bean example in eclipse europa  pls provide steps to create simple ejb3.0 application in eclipse .And also how to create entity bean ,session bean in ejb3.0  Hi Friend, Please visit the following links
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... is that it auto-detect BeanPostProcessor beans in their bean definations
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
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
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 ExampleADS_TO_REPLACE_1 In this example we want
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
Java Custom Annotation Example
Java Custom Annotation Example In this tutorial we will learn about the annotation in Java and how it can be created. Annotations in Java contains... will demonstrate you about how a custom annotation can be created. In this example we
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 5 Annotation Example
Learn to create Hibernate 5 Annotation Example This video instruction... a working Hibernate 5 annotation based example which can insert data into MySQL... of Creating Hibernate 4 Annotation example Following video tutorial teaches you
Stateless Session Bean Example
Stateless Session Bean Example   ... stateless session bean. The purpose of example is to performs the mathematical...; The example application consists of an enterprise bean, which performs
@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
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 Batch Example
Spring Batch Example       JDBC Template Batch update example, In the tutorial we have discussed about  batchUpdate()  method of class JdbcTemplate in Spring framework
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
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
Working Example for Spring AOP - Spring
Working Example for Spring AOP  Hi All, I need a complete working Java example /Code/Logic for Spring -Aspect Oriented Programming. Code provided will be highly appreciated. -- Deepak Lal
spring first example - Spring
spring first example  I am trying my first example of spring from the link http://www.maestric.com/doc/java/spring/setup But I am not getting the simple "Hi" message ,I have created simple one jsp page and /WEB_INF/web.xml
Spring Injection Example
Spring Injection Example       XML Bean-Injection, The given example below gives the brief.../lang/spring-lang-2.0.xsd"> <bean id="dataSource" class="
Spring Override Bean
Spring Override Bean The support of inheritance is present in the Spring framework and common values or configuration is shared among beans. The child bean inherits the properties and configuration of the parent bean or base bean

Ads