spring bean 不使用注入的方式获取的两种方式
2014-11-14 14:32
597 查看
非注入方式取得spring注入bean的util类实现
第一种,我用在webservice接口中。
实现ApplicationContextAware的Bean,在Bean被初始后,将会被注入ApplicationContext的实例。
applicationContext.xml
这样在spring配置文件加载时会自动执行ApplicationContextAware的setApplicationContext方法,将applicationContext对象传递给我们的Util类。
SpringBeanUtil.getBean("myBean");
在某些时候我们不希望通过注入也能取得某些bean时有用。
PS:
Spring 中提供一些Aware相关接口,像是BeanFactoryAware、 ApplicationContextAware、ResourceLoaderAware、ServletContextAware等等,这些 Aware接口的Bean在被初始之后,可以取得一些相对应的资源,例如实现BeanFactoryAware的Bean在初始后,Spring容器将会注入BeanFactory的实例,而实现ApplicationContextAware的Bean,在Bean被初始后,将会被注入 ApplicationContext的实例等等。
================================
第二种,就是传统的Application程序使用的了。
非注入方式取得spring注入bean的util类实现
第一种,我用在webservice接口中。
import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /** * SpringBean的工具类<br> * 非注入方式取得spring注入bean的util类实现 * @author langkai * */ public final class SpringBeanUtil implements ApplicationContextAware { private static ApplicationContext ctx; /** * 通过spring配置文件中配置的bean id取得bean对象 * @param id spring bean ID值 * @return spring bean对象 */ public static Object getBean(String id) { if (ctx == null) { throw new NullPointerException("ApplicationContext is null"); } return ctx.getBean(id); } @Override public void setApplicationContext(ApplicationContext applicationcontext) throws BeansException { ctx = applicationcontext; } }
实现ApplicationContextAware的Bean,在Bean被初始后,将会被注入ApplicationContext的实例。
applicationContext.xml
<bean class="*.SpringBeanUtil"/>
这样在spring配置文件加载时会自动执行ApplicationContextAware的setApplicationContext方法,将applicationContext对象传递给我们的Util类。
SpringBeanUtil.getBean("myBean");
在某些时候我们不希望通过注入也能取得某些bean时有用。
PS:
Spring 中提供一些Aware相关接口,像是BeanFactoryAware、 ApplicationContextAware、ResourceLoaderAware、ServletContextAware等等,这些 Aware接口的Bean在被初始之后,可以取得一些相对应的资源,例如实现BeanFactoryAware的Bean在初始后,Spring容器将会注入BeanFactory的实例,而实现ApplicationContextAware的Bean,在Bean被初始后,将会被注入 ApplicationContext的实例等等。
================================
第二种,就是传统的Application程序使用的了。
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("springConfig_bus.xml"); IMsgBusService client = (IMsgBusService) context.getBean("client");
相关文章推荐
- Spring(2)与接口配合使用和获取Bean容器的两种方式(BeanFactory 、ApplicationContext)
- SpringJUnit4加载类目录下(src)和WEF-INF目录下的配置文件二--获取注入的bean的二种方式
- spring 学习之 bean 的注入方式 property和constructor-arg的使用方式
- 第一天看Spring。。。汗。。。获取bean的两种方式
- Spring-2:bean的两种依赖注入方式
- spring中获取bean的两种方式
- 使用web.xml方式加载Spring时,获取Spring context的两种方式
- Spring实现Aware接口自定义获取bean的两种方式
- spring_01<bean>的两种注入方式
- spring工程中,不通过注入的方式获取bean
- action控制层,使用spring注解方式注入业务bean报空指向错误
- spring不通过注入的方式获取对象(BeanFactoryAware \ApplicationContextAware )
- 3.Spring Boot + Security初步使用(覆盖配置的两种方式 配置类和XML注入以自定义页面为例)
- spring注入bean两种方式(属性注入,构造器注入)
- spring中BeanNameAutoProxyCreator和AnnotationAwareAspectJAutoProxyCreator两种AOP代理方式使用总结
- Spring中使用两种Aware接口自定义获取bean
- 【spring set注入 注入集合】 使用set注入的方式注入List集合和Map集合/将一个bean注入另一个Bean
- Spring中使用两种Aware接口自定义获取bean
- spring4笔记----使用装配注入合作者Bean的三种方式
- 将spring管理的bean使用注解的方式注入到servlet中