Spring 初探(五)(Spring Bean 自动装配与自定义监听事件)
2016-12-21 17:24
381 查看
现在通行的使用bean的方法是不通过xml进行配置,而仅仅采用java
内置的类指明bean及相应的依赖关系,下面展开叙述。
@Configuration可以声明某个类是作为配置类,在这样的类中
通过@Bean进行修饰的方法可以返回相应的bean类
通过由AnnotationConfigApplicationContext初始化的Application的getBean
方法(以类名.class为参数)即可得到相应bean实例。
(其找到相应bean类的方式可以相应地理解为byType)
可以在最后实现文件中加载多个configure类,相应参照网站(register refresh)
同样地可以对bean实现依赖注入的功能。(即bean间的依赖)
这可以通过构造函数的方式进行,@Bean函数的构造返回与类的初始化方式相一致。
使用@Import(ConfigClass.class)对具有@Configuration的配置类进行修饰
可以使得在该配置类中使用ConfigureClass中定义的bean作为依赖。
在xml bean中可以规定的init-method及destory-method在@Bean的配置形式中有相应的
版本,即@Bean(initMethod = "*", destoryMethod = "*")
类似地有scope的相应操作,如在@Bean下加修饰@Scope("prototype")
可以将每一个返回新的bean实体。
Spring中的事件处理基本上类似于signal的意义(熟悉scrapy或者一些基于事件驱动
进行传递信息框架的人很熟悉)
如:
监听 ContextStartedEvent -> implements ApplicationListener<ContextStartedEvent>
-> Override onApplicationEvent(ContextStartedEvent event)
相应地有对ContextStoppedEvent的处理。
下面看一个实现了利用@Bean进行自动配置及进行事件处理的例子。
下面对自定义事件展开简要介绍,
自定义事件需要定义继承org.springframework.context.ApplicationEvent
的事件类,由于基类没有提供默认构造函数,在继承时必须重载基于source
的构造函数,source参数指定了事件的publisher,一般在此publisher中
的某个函数中对此事件进行初始化,并调用此publisher的一个方法
实现事件的publish。
值得注意的是,下面例子中的publish的细节是调用一个ApplicationEventPublisher接口
的实现类相应的publishEvent函数。
(这里使用了java的一个特性,即java能够对接口及其实现类实施基类与派生类的多态
关系,基本上就是虚基类的作用)
事件处理的部分已经在前面提到了(onApplicationEvent)
下面是示例代码:
内置的类指明bean及相应的依赖关系,下面展开叙述。
@Configuration可以声明某个类是作为配置类,在这样的类中
通过@Bean进行修饰的方法可以返回相应的bean类
通过由AnnotationConfigApplicationContext初始化的Application的getBean
方法(以类名.class为参数)即可得到相应bean实例。
(其找到相应bean类的方式可以相应地理解为byType)
可以在最后实现文件中加载多个configure类,相应参照网站(register refresh)
同样地可以对bean实现依赖注入的功能。(即bean间的依赖)
这可以通过构造函数的方式进行,@Bean函数的构造返回与类的初始化方式相一致。
使用@Import(ConfigClass.class)对具有@Configuration的配置类进行修饰
可以使得在该配置类中使用ConfigureClass中定义的bean作为依赖。
在xml bean中可以规定的init-method及destory-method在@Bean的配置形式中有相应的
版本,即@Bean(initMethod = "*", destoryMethod = "*")
类似地有scope的相应操作,如在@Bean下加修饰@Scope("prototype")
可以将每一个返回新的bean实体。
Spring中的事件处理基本上类似于signal的意义(熟悉scrapy或者一些基于事件驱动
进行传递信息框架的人很熟悉)
如:
监听 ContextStartedEvent -> implements ApplicationListener<ContextStartedEvent>
-> Override onApplicationEvent(ContextStartedEvent event)
相应地有对ContextStoppedEvent的处理。
下面看一个实现了利用@Bean进行自动配置及进行事件处理的例子。
package com.tutorialspoint; import org.springframework.beans.factory.annotation.Value; /** * Created by admin on 2016/12/21. */ public class HelloWorld { @Value("Hello World!") private String message; public void setMessage(String message) { this.message = message; } public void getMessage(){ System.out.println("Your mesage : " + message); } } package com.tutorialspoint; import org.springframework.beans.factory.annotation.Value; /** * Created by admin on 2016/12/21. */ public class HelloWorld { @Value("Hello World!") private String message; public void setMessage(String message) { this.message = message; } public void getMessage(){ System.out.println("Your mesage : " + message); } } package com.tutorialspoint; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextStoppedEvent; /** * Created by admin on 2016/12/21. */ public class CStopEventHandler implements ApplicationListener<ContextStoppedEvent>{ public void onApplicationEvent(ContextStoppedEvent event){ System.out.println("ContextStopped Received"); } } package com.tutorialspoint; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextStoppedEvent; /** * Created by admin on 2016/12/21. */ public class CStopEventHandler implements ApplicationListener<ContextStoppedEvent>{ public void onApplicationEvent(ContextStoppedEvent event){ System.out.println("ContextStopped Received"); } } package com.tutorialspoint; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextStoppedEvent; /** * Created by admin on 2016/12/21. */ public class CStopEventHandler implements ApplicationListener<ContextStoppedEvent>{ public void onApplicationEvent(ContextStoppedEvent event){ System.out.println("ContextStopped Received"); } }
下面对自定义事件展开简要介绍,
自定义事件需要定义继承org.springframework.context.ApplicationEvent
的事件类,由于基类没有提供默认构造函数,在继承时必须重载基于source
的构造函数,source参数指定了事件的publisher,一般在此publisher中
的某个函数中对此事件进行初始化,并调用此publisher的一个方法
实现事件的publish。
值得注意的是,下面例子中的publish的细节是调用一个ApplicationEventPublisher接口
的实现类相应的publishEvent函数。
(这里使用了java的一个特性,即java能够对接口及其实现类实施基类与派生类的多态
关系,基本上就是虚基类的作用)
事件处理的部分已经在前面提到了(onApplicationEvent)
下面是示例代码:
package com.tutorialspoint; import org.springframework.context.ApplicationEvent; /** * Created by admin on 2016/12/21. */ public class CustomEvent extends ApplicationEvent { public CustomEvent(Object source){ super(source); } public String toString(){ return "My Custom Event"; } } package com.tutorialspoint; import org.springframework.context.ApplicationEvent; /** * Created by admin on 2016/12/21. */ public class CustomEvent extends ApplicationEvent { public CustomEvent(Object source){ super(source); } public String toString(){ return "My Custom Event"; } } package com.tutorialspoint; import org.springframework.context.ApplicationListener; /** * Created by admin on 2016/12/21. */ public class CustomEventHandler implements ApplicationListener<CustomEvent> { public void onApplicationEvent(CustomEvent event){ System.out.println(event.toString()); } } package com.tutorialspoint; import org.springframework.context.annotation.*; /** * Created by admin on 2016/12/21. */ @Configuration public class TestConfigure { @Bean public CustomEventHandler customEventHandler(){ return new CustomEventHandler(); } @Bean public CustomEventPublisher customEventPublisher(){ return new CustomEventPublisher(); } } package com.tutorialspoint; import org.springframework.context.annotation.*; import org.springframework.context.ConfigurableApplicationContext; /** * Created by admin on 2016/12/21. */ public class MainApp { public static void main(String [] args){ ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(TestConfigure.class); CustomEventPublisher cvp = (CustomEventPublisher) context.getBean(CustomEventPublisher.class); cvp.publish(); cvp.publish(); } }
相关文章推荐
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-004-消除BEAN自动装配的歧义@QUALIFIER及自定义注解
- Spring 全注解配置 bean 和 调用 (6) 自定义注解 自动装配歧义性消除
- [Spring]Bean的自动装配Autowire【学习笔记】
- Spring常用注解,自动扫描装配Bean
- Spring -- 入门,装备集合,自动装配,分散装配,自定义编辑器
- 一个和spring整合的依赖注入的servlet,子类可自动装配依赖Bean
- Spring -- 入门,装备集合,自动装配,分散装配,自定义编辑器
- Quartz SchedulerFactoryBean异常--Spring自动装配惹的祸
- Spring视频学习笔记(3)Bean的作用域及自动装配
- Spring3系列8- Spring 自动装配 Bean
- Quartz SchedulerFactoryBean异常--Spring自动装配惹的祸
- 一个和spring整合的依赖注入的servlet,子类可自动装配依赖Bean
- Spring 管理Bean(获取Bean,初始化bean事件,自动匹配ByName······等)
- Spring中Bean自动装配的五种模式
- java Spring常用注解,自动扫描装配Bean
- spring initializingBean vs init-method && 自动装配
- Spring内核研究-管理bean之间的关系三(自动装配)
- 11级_Java_曹建波 05.06 Spring的autowire自动装配bean的四种方式
- Java乔晓松-spring自动装配Bean的4种方式
- 7.5.6: Spring容器中的Bean---使用自动装配注入合作者Bean