Close

Spring Framework - @Primary Examples

Spring Framework 

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;
import org.springframework.context.annotation.Primary;
@Configuration
public class AppConfig {

@Bean
public OrderService orderService() {
return new OrderService();
}

@Bean
public Dao daoA() {
return new DaoA();
}

@Bean
@Primary
public Dao daoB() {
return new DaoB();
}

public static void main(String[] args) throws InterruptedException {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(AppConfig.class);

OrderService orderService = context.getBean(OrderService.class);
orderService.placeOrder("122");

}
}
Original Post




See Also