Close

Spring Framework - @Bean Examples

Spring Framework 

This example shows how to use @Bean annotation

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

@Bean
Calculator calculator() {
return new Calculator();
}

public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(AppConfig.class);
Calculator calculator = context.getBean(Calculator.class);
int sum = calculator.sum(5, 7);
System.out.println(sum);
}
}
public class Calculator {

public int sum(int x, int y) {
return x + y;
}
}
Original Post




Using @Bean annotation's element 'autowire' with value Autowire.NO. This is actually redundant as it's the default autowiring mode.

import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

public class AutowireNoExample {

public static void main (String[] args) {
AnnotationConfigApplicationContext context = new
AnnotationConfigApplicationContext(Config.class);
ClientBean bean = context.getBean(ClientBean.class);
bean.doSomething();
}

@Configuration
public static class Config {

@Bean(autowire = Autowire.NO)
public ClientBean clientBean () {
return new ClientBean();
}

@Bean
public ServiceBean serviceBean () {
return new ServiceBean("Service bean 1");
}
}

private static class ClientBean {
@Autowired
private ServiceBean serviceBean;

public void doSomething () {
System.out.println(serviceBean.getMsg());
}
}

private static class ServiceBean {
private String msg;

public ServiceBean (String msg) {
this.msg = msg;
}

public String getMsg () {
return msg;
}
}
}

Output

Service bean 1
Original Post




Using @Bean annotation's element 'autowire' with value Autowire.BY_TYPE. @Bean can only be used on methods or another annotation definitions. They are typically used on the methods of a @Configuration class to register bean to Spring context.

import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

public class AutowireByType {

public static void main (String[] args) {
AnnotationConfigApplicationContext context = new
AnnotationConfigApplicationContext(Config.class);
ClientBean bean = context.getBean(ClientBean.class);
bean.doSomething();
}

@Configuration
public static class Config {

@Bean(autowire = Autowire.BY_TYPE)
public ClientBean clientBean () {
return new ClientBean();
}

@Bean
public ServiceBean serviceBean1 () {
return new ServiceBean("Service bean 1");
}
}

private static class ClientBean {
private ServiceBean serviceBean;

public void setServiceBean (ServiceBean serviceBean) {
this.serviceBean = serviceBean;
}

public void doSomething () {
System.out.println(serviceBean.getMsg());
}
}

private static class ServiceBean {
private String msg;

public ServiceBean (String msg) {
this.msg = msg;
}

public String getMsg () {
return msg;
}
}
}

Output

Service bean 1
Original Post




See Also