private static ApplicationContext applicationContext;/ 实现ApplicationContextAware接口的回调方法,设置上下文环境/public void setApplicationContext(ApplicationContext applicationContext){
SpringContextUtilapplicationContext = applicationContext;}
public static ApplicationContext getApplicationContext(){
return applicationContext;}/ 获取对象@returnObject 一个以所给名字注册的bean的实例 (service注解方式,自动生成以首字母小写的类名为bean name)/public static Object getBean(String name) throws BeansException
在配置文件中加入:
Xml代码
<!-- 用于持有ApplicationContext,可以使用SpringContextHoldergetBean('xxxx')的静态方法得到spring bean对象 -->
<bean class="comxxxxxSpringContextHolder" lazy-init="false" />
Java代码
import orgspringframeworkcontextApplicationContext;
import orgspringframeworkcontextApplicationContextAware;
/
以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext
/
public class SpringContextHolder implements ApplicationContextAware {
private static ApplicationContext applicationContext;
/
实现ApplicationContextAware接口的context注入函数, 将其存入静态变量
/
public void setApplicationContext(ApplicationContext applicationContext) {
SpringContextHolderapplicationContext = applicationContext; // NOSONAR
}
/
取得存储在静态变量中的ApplicationContext
/
public static ApplicationContext getApplicationContext() {
checkApplicationContext();
return applicationContext;
}
/
从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型
/
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) {
checkApplicationContext();
return (T) applicationContextgetBean(name);
}
/
从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型
/
@SuppressWarnings("unchecked")
public static <T> T getBean(Class<T> clazz) {
checkApplicationContext();
return (T) applicationContextgetBeansOfType(clazz);
}
/
清除applicationContext静态变量
/
public static void cleanApplicationContext() {
applicationContext = null;
}
private static void checkApplicationContext() {
if (applicationContext == null) {
throw new IllegalStateException("applicaitonContext未注入,请在applicationContextxml中定义SpringContextHolder");
}
}
}
常用的5种获取spring 中bean的方式总结:
方法一:在初始化时保存ApplicationContext对象
代码:
ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContextxml");
acgetBean("beanId");
说明:这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况。
方法二:通过Spring提供的工具类获取ApplicationContext对象
代码:
import orgspringframeworkwebcontextsupportWebApplicationContextUtils;
ApplicationContext ac1 = WebApplicationContextUtilsgetRequiredWebApplicationContext(ServletContext sc);
ApplicationContext ac2 = WebApplicationContextUtilsgetWebApplicationContext(ServletContext sc);
ac1getBean("beanId");
ac2getBean("beanId");
说明:
spring的上下文可以同时获取两个bean。spring项目,在相同的上下文中创建两个springbean。需要在单独的Configuration文件和单独的访问中创建。bean对象的创建,是靠注解,想要在代码里面,不是使用注解获取到bean对象,而是在上下文对象里面获取到bean对象,项目起动,就扫描注解,让被注解的类,创建bean对象,放到spring容器里面,从容器里面获取到对象。
最近在项目的时候碰到pring事务不起作用的情况,后来解决了,这里我汇总下:1、首先使用如下代码确认你的bean是代理对象吗?必须是Spring定义(通过XML或注解定义都可以)的Bean才接受事务。直接new出来的对象添加事务是不起作用的。可以通过以下方式判断是否是代理对象:AopUtilsisAopProxy(Objectobject)AopUtilsisCglibProxy(Objectobject)//cglibAopUtilsisJdkDynamicProxy(Objectobject)//jdk动态代理2、入口函数必须是public,否则事务不起作用。这一点由Spring的AOP特性决定的。3、切入点配置错误。4、如果你使用了springmvc,可能是context:component-scan重复扫描引起的:5、如使用mysql且引擎是MyISAM造成的(因为不支持事务),改成InnoDB即可。
GamePackService gamePackService;
@Override
public void init() throws ServletException
{
superinit();
WebApplicationContext wac = WebApplicationContextUtils
getRequiredWebApplicationContext(getServletContext());
gamePackService = (GamePackService) wacgetBean("gamePackService");
}、
在servlet init 方法中加入
ModelAndView mav=new ModelAndView()
mavaddObject("bean",你的bean);
页面返回的各个属性值${beanxxx}
1、servlet方式加载时,
webxml
Xml代码
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>orgspringframeworkwebservletDispatcherServlet</servlet-class>
<init-param>
<param-name>contExtConfigLocation</param-name>
<param-value>classpath:/springMVCxml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
spring容器放在ServletContext中的key是orgspringframeworkwebservletFrameworkServletCONTEXTspringMVC
注意后面的springMVC,是你的servlet-name配置的值,注意适时修改。
Java代码
ServletContext sc=略
WebApplicationContext attr = (WebApplicationContext)scgetAttribute("orgspringframeworkwebservletFrameworkServletCONTEXTspringMVC");
2、listener方式加载时:
webxml
Xml代码
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext</param-value>
</context-param>
<listener>
<listener-class>orgspringframeworkwebcontextContextLoaderListener</listener-class>
</listener>
jsp/servlet可以这样取得
Java代码
ServletContext context = getServletContext();
WebApplicationContext applicationContext = WebApplicationContextUtils getWebApplicationContext(context);
3、通用的方法来了,神器啊,前的 1、2两种方法并不通用,可以抛弃了。
在配置文件中加入:
Xml代码
<!-- 用于持有ApplicationContext,可以使用SpringContextHoldergetBean('xxxx')的静态方法得到spring bean对象 -->
<bean class="comxxxxxSpringContextHolder" lazy-init="false" />
Java代码
import orgspringframeworkcontextApplicationContext;
import orgspringframeworkcontextApplicationContextAware;
/
以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext
/
public class SpringContextHolder implements ApplicationContextAware {
private static ApplicationContext applicationContext;
/
实现ApplicationContextAware接口的context注入函数, 将其存入静态变量
/
public void setApplicationContext(ApplicationContext applicationContext) {
SpringContextHolderapplicationContext = applicationContext; // NOSONAR
}
/
取得存储在静态变量中的ApplicationContext
/
public static ApplicationContext getApplicationContext() {
checkApplicationContext();
return applicationContext;
}
/
从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型
/
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) {
checkApplicationContext();
return (T) applicationContextgetBean(name);
}
/
从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型
/
@SuppressWarnings("unchecked")
public static <T> T getBean(Class<T> clazz) {
checkApplicationContext();
return (T) applicationContextgetBeansOfType(clazz);
}
/
清除applicationContext静态变量
/
public static void cleanApplicationContext() {
applicationContext = null;
}
private static void checkApplicationContext() {
if (applicationContext == null) {
throw new IllegalStateException("applicaitonContext未注入,请在applicationContextxml中定义SpringContextHolder");
}
}
}
以上就是关于自定义标签中怎样获取注解形式的spring bean全部的内容,包括:自定义标签中怎样获取注解形式的spring bean、如何取得Spring管理的bean、spring 怎么获取bean等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)