`

org.springframework.context.ApplicationContextAware使用理解

 
阅读更多

一、这个接口有什么用?

当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean。换句话说,就是这个类可以直接获取spring配置文件中,所有有引用到的bean对象。

二、怎么用?

举个例子吧:

例如我有一个方法类AppUtil,这个方法类中需要使用到的ApplicationContext中的某个bean(companyService)。

1、因为spring要建立属于自己的容器,就必须要加载自己的配置文件。

     这个时候,需要注册ContextLoaderListener或者这个类的子类。

在web.xml加上以下的信息:

<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

当然,这样子的话只会读取默认路径下的application.xml配置文件的。如果需要读取特定路径下的配置文件。需要在web.xml中

添加如下信息。可以参考我的示例,指定配置文件,如下:

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:conf/app-context.xml</param-value>
 </context-param>

 

注意:<param-name>contextConfigLocation</param-name>是不能改变的。

 

2、方法类AppUtil的处理

方法类AppUtil实现ApplicationContextAware接口:

public class AppUtil
  implements ApplicationContextAware

为方法类AppUtil增加一个静态的成员ApplicationContext类型的对象。以后方法类AppUtil获取ApplicationContext,就是通过读取这个

成员变量的。具体如下所示:

private static ApplicationContext appContext;

 

实现ApplicationContextAware接口的默认方法:

 public void setApplicationContext(ApplicationContext paramApplicationContext)
    throws BeansException
  {
    appContext = paramApplicationContext;
  }

 

3、在spring的配置文件中,注册方法类AppUtil

严格上来说,方法类AppUtil是一个bean,而且从步骤2中我们不难发现,之所以方法类AppUtil能够灵活自如地获取ApplicationContext

就是因为spring能够为我们自动地执行了setApplicationContext。但是,spring不会无缘无故地为某个类执行它的方法的,所以,就很有必要

通过注册方法类AppUtil的方式告知spring有这样子一个类的存在。

其实,方法很简单,就是将方法类AppUtil作为一个普通的bean在spring的配置文件中进行注册:

<bean id="appUtil" class="com.htsoft.core.util.AppUtil"/>

4、使用静态的成员ApplicationContext类型的对象,appContext,来调用其他bean。在方法类AppUtil中增加如下方法:

public static Object getBean(String paramString)
  {
    return appContext.getBean(paramString);
  }

那么,在

方法类AppUtil中就能够灵活地调用其他任何一个bean了,例如:

CompanyService localCompanyService = (CompanyService)getBean("companyService");

注:配置文件中关于companyService的内容:

<bean id="companyService" class="com.kaiwii.service.system.impl.CompanyServiceImpl">
        <constructor-arg index="0" ref="companyDao"/>      
</bean>

 

分享到:
评论

相关推荐

    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 4.0.7 源码

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

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

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

    spring3.1.4源码

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

    RabbitMQ-高级篇.pdf

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

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

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

    采用springContextAware注册javaBean的例子

    import org.springframework.context.ApplicationContextAware; public class MyBean 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配置扫描多个包问题解析

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

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

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

    DWR comet的完整实现.pdf

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

    spring-framework-reference4.1.4

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

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

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

    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...

    springTrigger 触发器

    自定义任务类需要实现`org.quartz.Job`接口或者继承`org.springframework.scheduling.quartz.StatefulJob`(如果需要保持任务状态),并在`execute(JobExecutionContext context)`方法中编写具体的执行逻辑。...

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

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

    SpringPython使用介绍指南.doc

    xsi:schemaLocation="http://www.springframework.org/springpython/schema/objects/ http://springpython.webfactional.com/schema/context/spring-python-context-1.1.xsd"&gt; class="springpythontest.support....

    Spring-EventDemo.zip

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

Global site tag (gtag.js) - Google Analytics