在Spring Boot中,注入Bean的方法有以下几种:
@Autowired
private BeanName beanName;
@Resource
private BeanName beanName;
@Configuration
public class AppConfig {
@Bean
public BeanName beanName() {
return new BeanName();
}
}
@Service
public class MyService {
private BeanName beanName;
@Autowired
public MyService(BeanName beanName) {
this.beanName = beanName;
}
}
@Autowired
@Qualifier("beanName1")
private BeanName beanName;