Spring注解
1.@Qualifier:缩小匹配类型,以至于为每个参数选择特定的Bean。如下例子:
public class MovieRecommender {
@Autowired
@Qualifier("main")
private MovieCatalog movieCatalog;
}
//为构造方法制定特定的Bean
public class MovieRecommender {
private MovieCatalog movieCatalog;
private CustomerPreferenceDao customerPreferenceDao;
@Autowired
public void prepare(@Qualifier("main")MovieCatalog movieCatalog,
CustomerPreferenceDao customerPreferenceDao) {
this.movieCatalog = movieCatalog;
this.customerPreferenceDao = customerPreferenceDao;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean class="example.SimpleMovieCatalog">
//限定符为"main"的Bean
<qualifier value="main"/>
</bean>
<bean class="example.SimpleMovieCatalog">
//限定符为"action"的Bean
<qualifier value="action"/>
</bean>
<bean id="movieRecommender" class="example.MovieRecommender"/>
</beans>
2.@Resource,@Autowired, @Inject功能一样,都是依赖注入
public class MovieRecommender {
//依赖注入
@Resource
private CustomerPreferenceDao customerPreferenceDao;
private MovieFinder movieFinder;
//制定特定的Bean
@Resource(name="myMovieFinder")
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
public MovieRecommender() {
}
}
3.@Named,@Component,@ManagedBean都可以启用组件扫描,使Spring创建被注解的类上
@Named
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Inject
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
}
@Named("movieListener") //也可以使用@ManagedBean("movieListener")
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Inject
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
}
4.@Configuration和@Bean连用,他们的应用场景就是java的新特性,不适用spring的ioc依赖注入,利用注解的方式将自己创建的bean融入到spring容器中
@Configuration
public class AppConfig {
@Bean
@Scope("prototype")//指定Bean的范围,默认为单利,等价于XML里"Scope='xxx'"
public MyService myService() {
return new MyServiceImpl();
}
@Bean(initMethod = "init")//创建Bean的时候回调初始化方法,等价于XML里"init-method"
public Foo foo() {
return new Foo();
}
@Bean(destroyMethod = "cleanup")//创建Bean的时候回调销毁方法,等价于XML里"destroy-method"
public Bar bar() {
return new Bar();
}
}
//以下跟上边的是等价的
<beans>
<bean id="myService" class="com.acme.services.MyServiceImpl"/>
</beans>
5.@ComponentScan(basePackages = “com.acme”)启用扫描组件以及扫描哪个包,等价的xml配置如下:
<beans>
<context:component-scan base-package="com.acme"/>
</beans>
6.@Description对@Bean的描述:
@Configuration
public class AppConfig {
@Bean
@Description("Provides a basic example of a bean")
public Foo foo() {
return new Foo();
}
}
7.@ImportResource(“classpath:/com/acme/properties-config.xml”)为注解的类引入xml文件,得到配置文件里的属性值。例如
@Configuration
@ImportResource("classpath:/com/acme/properties-config.xml")//引入配置文件
public class AppConfig {
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public DataSource dataSource() {
return new DriverManagerDataSource(url, username, password);
}
}
//被引入的配置文件
properties-config.xml
<beans>
<context:property-placeholder location="classpath:/com/acme/jdbc.properties"/>
</beans>
8.@PropertySource(“classpath:/com/myco/app.properties”)引入键值对存在的配置文件,然后通过 Environment 这个类取出键多对应的值。例子如下:
@Configuration
@PropertySource("classpath:/com/myco/app.properties")
public class AppConfig {
@Autowired
Environment env;
@Bean
public TestBean testBean() {
TestBean testBean = new TestBean();
//env.getProperty("testbean.name")返回值就是"rubby"
testBean.setName(env.getProperty("testbean.name"));
return testBean;
}
}
//app.property
testbean.name=rubby
9.@EventListener事件监听器,它有三个类组成
①.事件类(封装数据,以及传递数据)
②.广播类(事件的发布类,事件监听的入口,执行此类中的方法会触发监听类中的方法)
③.监听类.(广播类执行方法发布广播的时候他就会根据发出的事件类进行匹配,如果匹配就会执行带有@EventListener注解的方法,并且能拿到广播类发布的事件类,并从中取出自己想要的数据),代码如下:
//事件类,封装数据,数据的承载着者,必须继承ApplicationEvent类
public class MyTestEvents extends ApplicationEvent{
private static final long serialVersionUID = 1L;
private String massage;
public MyTestEvents(Object source, String massage) {
super(source);
this.massage = massage;
}
public String getMassage() {
return massage;
}
public void setMassage(String massage) {
this.massage = massage;
}
}
//广播类,广播事件,调用pushListener方法就是监听功能的入口,
@Component
ApplicationContextAware{
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext=applicationContext;
}
public void pushListener(String msg){
applicationContext.publishEvent(new MyTestEvents(this,msg));
}
}
//监听事件的监听者以及事件的接受者
@Component
public class MyEventsListener {
//SpEL表达式,匹配Events对象里massage属性等于"监听成功了",如果匹配成功,那么这个方法就会执行
@EventListener(condition="#events.massage=='监听成功了'")
public void listener(MyTestEvents events){
System.err.println("监听器监听"+events.getMassage());
}
//没有匹配规则,只要是调用了发布者的发布方法,那么这个方法就会执行
@EventListener
public void listener1(MyTestEvents events){
System.err.println("没有监听成功"+events.getMassage());
}
}
//测试类
public class EventsListenerTest extends BaseTest{
//事件监听发布类
@Autowired
private MyTestEventsPublisher myTestEventsPublisher;
@Test
public void listener(){
this.myTestEventsPublisher.pushListener();
}
}
//执行结果为
监听器监听监听成功了
没有监听成功监听成功了
//下边是该注解的其他匹配规则
@EventListener(condition = "#blEvent.test == 'foo'")//只有符合该类下的属性test=“foo"才会触发这个监听器
public void processBlackListEvent(BlackListEvent blEvent) {
...
}
@EventListener({ContextStartedEvent.class, ContextRefreshedEvent.class})//系统默认给出的触发监听条件
public void handleContextStart() {
...
}
@EventListener
@Async//与这个注解连用,那么出发此方法的时候该方法就成了异步方法
public void processBlackListEvent(BlackListEvent event) {
// BlackListEvent is processed in a separate thread
}