`

Spring Bean相关的接口、aware接口、FactoryBean

阅读更多
1、
Java代码  收藏代码
package spring.beansandcontext; 
 
import org.springframework.beans.BeansException; 
import org.springframework.beans.factory.BeanClassLoaderAware; 
import org.springframework.beans.factory.BeanFactory; 
import org.springframework.beans.factory.BeanFactoryAware; 
import org.springframework.beans.factory.BeanNameAware; 
import org.springframework.beans.factory.DisposableBean; 
import org.springframework.beans.factory.InitializingBean; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.ApplicationContextAware; 
 
//如果被装配的Bean实现了相应的接口,就可以在Bean中获得相应的信息。,或者进行某些操作。 
public class HelloServiceImpl implements HelloService, 
//以下同时为接口的调用的顺序 
BeanNameAware,//获得Bean名,也就是<Bean>标签的id属性值。 
BeanClassLoaderAware,//获得装载过程中的ClassLoader对象。 
BeanFactoryAware,//获得BeanFactory对象 
ApplicationContextAware,//获得ApplicationContext对象 
 
InitializingBean, //在Bean的所有属性设置完后,并且在调用完上面接口的方法后,调用此接口的afterPropertiesSet方法 
DisposableBean //当销毁Bean时,调用此接口的destroy方法 

    private String greeting; 
 
    public String getGreeting() { 
        // TODO Auto-generated method stub 
        return "hello " + greeting; 
    } 
 
    // 以下同时为方法的调用的顺序 
    public void setGreeting(String greeting) { 
        this.greeting = greeting; 
        System.out.println("setGreeting 设置greeting属性值"); 
    } 
 
    public void setBeanName(String name) { 
        System.out.println("BeanNameAware 接口方法  获得Bean名,也就是<Bean>标签的id属性值:" + name); 
    } 
 
    public void setBeanClassLoader(ClassLoader arg0) { 
        System.out.println("BeanClassLoaderAware接口方法 获得装载过程中的ClassLoader对象:" + arg0); 
    } 
 
    public void setBeanFactory(BeanFactory arg0) throws BeansException { 
        System.out.println("BeanFactoryAware接口方法 获得BeanFactory对象:" + arg0); 
    } 
 
    public void setApplicationContext(ApplicationContext applicationContext) 
            throws BeansException { 
        System.out.println("ApplicationContextAware接口方法 获得ApplicationContext对象:" + applicationContext); 
    } 
 
    public void afterPropertiesSet() throws Exception { 
        System.out.println("InitializingBean接口方法 afterPropertiesSet"); 
 
    } 
    public void initMethod() { 
        System.out.println("<bean>标签的init-Method属性指定的方法,此方法在afterPropertiesSet()之后调用"); 
    } 
 
    public void destroy() throws Exception { 
        System.out.println("DisposableBean 接口方法 destroy"); 
    } 
 
    public void destroyMethod() { 
        System.out.println("<bean>标签的destroy-Method属性指定的方法,此方法在destroy()之后调用"); 
    } 


2、Spring的InitializingBean和init-method


Spring的InitializingBean和init-method
Spring在设置完一个bean所有的属性后,会检查bean是否实现了InitializingBean接口,如果实现就调用bean的afterPropertiesSet方法。另外,如果bean是单例的,则afterPropertiesSet方法只会被调用一次;否则每次创建bean时afterPropertiesSet方法都会被重新调用.
Spring虽然可以通过InitializingBean完成一个bean初始化后对这个bean的回调,但是这种方式要求bean实现 InitializingBean接口。一但bean实现了InitializingBean接口,那么这个bean的代码就和Spring耦合到一起了。通常情况下不建议直接实现InitializingBean,而是用Spring提供的init-method的功能来执行一个bean 子定义的初始化方法,这可以在一个bean的配置文件中通过init-method声明:
<bean id="testBean" class="TestClass" init-method="initialize"/>

spring要求这个init-method方法是一个无参数的方法
如果一个bean同时实现了这两种方式的初始化配置,则spring会先调用afterPropertiesSet方法,然后通过反射调用init-method,任何一个方法出错都会导致spring创建bean失败.如果afterPropertiesSet方法调用失败,也不会再继续执行init-mehtod方法.


init-method属性用于指定自定义初始化方法

---------------------------------

引用配置文件的值
<!-- cache.properties中定义memcache.cache_prefix=sns\:2\: -->
<bean id="globalCacheManager" class="com.xiaonei.wap.framework.cache.GlobalCacheManager">
<property name="memCacheManager" ref="memCacheManger" />
<property name="cachePrefix" value="${memcache.cache_prefix}" />
</bean>

spring提供了很多*aware接口,比较常用的就是BeanFactoryAware,ApplicationContextAware,BeanNameAware
实现这些接口的Bean则能获得相应的BeanFactory,ApplicationContext实例,从而能直接获得容器信息。

3、FactoryBean接口作用就是封装自己定制的实例化逻辑(例如你想用工厂模式来实例化,或者Class.getInstance()),然后让spring统一管理。
http://fuhuijun.iteye.com/blog/1260609
http://readwall.blog.163.com/blog/static/10127132201042751036331
FactoryBean 用于在 spring 容器中创建其他的 Bean, 我们平时用得最多的 JndiObjectFactoryBean, hibernate 的 LocalSessionFactoryBean 都是 FactoryBean 的具体实现, 既然如此, 读取动态配置就变得易如反掌了, 假如我们要实现动态读取数据库配置的功能, 拿使用率最高的 BasicDatasource 为例, 简单的实现一个 BasicDatasource FactoryBean 如下即可

  代码

public class BasicDataSourceFactoryBean implements FactoryBean {
 public Object getObject() throws Exception {
  BasicDataSource dataSource = new BasicDataSource();
  // 读取外部配置, 设置到 dataSource 中 ...
  return dataSource;
 }

 public Class getObjectType() {
  return BasicDataSource.class;
 }

 public boolean isSingleton() {
  return true;
 }
}

然后在 spring 中如此声明

<bean id="dataSource" class="BasicDataSourceFactoryBean ">
... 你的配置来源
</bean>
4、BeanPostProcessor,BeanFactoryPostProcessor接口都是容器扩展点,容许spring在bean初始化或者销毁的时候回调,回调顺序可以用order来指定,前提是必须执行Ordered接口。
分享到:
评论

相关推荐

    spring-aware接口实现与bean作用域(spring多容器层面)

    使用了ApplicationContextAware接口,获取spring管理的bean; 多项目整合夸spring容器获取bean的实现方式。

    spring入门 aware接口实现

    通过aware接口,可以对spring相应资源(可能包含相关核心资源)进行操作(一定要慎重) 首先创建一个类,实现ApplicationContextAware接口 , 该借口需要实现 setApplicationContext方法,该方法的参数由容器传递...

    Spring实现Aware接口自定义获取bean的两种方式

    主要介绍了Java编程实现Aware接口自定义获取bean的两种方式,通过BeanFactoryAware和ApplicationContextAware,具有一定参考价值,需要的朋友可以了解下。

    Spring特性——Aware感知特性

    NULL 博文链接:https://ylxy3058.iteye.com/blog/2225314

    Spring Aware标记接口使用案例解析

    主要介绍了Spring Aware标记接口使用案例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    开源框架面试专题及答案.pdf

    同样的,当一个 bean 不在被调用时需要进行相关的析构操 作,并从 bean 容器中移除。 &gt; Spring bean factory 负责管理在 spring 容器中被创建的 bean 的生命周期。Bean 的生命 周期由两组回调(call back)方法组成...

    Spring 3 Reference中文

    4.6.3 其它Aware 接口 75 4.7 Bean 定义的继承. 77 4.8 容器扩展点. 78 4.8.1 使用BeanPostProcessor 来自定义bean 78 4.8.1.1 示例:BeanPostProcessor 风格的Hello World.. 79 4.8.1.2 ...

    开源框架 Spring Gossip

    不使用XML定义档进行 Bean设置 Aware 相关介面 BeanPostProcessor BeanFactoryPostProcessor PropertyPlaceholderConfigurer PropertyOverrideConfigurer CustomEditorConfigurer ...

    Self-Aware Computing Systems

    Self-Aware Computing Systems ISBN-10 书号: 3319474723 ISBN-13 书号: 9783319474724 Edition 版本: 1st ed. 2017 出版日期: 2017-01-23 pages 页数: (722 ) $219.99 This book provides formal and informal ...

    USB接口器件封装库大全表贴直插PCB图MICRO MINI USB USB3.0接口等20个ADf封装库( 2D3D库)

    USB接口器件封装库大全表贴直插PCB图MICRO MINI USB USB3.0接口等20个ADf封装库( 2D3D库),包含市面上所有常用USB接口pcb封装库,可直接应用到你的项目设计中。 PCB封装型号: 61400413321 61400416021 61400416121...

    Spring-quartz.zip

    Spring动态任务与静态任务调用实例 静态任务直接使用了spring进行的管理 利用了aware与FactoryBean的思想 动态任务仍然使用quartz,使用Scheduler进行任务管理

    CVPR2022 Image Dehazing Transformer with Transmission-Aware 3D代码

    CVPR 2022 Image Dehazing Transformer with Transmission-Aware 3D Position Embedding 源代码,很有学习价值,大佬任文琦团队的最新去雾杰作,从3D定位的角度去思考去雾。CVPR 2022 Image Dehazing Transformer ...

    Wi-Fi_Aware_Specification_v3.2

    Wi-Fi_Aware_Specification_v3.2

    springboot学习思维笔记.xmind

    Spring Aware BeanNameAware BeanFactoryAware ApplicationContextAware MessageSourceAware ApplicationEventPublisherAware ResourceLoaderAware 多线程 计划任务 cron fixDelay ...

    JavaEE开发的颠覆者 Spring Boot实战,多个地址免费下载,

    3.1 Spring Aware .... ..................................... 48 3.2 多线程 .... .... ......... 51 3.3 计划任务 .... .... ..... 54 3.4 条件注解@Conditional .... .................... 56 3.5 组合注解与元...

    spring boot 项目代码,直接启动本人编写的,里面包含PPT下载地址

    3.1 Spring Aware .... ..................................... 48 3.2 多线程 .... .... ......... 51 3.3 计划任务 .... .... ..... 54 3.4 条件注解@Conditional .... .................... 56 3.5 组合注解与元...

    Context_aware Sequential Recommender

    Since sequential information plays an ...art sequential recommendation methods and context-aware recommendation methods on two public datasets, i.e., the Taobao dataset and the Movielens-1M dataset.

    UPF Based Power Aware Dynamic Simulation.pdf

    UPF based power aware (PA) verifcation adopts several power dissipation reduction techniques based on the target design implementation and UPF power specifcation or intent, as discussed in Chap....

    JavaEE开发的颠覆者SpringBoot实战[完整版].part3

    3.1 Spring Aware 48 3.1.1 点睛 48 3.1.2 示例 49 3.2 多线程 51 3.2.1 点睛 51 3.2.2 示例 51 3.3 计划任务 54 3.3.1 点睛 54 3.3.2 示例 54 3.4 条件注解@Conditional 56 3.4.1 点睛 56 3.4.2 示例 57 3.5 组合...

    pro Spring

    Become aware of the seamlessness and power of Spring by referencing the easy-to- understand sample application we provide. Learn how to replace common EJB features with Spring alternatives, including...

Global site tag (gtag.js) - Google Analytics