@Configuration
@Bean
메서드가 Spring 컨테이너에서 관리할 새 객체를 인스턴스화, 구성 및 초기화한다는 것을 나타내는 데 사용
// DependencyConfig 클래스 컨텍스트를 인스턴스화할 때 @Configuration public class DependencyConfig { @Bean public MyService myService() { return new MyServiceImpl(); } }
<beans> <bean id="myService" class="com.acme.services.MyServiceImpl"/> </beans>
AnnotationConfigApplicationContext
public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(DependencyConfig.class); MyService myService = ctx.getBean(MyService.class); myService.doStuff(); }
// @Autowired - MyServiceImpl, Dependency1, Dependency2에서 스프링 의존성 주입 애너테이션을 사용한 예제 public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(MyServiceImpl.class, Dependency1.class, Dependency2.class); MyService myService = ctx.getBean(MyService.class); myService.doStuff(); }
@Bean은 메서드-레벨 애너테이션이며, 에서 제공하는 일부 속성을 지원합니다.
@Bean 애너테이션은 @Configuration-annoted 또는 @Component-annoted 클래스에서 사용할 수 있습니다.
@Configuration public class DependencyConfig { @Bean public TransferServiceImpl transferService() { return new TransferServiceImpl(); } }
<beans> <bean id="transferService" class="com.acme.TransferServiceImpl"/> </beans>
public interface BaseConfig { @Bean default TransferServiceImpl transferService() { return new TransferServiceImpl(); } } @Configuration public class DependencyConfig implements BaseConfig { }
@Configuration public class DependencyConfig { @Bean public TransferService transferService(AccountRepository accountRepository) { return new TransferServiceImpl(accountRepository); } }
@Configuration는 해당 객체가 bean definitions의 소스임을 나타내는 애너테이션입니다.
@Configuration는 @Bean-annoted 메서드를 통해 bean을 선언
@Configuration 클래스의 @Bean 메서드에 대한 호출을 사용하여 bean 사이의 의존성을 정의할 수도 있습니다.
@Configuration public class DependencyConfig { @Bean public BeanOne beanOne() { return new BeanOne(beanTwo()); } @Bean public BeanTwo beanTwo() { return new BeanTwo(); } }
@Configuration public class DependencyConfig { @Bean public ClientService clientService1() { ClientServiceImpl clientService = new ClientServiceImpl(); clientService.setClientDao(clientDao()); return clientService; } @Bean public ClientService clientService2() { ClientServiceImpl clientService = new ClientServiceImpl(); clientService.setClientDao(clientDao()); return clientService; } @Bean public ClientDao clientDao() { return new ClientDaoImpl(); } }
@Configuration public class DependencyConfigA { @Bean public A a() { return new A(); } } @Configuration @Import(DependencyConfigA.class) public class DependencyConfigB { @Bean public B b() { return new B(); } }
public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(DependencyConfigB.class); // now both beans A and B will be available... A a = ctx.getBean(A.class); B b = ctx.getBean(B.class); }
@Configuration public class ServiceConfig { @Bean public TransferService transferService(AccountRepository accountRepository) { return new TransferServiceImpl(accountRepository); } } @Configuration public class RepositoryConfig { @Bean public AccountRepository accountRepository(DataSource dataSource) { return new JdbcAccountRepository(dataSource); } } @Configuration @Import({ServiceConfig.class, RepositoryConfig.class}) public class SystemTestConfig { @Bean public DataSource dataSource() { // return new DataSource } } public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(SystemTestConfig.class); // everything wires up across configuration classes... TransferService transferService = ctx.getBean(TransferService.class); transferService.transfer(100.00, "A123", "C456"); }
@Configuration public class ServiceConfig { @Autowired private AccountRepository accountRepository; @Bean public TransferService transferService() { return new TransferServiceImpl(accountRepository); } } @Configuration public class RepositoryConfig { private final DataSource dataSource; public RepositoryConfig(DataSource dataSource) { this.dataSource = dataSource; } @Bean public AccountRepository accountRepository() { return new JdbcAccountRepository(dataSource); } } @Configuration @Import({ServiceConfig.class, RepositoryConfig.class}) public class SystemTestConfig { @Bean public DataSource dataSource() { // return new DataSource } } public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(SystemTestConfig.class); // everything wires up across configuration classes... TransferService transferService = ctx.getBean(TransferService.class); transferService.transfer(100.00, "A123", "C456"); }