`

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的依赖,可以比较容易从现有的框架中脱离

分享到:
评论

相关推荐

    Spring Boot技术知识点:如何读取不同路径里的applicationContext.xml配置文件2

    import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextInitializer; import org.springframework.core.io.Resource; import org.springframework....

    spring boot源码-自定义初始化器的三种方式

    import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; ...

    采用springContextAware注册javaBean的例子

    import org.springframework.context.ApplicationContextAware; public class MyBean implements ApplicationContextAware { private static ApplicationContext context; @Override public void ...

    spring 4.0.7 源码

    在源码中,可以看到`org.springframework.beans`和`org.springframework.context`包下的类,如`BeanFactory`和`ApplicationContext`,它们是IoC容器的实现基础。 2. **AOP(Aspect Oriented Programming)**:...

    DWR comet的完整实现.pdf

    import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationEvent; public class DwrService implements ApplicationContextAware { private ...

    spring配置扫描多个包问题解析

    import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.spring...

    RabbitMQ-高级篇.pdf

    import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; @SpringJUnitConfig(CommonConfig.class) @TestPropertySource(locations = "classpath:application.yml") public class ...

    spring3.1.4源码

    在`org.springframework.jdbc`和`org.springframework.orm`包中,可以研究其底层实现。 5. **Spring MVC**:作为Spring的Web MVC框架,Spring MVC负责处理HTTP请求,提供模型-视图-控制器结构。`org.spring...

    Spring-EventDemo.zip

    &lt;bean id="eventPublisher" class="org.springframework.context.ApplicationContextAware"&gt; ``` 发布事件时,调用`eventPublisher.publishEvent(event)`,其中`event`是你要发布的事件对象。 2. 注解方式调用...

    SpringBoot+Mybatis项目使用Redis做Mybatis的二级缓存的方法

    import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component public class ApplicationContextHolder implements ApplicationContextAware { ...

    Web项目中获取SpringBean与在非Spring组件中获取SpringBean.pdf

    import org.springframework.web.context.support.SpringBeanAutowiringSupport; public class SpringWiredBean extends SpringBeanAutowiringSupport { @Autowired private static BeanFactory beanFactory; /...

    Spring Boot 自定义 Shiro 过滤器无法使用 @Autowired问题及解决方法

    import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; / * Spring 上下文工具类 * * @author nwgdk */ @Component public class ...

    Spring Boot中Bean定义方调用方式解析

    import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component public class SpringUtil implements ApplicationContextAware { private static ...

    Springboot实现多线程注入bean的工具类操作

    import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component public class SpringUtils implements ApplicationContextAware { private static ...

    spring中通过ApplicationContext getBean获取注入对象的方法实例

    import org.springframework.context.ApplicationContextAware; public class SpringContextUtil implements ApplicationContextAware { private static ApplicationContext context; @Override public void ...

    spring-framework-reference-4.1.2

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    Spring 多线程下注入bean问题详解

    import org.springframework.context.ApplicationContextAware; public class ApplicationContextHolder implements ApplicationContextAware { private static ApplicationContext context; @Override public ...

    quartz+spring示例

    &lt;bean id="myJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"&gt; &lt;property name="jobClass" value="com.example.MyJob"/&gt; &lt;bean id="myTrigger" class="org.spring...

Global site tag (gtag.js) - Google Analytics