Web项目中获取SpringBean——在非Spring组件中获取SpringBean
2014-04-26 10:43
543 查看
自定义一个工具类,实现自ApplicationContextAware接口,接口的方法是setApplicationContext,我们实现它,并让其为我们服务,因为Spring在load自己的时候会将上下文环境填充进来。我们所要做的就是将得到的ApplicationContext保存下来用。
上文的类就是我们要用的,而其中的setApplicationContext是接口中需要实现的,Spring会自动进行填充。我们在Spring的配置文件中注册一下:
这样就可以了,Spring把我们需要的东西给我们了。
我们就可以在需要的地方这样做:
当然,前提是你需要让Spring自动装配哦。
以下为方法二,使用了注解和静态化的方式来产生SpringFactory对象
上文的方法有个麻烦的地方:需要配置。而Spring2.5及之后的版本实际上加入了注解的方式进行依赖项的注入,使用如下代码也许更好:
如果使用@Autowired注解自动装配的话,继承SpringBeanAutowiringSupport类是不能少的。当然,使用@Component等注解也是可以的。使用注解的话配置就需要改动了,不过因为我们为支持Spring注解的配置是可以多用的,所以还好。如下:
该配置即可让org.ahe下所有包(您依然可以通过子标签的正则表达式匹配来进行更多设置)下的注解起作用。
package org.coderecord.ccms.web.action.util; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /** * Spring IOC上下文工具类 * * @author Ryan Shaw * */ public class SpringUtil implements ApplicationContextAware { /** * 当前IOC */ private static ApplicationContext applicationContext; /** * 设置当前上下文环境,此方法由spring自动装配 */ @Override public void setApplicationContext(ApplicationContext arg0) throws BeansException { applicationContext = arg0; } /** * 从当前IOC获取bean * * @param id * bean的id * @return */ public static Object getObject(String id) { Object object = null; object = applicationContext.getBean(id); return object; } }
上文的类就是我们要用的,而其中的setApplicationContext是接口中需要实现的,Spring会自动进行填充。我们在Spring的配置文件中注册一下:
<bean id="springUtil" class="org.coderecord.ccms.web.action.util.SpringUtil" />
这样就可以了,Spring把我们需要的东西给我们了。
我们就可以在需要的地方这样做:
YouClass obj = (YouClass)SpringUtil.getObject("beanid");
当然,前提是你需要让Spring自动装配哦。
以下为方法二,使用了注解和静态化的方式来产生SpringFactory对象
上文的方法有个麻烦的地方:需要配置。而Spring2.5及之后的版本实际上加入了注解的方式进行依赖项的注入,使用如下代码也许更好:
package org.ahe.util; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.context.support.SpringBeanAutowiringSupport; public class SpringWiredBean extends SpringBeanAutowiringSupport { /** * 自动装配注解会让Spring通过类型匹配为beanFactory注入示例 */ @Autowired private BeanFactory beanFactory; private SpringWiredBean() { } private static SpringWiredBean instance; static { // 静态块,初始化实例 instance = new SpringWiredBean(); } /** * 实例方法 * 使用的时候先通过getInstance方法获取实例 * * @param beanId * @return */ public Object getBeanById(String beanId) { return beanFactory.getBean(beanId); } public static SpringWiredBean getInstance() { return instance; } }
如果使用@Autowired注解自动装配的话,继承SpringBeanAutowiringSupport类是不能少的。当然,使用@Component等注解也是可以的。使用注解的话配置就需要改动了,不过因为我们为支持Spring注解的配置是可以多用的,所以还好。如下:
<context:component-scan base-package="org.ahe"></context:component-scan>
该配置即可让org.ahe下所有包(您依然可以通过子标签的正则表达式匹配来进行更多设置)下的注解起作用。
相关文章推荐
- Web项目中获取SpringBean——在非Spring组件中获取SpringBean
- Web项目中获取SpringBean——在非Spring组件中获取SpringBean
- Web项目中获取SpringBean——在非Spring组件中获取SpringBean
- Web项目中获取SpringBean——在非Spring组件中获取SpringBean
- Web项目中获取SpringBean——在非Spring组件中获取SpringBean
- Web项目中获取SpringBean——在非Spring组件中获取SpringBean
- tomcat中运行正常的web项目部署至websphere后,spring无法获取bean
- web项目中获取spring的bean对象
- web项目中获取spring的bean对象
- 在Bean中获取Spring容器中的各种组件
- web.xml中配置spring的几种方式 以及 Spring获取Bean的几种方式
- 传统Java Web非Spring Boot项目从Spring Cloud Eureka中获取服务
- 如果spring--bean是单例,那么web项目一启动就会加载到内存,如果bean是多例,项目会在刚刚使用的时候,就是走到这个请求地址的时候,类才会加载---spring 默认为单例
- spring心得2--bean的生命周期@Spring监听器的作用@Spring初始化容器案例分析@web项目使用
- JavaWeb项目获取Spring自动装配的Bean对象
- Web容器中获取spring容器的bean对象
- spring心得2--bean的生命周期@Spring监听器的作用@Spring初始化容器案例分析@web项目使用
- 在web应用中获取spring的context,并打印出context中所有bean的id
- Spring项目获取注入bean集合
- IntelliJIdea 2016.2 使用 tomcat 8.5 调试spring的web项目时,bean被实例化两次导致timer和thread被启动了两遍的问题的解决