很多情况下,我们需要在普通javabean中获取spring 配置文件中的bean。
1.创建一个类 ,让其实现org.springframework.context.ApplicationContextAware接口来让Spring在启动的时候为我们注入ApplicationContext对象.
不实现ApplicationContextAware接口应该也是可以的,不过我这里测试没通过。
示例代码:
public class MyApplicationContextUtil implements ApplicationContextAware{
private static ApplicationContext context;//声明一个静态变量保存
public void setApplicationContext(ApplicationContext contex)
throws BeansException {
this.context=contex;
}
public static ApplicationContext getContext(){
return context;
}
}
2.在applicationContext.xml文件中配置此bean,以便让Spring启动时自动为我们注入ApplicationContext对象.
<!-- 这个bean主要是为了得到ApplicationContext 所以它不需要其它属性-->
<bean id="myApplicationContextUtils" class="org.ing.springutil.MyApplicationContextUtil"></bean>
3.有了这个bean取得ApplicationContext之后我们就可以调用其getBean("beanName")方法来得到由Spring 管理所有对象.
ApplicationContext springContext = MyApplicationContextUtils.getApplicationContext();
ITesTaskExcuteService tesTaskExcuteService=(ITesTaskExcuteService)springContext.getBean("tesTaskExcuteService");
这样就可以取得你想要的spring bean。
分享到:
相关推荐
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean" p:dataSource-ref="dataSourceProxy"> <value>classpath:SqlMapConfig.xml</value> </property> </bean...
以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext. 在配置文件中注入<bean class="xxx.xxx.SpringContextHolder" lazy-init="false" /> 即可使用
Spring Bean封装机制 Bean Wrapper Bean Factory ApplicationContext Web Context Spring 高级特性 Web 应用与 MVC Spring MVC Spring MVC指南 基于模板的Web表示层技术 Web 应用中模板技术与 JSP 技术的...
Bean是Spring管理的基本单位,在基于Spring的Java EE应用中,所有的组件都被当成Bean处理,包括数据源、Hibernate的SessionFactory、事务管理器等。在Spring中,Bean的是一个非常广义的概念,任何的Java对象、Java...
SpringUtil工具类(获取applicationContext,通过name获取 Bean,通过class获取Bean,通过name,以及Clazz返回)
(5)如果Bean实现了ApplicationContextAware接口,则Spring调用setApplicationContext()方法传入当前ApplicationContext实例的引用。 (6)如果BeanPostProcessor和Bean关联,则Spring将调用该接口的预初始化方法...
Spring4 HelloWorld初学Spring,java bean通过applicationContext.xml配置SpringIoC容器生成。
请举例说明如何在Spring中注入一个Java Collection 如何向Spring Bean中注入一个JavautilProperties 请解释Spring Bean的自动装配 请解释自动装配模式的区别 如何开启基于注解的自动装配 请举例解释Required ...
//创建spring的ApplicationContext ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); //输出spring容器 System.out.println(ctx); //打印加载的bean名称 ...
Bean生命周期 3 Java反射 4 beanfactory和applicationcontext 5 类装载器ClassLoader 6 Spring aop 原理 6 Aop代理 7 Spring 事物 10 数据库锁 12 ThreadLocal 13 Spring TaskExecutor线程池 16
在构造过程中,SpringBusFactory会搜索META-INF/cxf(包含在 CXF 的jar中)下的所有bean配置文件。 * 根据这些配置文件构建一个ApplicationContext。开发者也可以提供自己的配置文件来定制Bus。
support模块和SpEL(Spring Expression Language,Spring表达式语言)模块组成,具体介绍如下: Beans模块:提供了BeanFactory,是工厂模式的经典实现,Spring将管理对象称为Bean。 Core核心模块:提供了Spring框架...
12.2.2. 在Spring的application context中创建 SessionFactory 12.2.3. HibernateTemplate 12.2.4. 不使用回调的基于Spring的DAO实现 12.2.5. 基于Hibernate3的原生API实现DAO 12.2.6. 编程式的事务划分 12.2.7. ...
15、如何向 Spring Bean 中注入一个 Java.util.Properties? 16、请解释 Spring Bean 的自动装配? 17、请解释自动装配模式的区别? 18、如何开启基于注解的自动装配? 19、请举例解释@Required 注解? 20、请举例...
spring.jar是包含有完整发布的单个jar包,spring.jar中包含除了 spring-mock.jar里所包含的内容外其它所有jar包的内容,因为只有在开发环境下才会用到spring-mock.jar来进行辅助测试,正式应用系统中是用不得这些类...
可以找到使用Spring ApplicationContext特性时所需的全部类,JDNI 所需的全部类,instrumentation组件以及校验Validation 方面的相关类。 外部依赖spring-beans, (spring-aop)。 5. spring-context-support-4.1.1....
3.10. 以J2EE RAR文件的形式部署Spring ApplicationContext 3.11. 基于注解(Annotation-based)的配置 3.11.1. @Autowired 3.11.2. 基于注解的自动连接微调 3.11.3. CustomAutowireConfigurer 3.11.4. @...
15、如何向 Spring Bean 中注入一个 Java.util.Properties? 16、请解释 Spring Bean 的自动装配? 17、请解释自动装配模式的区别? 18、如何开启基于注解的自动装配? 19、请举例解释@Required 注解? 20、请举例...
ApplicationContext ApplicationContextAware ApplicationContextAwareProcessor ApplicationContextException ApplicationEvent ApplicationEventMulticaster ApplicationEventPublisher ...
3.5.2 ApplicationContext中Bean的生命周期 3.6 小结 第4章 在IoC容器中装配Bean 4.1 Spring配置概述 4.1.1 Spring容器高层视图 4.1.2 基于XML的配置 4.2 Bean基本配置 4.2.1 装配一个Bean 4.2.2 Bean的命名 4.3 ...