org.springframework.context.ApplicationContextAware

Spring中提供一些Aware相关接口,像是BeanFactoryAware、 ApplicationContextAware、ResourceLoaderAware、ServletContextAware等等,实现这些 Aware接口的Bean在被初始之后,可以取得一些相对应的资源。例如实作ApplicationContextAware的Bean,在Bean被初始后,将会被注入 ApplicationContext的实例等等。 Bean取得BeanFactory、ApplicationContextAware的实例目的是什么,一般的目的就是要取得一些档案资源的存取、相 关讯息资源或是那些被注入的实例所提供的机制



import org.springframework.context.ApplicationContextAware;

import org.springframework.context.ApplicationContext;

import org.springframework.beans.BeansException;

public class GlobalParamUtils implements ApplicationContextAware {

     private static ApplicationContext ac;

      public void setApplicationContext(ApplicationContext arg0) throws BeansException {
this.ac = arg0;

      }

 public static String generateCodeByType(String type) {

    String result = "";

    if (ac.containsBean("swmsBussinessCodeManager")) {

    BussinessCodeManager codeManager = (BussinessCodeManager) ac.getBean("swmsBussinessCodeManager");

    try {

        Method method = codeManager.getClass().getMethod(type, new Class[0]);

result = (String) method.invoke(codeManager, new Object[0]);

} catch (IllegalArgumentException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

} catch (SecurityException e) {

e.printStackTrace();

} catch (NoSuchMethodException e) {

e.printStackTrace();

}

    }

    return result;

    }

}



//swmsBussinessCodeManager在bean中的配置

        <bean class="com.vtradex.swms.server.service.GlobalParamUtils"/>

        <bean id="swmsBussinessCodeManager" parent="abstractTransactionProxy">
           <property name="target">
                    <bean class="com.vtradex.swms.server.service.sequence.pojo.DefaultBussinessCodeManager" parent="baseManager">
  <constructor-arg index="0">
<ref bean="sequenceGenerater"/>
</constructor-arg>
<constructor-arg index="1">
<ref bean="swmsSequenceDao"/>
</constructor-arg>
     </bean>
           </property>
</bean>

基本上,Spring虽然提供了这些Aware相关接口,然而Bean上若实现了这些界面,就算是与Spring发生了依赖,从另一个角度来看,虽然您可以直接在Bean上实现这些接口,但您也可以透过setter来完成依赖注入,例如:

public class GlobalParamUtils {

     private static ApplicationContext ac;

      public void setApplicationContext(ApplicationContext arg0) throws BeansException {
this.ac = arg0;

      }

 public static String generateCodeByType(String type) {

     String result = "";

     if (ac.containsBean("swmsBussinessCodeManager")) {  

     BussinessCodeManager codeManager =                          (BussinessCodeManager)ac.getBean("swmsBussinessCodeManager");

     try {

         Method method = codeManager.getClass().getMethod(type, new Class[0]);

result = (String) method.invoke(codeManager, new Object[0]);

} catch (IllegalArgumentException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

} catch (SecurityException e) {

e.printStackTrace();

} catch (NoSuchMethodException e) {

e.printStackTrace();

}

     }

     return result;

    }

}

注意这次我们并没有实作ApplicationContextAware,我们在程序中可以自行注入ApplicationContext实例:

ApplicationContext context = new ClassPathXmlApplicationContext("service.xml");


Bean而言,降低了对Spring的依赖,可以比较容易从现有的框架中脱离

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

woshimyc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值