How to create bean using Annotation in Spring ?

@Configuration – Annotating a class with the @Configuration annotation indicates that the class will be used by Java Config as a source of bean definitions. An application may make use of just one @Configuration-annotated class or many. @Configuration can be considered the equivalent of XML’s element. Like, it provides an opportunity to explicitly set defaults for all enclosed bean definitions.

@Bean – It is a method-level annotation and a direct analog of the XML element. The annotation supports most of the attributes offered by such as init-method, destroy-method, auto wiring, lazy-init, dependency-check, depends-on and scope.

How to declare a bean using annotation?

<beans>
     <bean name=“service” class=”com.spring.core.ServiceImpl”/>
</beans>

Is equal to

@Configuration
public class AppConfig {
     @Bean
     public Service service() {
          return new ServiceImpl();
     }
}

Lets Implement Simple One

Project structure be like below image

6.png

add the dependency in pom.xml file –

<dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-context</artifactId>
     <version>4.3.4.RELEASE</version>
</dependency>

bean class “Greetings.java

package com.spring.core;
public class Greetings {
    private String message;
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
}

In beans.xml need to enable annotation-driven

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
    <context:annotation-config />
</beans

BeanConfig – annotation is enabled so can use @Configuration, @Bean, and @Scope

package com.spring.core;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
@Configuration
public class BeanConfig {
    @Bean
    @Scope(value="prototype")
    public Greetings createGreetings() {
        Greetings greetings = new Greetings();
        greetings.setMessage("Hello Spring");
        return greetings;
    }
}

Application Main class

package com.spring.core;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
public class Application {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(BeanConfig.class);
        Greetings greet = (Greetings) context.getBean(Greetings.class);
        System.out.println(greet+" : "+greet.getMessage());     
        Greetings greet2 = (Greetings) context.getBean(Greetings.class);
        System.out.println(greet2+" : "+greet2.getMessage());
    }
}

Output –

Dec 28, 2016 1:15:26 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5e9f23b4: startup date [Wed Dec 28 13:15:26 IST 2016]; root of context hierarchy
com.spring.core.Greetings@5cdd8682 : Hello Spring
com.spring.core.Greetings@d6da883 : Hello Spring

Let’s Implement Multiple POJO

Project structure be like below image

7

beans.xml, BeanConfig, and Greetings like the above

bean class “Greetings2.java

package com.spring.core.pojo;
public class Greetings2 {
    private String message; 
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
}

BeanConfig2

package com.spring.core.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.spring.core.pojo.Greetings2;
@Configuration
public class BeanConfig2 {
    @Bean
    public Greetings2 createGreetings2() {
        Greetings2 greetings2 = new Greetings2();
        greetings2.setMessage("Hello Spring 2");
        return greetings2;
    }
}

Application Main class – so multiple configurations is there then need to register

package com.spring.core;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import com.spring.core.config.BeanConfig;
import com.spring.core.config.BeanConfig2;
import com.spring.core.pojo.Greetings;
import com.spring.core.pojo.Greetings2;
public class Application {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(BeanConfig.class, BeanConfig2.class);     
        context.refresh();        
        Greetings greet = (Greetings) context.getBean(Greetings.class);
        System.out.println(greet+" : "+greet.getMessage());        
        Greetings2 greet2 = (Greetings2) context.getBean(Greetings2.class);
        System.out.println(greet2+" : "+greet2.getMessage());
    }
}

Output –

Dec 28, 2016 1:22:57 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5e9f23b4: startup date [Wed Dec 28 13:22:57 IST 2016]; root of context hierarchy
com.spring.core.pojo.Greetings@679b62af : Hello Spring
com.spring.core.pojo.Greetings2@5cdd8682 : Hello Spring 2

Let’s Implement when beans are dependent on others

Project structure be like below image

8

beans.xml, BeanConfig2 and Greetings, Greeting2 like the above

bean class “Greetings3.java

package com.spring.core.pojo;
public class Greetings3 {
    private Greetings greetings;
    public Greetings getGreetings() {
        return greetings;
    }
    public void setGreetings(Greetings greetings) {
        this.greetings = greetings;
    }
}

BeanConfig

package com.spring.core.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import com.spring.core.pojo.Greetings;
import com.spring.core.pojo.Greetings3;
@Configuration
public class BeanConfig {  
    @Bean
    @Scope(scopeName="prototype")
    //@Scope(value="prototype")
    public Greetings createGreetings() {
        Greetings greetings = new Greetings();
        greetings.setMessage("Hello Spring");
        return greetings;
    }    
    @Bean
    public Greetings3 createGreetings3() {
        Greetings3 greetings2 = new Greetings3();
        greetings2.setGreetings(createGreetings());
        return greetings2;
    }
}

Application Main class –

package com.spring.core;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import com.spring.core.config.BeanConfig;
import com.spring.core.config.BeanConfig2;
import com.spring.core.pojo.Greetings;
import com.spring.core.pojo.Greetings2;
import com.spring.core.pojo.Greetings3;
@Configuration
public class Application {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(BeanConfig.class, BeanConfig2.class);
        context.refresh();
        Greetings greet = (Greetings) context.getBean(Greetings.class);
        System.out.println(greet + " : " + greet.getMessage());
        Greetings2 greet2 = (Greetings2) context.getBean(Greetings2.class);
        System.out.println(greet2 + " : " + greet2.getMessage());
        Greetings3 greet3 = (Greetings3) context.getBean(Greetings3.class);
        System.out.println(greet3 + " : " + greet3.getGreetings() + " : " + greet3.getGreetings().getMessage());
    }
}

Output –

Dec 28, 2016 1:30:09 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5e9f23b4: startup date [Wed Dec 28 13:30:09 IST 2016]; root of context hierarchy
com.spring.core.pojo.Greetings@69a10787 : Hello Spring
com.spring.core.pojo.Greetings2@2d127a61 : Hello Spring 2
com.spring.core.pojo.Greetings3@2bbaf4f0 : com.spring.core.pojo.Greetings@11c20519 : Hello Spring

For more detail, Please watch below video –

 

Thanks for reading 🙂

Please Subscribe our you tube channel Almighty Java

Leave a comment