Spring各种注解及含义

@Configuration
注解在类上,标记此类是一个配置类,Spring会扫描到配置类并读取内部配置

@ComponentScan
注解在类上,用于指定spring扫描组件的包路径,可以为数组;还可以进行筛选过滤:excludeFilters includeFilters可以按照类型、正则、自定义(实现接口)、注解、AspectJ

@Controller @Service @Repository @Component
都是用来标记Spring容器组件的,只是场景不同;Controller Service Repository 在Component上扩展
@Resource是java的标准,不指定名称时,按属性名称装配;如果没有,按类型装配

@Bean
用于手动注解一个类, 通常用于注入jar包中不会被扫描到的类,不如数据库连接类

@Autowired
用于向组件内自动装配一个bean,默认按类型装配,如果同样类型有多个,取属性名作为id从容器中获取

@Qualifier
用于从容器中获取指定id的bean,当相同类型的bean有多个时可使用此注解

@Primary
如果相同类型的bean存在多个,某个bean上加了此注解,在不指定名称时,Autowired优先注入添加了Primary的bean,如果使用其他bean,则使用Qualifier指定bean

@Lazy
表示容器在启动时不会加载对应的bean,在get对应bean时才会初始化对应bean

@Conditional
按照一定的条件进行组件注册,可根据不同的环境,加载不同的组件
在启动环境是配置参数:-DisTest=true
Spring各种注解及含义_第1张图片
添加bean组件

@Configuration
@ComponentScan("com.spring.test")
public class MainConfiguration {

    @Bean
    public Car car1() {
        System.out.println("-----------1");
        return new Car();
    }

    @Conditional(TestConditional.class)
    @Bean
    public Car car2() {
        System.out.println("-----------2");
        return new Car();
    }

}

添加TestConditional,必须实现Condition接口,实现matches方法
可获取环境参数,返回true表示加载对应的bean,否则不加载

public class TestConditional implements Condition {

    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        String isTest = conditionContext.getEnvironment().getProperty("isTest");
        return "true".equals(isTest);
    }
}

@Import
此注解用于向容器导入组件,此注解多用于spring框架扩展,mybatis,AOP都有使用,共有3种用法

@Configuration
@Import(Car.class)
// 手动导入多个类
@Configuration
@Import({Car.class, BenzCar.class})
public class MyImportSelector implements ImportSelector {

    @Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        annotationMetadata.getAnnotationTypes().forEach(System.out::println); // 可获取Import此类的类上的注解
        return new String[]{Car.class.getName()}; //类的全路径名
    }
}

@Configuration
@Import(MyImportSelector.class)
public class MyImportBeanDefinitionRegistry implements ImportBeanDefinitionRegistrar {

    @Override
    public void registerBeanDefinitions(
            AnnotationMetadata annotationMetadata,
            BeanDefinitionRegistry beanDefinitionRegistry) {
    	// 添加类定义信息
        RootBeanDefinition beanDefinition = new RootBeanDefinition(Car.class);
        beanDefinitionRegistry.registerBeanDefinition("car", beanDefinition);
        // 删除类定义信息
        beanDefinitionRegistry.removeBeanDefinition("indexController");
    }
}

@Configuration
@Import(MyImportBeanDefinitionRegistry.class)

FactoryBean
返回的是类中getObject的bean对象,可以设置bean是否为单例,如果需要获取MyFactoryBean,应该添加前缀“&”

@Component
public class MyFactoryBean implements FactoryBean<Car> {
    @Override
    public Car getObject() throws Exception {
        return new Car();
    }

    @Override
    public Class<?> getObjectType() {
        return Car.class;
    }

    @Override
    public boolean isSingleton() {
        return false;
    }
}

AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(MainConfiguration.class);
Car car = (Car) ac.getBean("myFactoryBean");
System.out.println(car);
MyFactoryBean factoryBean2 = (MyFactoryBean) ac.getBean("&myFactoryBean");
System.out.println(factoryBean2.isSingleton());

---- 运行结果 ----
Car(name=A6L, brand=Audi)
false

bean生命周期
自定义init和destroy方法

@Data
@AllArgsConstructor
public class Car {
    private String name;
    private String brand;

    public void init() {
        System.out.println("执行了init...");
    }

    public void destroy() {
        System.out.println("执行了destroy...");
    }
}

// 配置类中注入bean 并制定初始化和销毁方法
@Bean(initMethod = "init", destroyMethod = "destroy")
public Car car2() {
	return new Car("A4L", "Audi");
}

AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(MainConfiguration.class);
ac.close();

---- 运行结果 ----
执行了init...
执行了destroy...

InitializingBean
bean实现InitializingBean接口,可在bean初始化并且属性赋值完成之后执行afterPropertiesSet方法

@Data
@AllArgsConstructor
public class Car implements InitializingBean {
    private String name;
    private String brand;

    public void init() {
        System.out.println("执行了init...");
    }

    public void destroy() {
        System.out.println("执行了destroy...");
    }

    @Override
    public void afterPropertiesSet() {
        System.out.println(name + "--" + brand);
    }
}

// 配置类中注入bean 并制定初始化和销毁方法
@Bean(initMethod = "init", destroyMethod = "destroy")
public Car car2() {
	return new Car("A4L", "Audi");
}

AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(MainConfiguration.class);
ac.close();

---- 运行结果 ----
A4L--Audi
执行了init...
执行了destroy...

BeanPostProcessor
BeanPostProcessor为一个接口, 在bean初始化并且属性赋值完成后执行,其中postProcessBeforeInitialization,在Iinit方法之前; 而postProcessAfterInitialization,则在init方法之后执行

@Component
public class MyBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("before " + beanName + " " + bean);
        return null;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("after " + beanName + " " + bean);
        return null;
    }
}

@Bean(initMethod = "init", destroyMethod = "destroy")
public Car car() {
	return new Car("A4L", "Audi");
}

---- 运行结果 ----
before car Car(name=A4L, brand=Audi)
A4L--Audi
执行了init...
after car Car(name=A4L, brand=Audi)

@PostConstruct @PreDestroy
二者作用在bean的方法上, 属于javax的注解; PostConstruct 等同于initMethod; PreDestroy等同于destroyMethod

@PostConstruct
private void postConstruct() {
	System.out.println("执行了postConstruct");
}

@PreDestroy
private void preDestroy() {
	System.out.println("执行了preDestroy");
}

你可能感兴趣的:(spring,java)