项目中获取Spring的Bean的几种方式

项目中获取Spring的Bean的几种方式,第1张

但是,对于系统中非Spring框架管理的类,如果需要获取Spring管理的类,或者,程序中需要动态的根据Bean的id来获取Bean实例,不可能事先为该类提供所有需要的Bean属性的setter方法,在类似这样的情况下,获取Spring框架管理的类实例的方法有多种,现在简单总结如下:方法一:在初始化时保存ApplicationContext对象 代码:ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContextxml");acgetBean("beanId");说明:这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况。方法二:通过Spring提供的工具类获取ApplicationContext对象代码:import orgspringframeworkcontextApplicationContext;import orgspringframeworkwebcontextsupportWebApplicationContextUtils;ServletContext sc=loc_sessiongetServletContext();ApplicationContext ac1 = WebApplicationContextUtilsgetRequiredWebApplicationContext(sc)ApplicationContext ac2 = WebApplicationContextUtilsgetWebApplicationContext(sc)ac1getBean("beanId");ac2getBean("beanId");说明:这种方式适合于采用Spring框架的B/S系统,通过ServletContext对象获取ApplicationContext对象,然后在通过它获取需要的类实例。上面两个工具方式的区别是,前者在获取失败时抛出异常,后者返回null。方法三:继承自抽象类ApplicationObjectSupport说明:抽象类ApplicationObjectSupport提供getApplicationContext()方法,可以方便的获取到ApplicationContext。Spring初始化时,会通过该抽象类的setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入。方法四:继承自抽象类WebApplicationObjectSupport说明:类似上面方法,调用getWebApplicationContext()获取WebApplicationContext方法五:实现接口ApplicationContextAware说明:实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 对象。Spring初始化时,会通过该方法将ApplicationContext 对象注入。以上方法适合不同的情况,请根据具体情况选用相应的方法。

[引文]通常在struts2+spring的例子上,要想使用spring的Bean,可以在applicationContextxml加上如下配置<bean id="springBean" scope="prototype" class=""<property name="name" value="chen"/</bean<bean id="myAction" scope="prototype" class=""<property name="springBean" ref="springBean"/</bean如果是j2ee应用,启动web应用时将会自动加载ApplicationContext实例(Spring容器负责创建Bean实例)一旦struts2的myAction实例化,其中的SpringBean也会被自动注入进来,从而达到使用SpringBean的目的。[问题]但是仍有需要通过代码来调用SpringBean的情况:1)对于不是由spring创建管理的类,如在java 代码中直接使用new去创建一个对象,并且想在这个对象中使用SpringBean;因为这个对象并不是由Spring容器创建管理的类,所以即使它有setter方法,容器的springBean也不会被注入。2)动态更改springBean中的属性值,如在代码运行时,name值需要发生变动;3)对于一个独立的应用程序[解决]定义一个非Spring容器创建管理的类public class NonSpringClass implements ServletContextAware {private SpringBean springBean;//如果 testGetBean不是被Spring容器创建管理,即使它有setter方法,容器的springBean也不会被注入。public void setSpringBean(SpringBean springBean){thisspringBean=springBean;}//利用ApplicationContext 从spring容器中获得springBean;//Spring有两个核心接口BeanFactory和ApplicationContext,其中ApplicationContext是BeanFactory的子接口,//它们代表了Spring容器,Spring容器是产生Bean的工厂,用于管理容器中的Bean。public NonSpringClass (){//ApplicationContext acx = new ClassPathXmlApplicationContext("applicationContextxml");ApplicationContext acx = new FileSystemXmlApplicationContext("src/WEB-INF/applicationContextxml");springBean=(SpringBean)acxgetBean("springBean");}}调用这个类的代码:import comNonSpringClass;public class TestCode {private NonSpringClass nonSpringClass;//这样nonSpringClass里将包含Spring容器创建的springBean}}

不用配置xml,直接java代码实现,参考代码如下:public class GetApplicationContext {private static class ApplicationContextHolder {// 单例变量private static ApplicationContext AC = new FileSystemXmlApplicationContext("classpath:applicationContextxml");}// 私有化的构造方法,保证外部的类不能通过构造器来实例化。private GetApplicationContext() {}// 获取单例对象实例public static ApplicationContext getInstance() {if (ApplicationContextHolderAC == null) {ApplicationContextHolderAC = new FileSystemXmlApplicationContext("classpath:applicationContextxml");}return ApplicationContextHolderAC;}}获取所有spring自动装配的bean://获取spring装配的bean个数GetApplicationContextgetInstance()getBeanDefinitionNames()length;//逐个打印出spring自动装配的bean。根据我的测试,类名第一个字母小写即bean的名字for(int i=0;i<33;i++){Systemoutprintln( GetApplicationContextgetInstance()getBeanDefinitionNames()[i]);}然后通过下面的代码获取到spring注解装配的bean供自己使用:StorageReturnService ossService = (StorageReturnService) GetApplicationContextgetInstance()getBean("storageReturnServiceImpl");怎样获取 spring 已经注解的bean

spring在普通类中注入bean实例 或注入静态变量的bean Spring的注入有一个问题就是普通类没有办法获得Bean文件中的bean实例。这就是如果是在Web的Servlet环境中可以通过WebApplicationContextUtils,如果是普通类就不好处理了。

spring在普通类中注入bean实例 或注入静态变量的bean

Spring的注入有一个问题就是普通类没有办法获得Bean文件中的bean实例。这就是如果是在Web的Servlet环境中可以通过WebApplicationContextUtils,如果是普通类就不好处理了。这需

要一点设计的技巧。下面是一个静态类使用Bean文件中实例的例子

1、如果是在配置文件中配置来注入:

public class UserinfoUtil

{

private IUserInfo userInfo;

private static UserinfoUtil info;

public void setUserInfo(IUserInfo userInfo)

{

thisuserInfo = userInfo;

}

public void init()

{

info = this;

infouserInfo = thisuserInfo;

}

public static int addUserLoginCnt(String phonenumber)

{

return infouserInfoaddUserLoginCnt(phonenumber);

}

}

相应的bean的配置:

相应的Bean文件的配置

<bean id="userinfoUtil" class="comhuaweiaimiwebportalserviceUserinfoUtil" init-method="init">

<property name="userInfo" ref="userInfo"/>

</bean>

2、用注解的方式注入:

public class UserinfoUtil

{

@Autowired

private IUserInfo userInfo;

private static UserinfoUtil info;

public void setUserInfo(IUserInfo userInfo)

{

thisuserInfo = userInfo;

}

@PostConstruct

public void init()

{

info = this;

infouserInfo = thisuserInfo;

}

public static int addUserLoginCnt(String phonenumber)

{

return infouserInfoaddUserLoginCnt(phonenumber);

}

}

以上就是关于项目中获取Spring的Bean的几种方式全部的内容,包括:项目中获取Spring的Bean的几种方式、如何通过代码直接获得Spring容器中的Bean、怎样获取 spring 已经注解的bean等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9719745.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-01
下一篇2023-05-01

发表评论

登录后才能评论

评论列表(0条)

    保存