注解式编程
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
① 配置类&Bean注解
@Configuration
public class ApplicationConfig {
@Bean
public MyBean myBean(){
MyBean myBean = new MyBean();
return myBean;
}
}
② @ComponentScan扫描bean
@Configuration
@ComponentScan
public class ApplicationConfig {
}
@Component
public class MyBean {
}
③ @Scope组件范围&@Lazy懒加载-认识
@Configuration
public class ApplicationConfig {
@Bean(name = "myBean2",initMethod = "init",destroyMethod = "destory")
public MyBean myBean(){
MyBean myBean = new MyBean();
return myBean;
}
}
④ @Conditional-按照条件注册
@Configuration
public class ApplicationConfig {
@Bean
@Conditional(value = WindowBeanCreateCondition.class)
public MyBean windowsBean(){
MyBean myBean = new MyBean();
myBean.setSystem("windows");
return myBean;
}
@Bean
@Conditional(value = LinuxBeanCreateCondition.class)
public MyBean linuxBean(){
MyBean myBean = new MyBean();
myBean.setSystem("linux");
return myBean;
}
}
- LinuxBeanCreateCondition:
public class LinuxBeanCreateCondition implements Condition {
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Environment environment = context.getEnvironment();
String systemName = environment.getProperty("os.name");
if(systemName.contains("linux")){
return true;
}
return false;
}
}
- WindowsBeanCreateCondition:
public class WindowBeanCreateCondition implements Condition{
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
Environment environment = conditionContext.getEnvironment();
String systemName = environment.getProperty("os.name");
if (systemName.toUpperCase().contains("WINDOWS")){
return true;
}
return false;
}
}
⑤ @Import
@Configuration
@Import({ApplicationOtherConfig.class,TempBean.class})
public class ApplicationConfig {
@Autowired
private OtherBean otherBean;
@Bean
public MyBean myBean(){
MyBean myBean = new MyBean();
myBean.setOtherBean(otherBean);
return myBean;
}
}
==============================================
public class ApplicationOtherConfig {
@Bean
public OtherBean otherBean(){
return new OtherBean();
}
}
==============================================
public class TempBean {
}
==============================================
public class MyBean {
private OtherBean otherBean;
public OtherBean getOtherBean() {
return otherBean;
}
public void setOtherBean(OtherBean otherBean) {
this.otherBean = otherBean;
}
}
- 方式二:ImportSelector通过选择器导入
@Configuration
@Import(MyImportSelector.class)
public class ApplicationConfig {
}
===========================================
public class MyImportSelector implements ImportSelector{
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
return new String[]{
"cn.hx.spring._08import_selector.MyBean",
"cn.hx.spring._08import_selector.OtherBean"
};
}
}
===========================================
public class OtherBean {
}
===========================================
public class MyBean {
}
- 方式三:ImportBeanDefinitionRegistrar通过注册器导入
@Configuration
@Import(MyImportBeanDefinitionRegistrar .class)
public class ApplicationConfig {
}
===========================================
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
registry.registerBeanDefinition("myBean",new RootBeanDefinition(MyBean.class));
registry.registerBeanDefinition("otherBean",new RootBeanDefinition(OtherBean.class));
}
}
===========================================
public class OtherBean {
}
===========================================
public class MyBean {
}
⑥ 用FactoryBean注册组件
public class MyBeanFactoryBean implements FactoryBean<MyBean>{
@Override
public MyBean getObject() throws Exception {
return new MyBean();
}
@Override
public Class<?> getObjectType() {
return MyBean.class;
}
@Override
public boolean isSingleton() {
return true;
}
}
===========================================
@Configuration
public class ApplicationConfig {
@Bean
public MyBeanFactoryBean myBean(){
return new MyBeanFactoryBean();
}
}
===========================================
public class MyBean {
}
⑦组件生命周期
@Scope("prototype")
@Bean(initMethod = "init",destroyMethod = "destory")
public Car car()
{
return new Car();
}
===========================================
@Test
public void test01(){
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(MainConfigLifeCycle.class);
context.getBean("car");
context.close();
}
- 方式二、接口InitializingBean和DisposableBean
public class MyBean implements InitializingBean,DisposableBean{
@Override
public void destroy() throws Exception {
System.out.println("销毁了");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("被初始化了");
}
}
===========================================
@Configuration
public class ApplicationConfig {
@Bean
public MyBean myBean(){
return new MyBean();
}
}
- 方式三、注解@PostConstruct @PreDestroy
public class MyBean {
@PreDestroy
public void destroy() throws Exception {
System.out.println("销毁了");
}
@PostConstruct
public void afterPropertiesSet() throws Exception {
System.out.println("被初始化了");
}
}
===========================================
@Configuration
public class ApplicationConfig {
@Bean
public MyBean myBean(){
return new MyBean();
}
}