springboot注解
@Configuration
@Configuration :Spring的配置标签,标记改类是Spring的配置类
Spring的配置类 相当于是:applicationContext.xml
@Configuration
public class ApplicationConfig {
}
ComponentScan&ComponentScans自动扫描
ComponentScan是ioc组件自动扫描,相当于是 context:component-scan base-package=*
默认扫描当前包,及其子包 ;
ComponentScan.lazyInit :懒初始化
ComponentScan.excludeFilters :排除
@Component
public class MyBean {
}
@Configuration
//@ComponentScans
@ComponentScan("包名")
public class ApplicationConfig {
}
@Bean
Spring的bean的定义标签 ,标记方法返回的对象交给Spring容器管理
bean的id: 方法名 myBean
bean的name:可以通过 @Bean标签的name属性指定
生命周期方法:initMethod , destroyMethod
单利多利: @Scope(“prototype”)
@Configuration
public class ApplicationConfig {
@Bean
public MyBean myBean(){
return new MyBean();
}
}
@Bean(name="" , initMethod ="" ,destroyMethod="" )
//@Scope("prototype")
//@Lazy
public MyBean myBean(){
return new MyBean();
}
依赖注入
1.手动定义bean的方式
直接通过调方法的方式注入bean,或者通过参数注入bean
2.自动扫描定义bean的方式
使用 @Autowired 注入
3.通过工厂定义bean
3.1.定义FactoryBean
public class MyBeanFactoryBean implements FactoryBean<MyBean> {
public MyBean getObject() throws Exception {
return new MyBean();
}
public Class<?> getObjectType() {
<