如何让不受spring管理的类具有spring自动注入的特性.--AutowireCapableBeanFactory
来源:互联网 发布:淘宝搜索关键词 编辑:程序博客网 时间:2024/05/16 12:20
package spring.core.pracitce;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import org.apache.commons.lang3.StringUtils;import org.junit.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.beans.factory.config.AutowireCapableBeanFactory;import org.springframework.context.ApplicationEvent;import org.springframework.context.ApplicationEventPublisher;import org.springframework.context.ApplicationEventPublisherAware;import org.springframework.context.ApplicationListener;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/** * @link http://www.javacodegeeks.com/2012/08/observer-pattern-with-spring-events.html * 如何让不受spring管理的类具有spring自动注入的特性. * 例子:java配置方式 * @author doctor * * @time 2014年12月3日 下午4:04:44 */public class ObserverPatternWithSpringEvents {@Autowired@Qualifier("messageEventSource")private MessageEventSource messageEventSource;private AnnotationConfigApplicationContext context;@Test public void test()throws Throwable{context = new AnnotationConfigApplicationContext(ObserverPatternWithSpringEventsConfigure.class);//AutowireCapableBeanFactory有个下面重要的方法可以实现让不受spring管理的类具有spring自动注入的特性.context.getAutowireCapableBeanFactory().autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, true);Thread thread = new Thread(messageEventSource);thread.start();thread.join();}@Configurationpublic static class ObserverPatternWithSpringEventsConfigure{@Bean(name="messageEventSource")public MessageEventSource messageEventSource(){return new MessageEventSource();}@Bean(name="messageListener")public MessageListener messageListener(){return new MessageListener();}}public static class MessageEvent extends ApplicationEvent{private static final long serialVersionUID = 4927935382440103532L;private String message = "";public MessageEvent(Object source,String message) {super(source);if (!StringUtils.isBlank(message)) {this.message = message;}}@Overridepublic String toString(){StringBuilder stringBuilder = new StringBuilder();stringBuilder.append("MessageEvent [message=").append(message).append("]");return stringBuilder.toString();}}/** * 实现ApplicationEventPublisherAware接口,是为了让spring注入ApplicationEventPublisher, * 它是spring分发事件源 * @author doctor * * 2014年12月4日 下午10:52:18 */public static class MessageEventSource implements Runnable,ApplicationEventPublisherAware{private ApplicationEventPublisher applicationEventPublisher;@Overridepublic void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {this.applicationEventPublisher = applicationEventPublisher;}@Overridepublic void run() {BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));try {while (true) {String readLine = reader.readLine();//发布事件applicationEventPublisher.publishEvent(new MessageEvent(this, Thread.currentThread().getName() + "---" + readLine));}} catch (IOException e) {e.printStackTrace();}}}public static class MessageListener implements ApplicationListener<MessageEvent>{@Overridepublic void onApplicationEvent(MessageEvent event) {System.out.println(Thread.currentThread().getName() + event);}}}
0 0
- 如何让不受spring管理的类具有spring自动注入的特性.--AutowireCapableBeanFactory
- 不受spring管理的类注入问题
- Spring中AutowireCapableBeanFactory的使用
- Spring中AutowireCapableBeanFactory的使用
- Spring中AutowireCapableBeanFactory的使用
- Spring中AutowireCapableBeanFactory的使用
- Spring的自动注入
- spring--filter如何注入 spring管理的bean(54)
- Spring学习,集合类的自动注入
- Spring的自动扫描注入
- Servlet注入Spring管理的类
- spring 常识及不受spring管理的插件里面获取bean
- 【Spring】Spring的注入
- quartz 与Spring的整合-Quartz中的job如何自动注入spring容器托管的对象
- Quartz与Spring的整合-Quartz中的job如何自动注入spring容器托管的对象
- Quartz与Spring的整合-Quartz中的job如何自动注入spring容器托管的对象
- Quartz与Spring的整合-Quartz中的job如何自动注入spring容器托管的对象
- Quartz与Spring的整合-Quartz中的job如何自动注入spring容器托管的对象
- UI界面效果研究1
- Seqlist 代码
- 自定义时间控件MDateWidget
- android给字体加粗上色
- Linux之Makefile(make自动推导)
- 如何让不受spring管理的类具有spring自动注入的特性.--AutowireCapableBeanFactory
- day12
- Convert Sorted Array to Binary Search Tree
- C语言 冒泡法排序,选择法排序和插入排序
- Cart项目开发记录学习笔记
- Tomcat设置cmd窗口的title属性
- 阿里云服务器安装配置redis并且加入到开机启动
- UTF-8转换成UTF-16或者CSTtring
- 在ubuntu中使用smaba