Spring web应用下方便的获得bean(转)
2013-05-24 15:40
513 查看
如果我们的系统不是分布式的(在分布式里,我一般自己加载spring的配置文件),不是一般的application,通过自己加载Spring的配置文件的方式。而是一般的web应用,我们通过在web.xml里配置spring的配置文件。我们怎么方便的得到一个Bean的实例呢?当然,web应用启动后,它已经创建好一个WebApplicationContext(这个是接口,其实也是ApplicationContext类型的,因为WebApplicationContext继承自ApplictionContext这个接口)类型的实例对象,通过org.springframework.web.context.support.WebApplicationContextUtils里的
getWebApplicationContext(ServletContext sc)可以得到这个对象的引用(这个就像我们一般的java application下得到ApplicationContext类型的引用一样),我们就可以通过它的getBean方法得到我们的bean实例了。但是这里有个问题getWebApplicationContext(ServletContext sc)这个方法的参数ServletContext代表的是你web应用的环境,也就是说,也就是说web应用环境下特有的。这个时候如果你想得到一个bean的话,必须要有这个ServletContext对象存在,如果你每个类里都写一个方法来接受ServletContext对象,从而得到WebApplicationContext类型实例的引用,之后再得到bean,进行你要的操作,这个是不是很麻烦?这个不是要写很多代码么?我觉得可以把获得bean的这个操作的功能代码放在一个Servlet里,让这个Servlet在web应用启动的时候加载,我们之后把这个Servlet当作普通类使用,调用里面的getBean方法就可以了。这个servlet的代码如下:
package com.nstc.cti.event;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class GetBean extends HttpServlet {
private static WebApplicationContext context;
public void init() throws ServletException {
context = WebApplicationContextUtils.getWebApplicationContext(this
.getServletContext());
}
public static Object getBean(String id) {
Object bean = context.getBean(id);
return bean;
}
}
web.xml配置里:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- Spring的配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beans.xml</param-value>
</context-param>
<servlet>
<servlet-name>springInitServlet</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>GetBeanServlet</servlet-name>
<servlet-class>com.nstc.cti.event.GetBean</servlet-class>
<load-on-startup>3</load-on-startup>
</servlet>
<!--Spring配置结束 -->
<servlet-mapping>
<servlet-name>GetBeanServlet</servlet-name>
<url-pattern>/servlet/GetBeanServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
红色也可以为:
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
使用示例:
假使我有一个Person的类,属性有name和age,在spring配置文件里配置的id为“person”
你在你的servlet或者jsp想得到这个实例的时候,直接这么做就可以了:
Person person=(Person)GetBeanServlet.getBean("person");
getWebApplicationContext(ServletContext sc)可以得到这个对象的引用(这个就像我们一般的java application下得到ApplicationContext类型的引用一样),我们就可以通过它的getBean方法得到我们的bean实例了。但是这里有个问题getWebApplicationContext(ServletContext sc)这个方法的参数ServletContext代表的是你web应用的环境,也就是说,也就是说web应用环境下特有的。这个时候如果你想得到一个bean的话,必须要有这个ServletContext对象存在,如果你每个类里都写一个方法来接受ServletContext对象,从而得到WebApplicationContext类型实例的引用,之后再得到bean,进行你要的操作,这个是不是很麻烦?这个不是要写很多代码么?我觉得可以把获得bean的这个操作的功能代码放在一个Servlet里,让这个Servlet在web应用启动的时候加载,我们之后把这个Servlet当作普通类使用,调用里面的getBean方法就可以了。这个servlet的代码如下:
package com.nstc.cti.event;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class GetBean extends HttpServlet {
private static WebApplicationContext context;
public void init() throws ServletException {
context = WebApplicationContextUtils.getWebApplicationContext(this
.getServletContext());
}
public static Object getBean(String id) {
Object bean = context.getBean(id);
return bean;
}
}
web.xml配置里:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- Spring的配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beans.xml</param-value>
</context-param>
<servlet>
<servlet-name>springInitServlet</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>GetBeanServlet</servlet-name>
<servlet-class>com.nstc.cti.event.GetBean</servlet-class>
<load-on-startup>3</load-on-startup>
</servlet>
<!--Spring配置结束 -->
<servlet-mapping>
<servlet-name>GetBeanServlet</servlet-name>
<url-pattern>/servlet/GetBeanServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
红色也可以为:
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
使用示例:
假使我有一个Person的类,属性有name和age,在spring配置文件里配置的id为“person”
你在你的servlet或者jsp想得到这个实例的时候,直接这么做就可以了:
Person person=(Person)GetBeanServlet.getBean("person");
相关文章推荐
- Spring web应用下怎么方便的获得bean?
- Spring在web应用中获得Bean的方法
- Spring在web应用中获得Bean的方法
- Spring在web应用中获得Bean的方法 实现getBean方法
- Spring在web应用中获得Bean的方法 实现getBean方法
- 当web中应用spring框架启动后,获得spring初始化的bean和ServletContext
- web后台守护线程如何获得spring容器中的bean
- web应用中spring 获取bean的方式
- web后台守护线程如何获得spring容器中的bean
- Spring web应用下怎么方便的获得bean?
- 在web应用中获取spring的context,并打印出context中所有bean的id
- 【Spring】初始化Spring IoC容器(非Web应用),并获取Bean
- Spring boot web 应用 Bean介绍 : AutoConfigurationPackages
- web后台守护线程如何获得spring容器中的bean
- 实例讲解-整合iBATIS和Spring在WEB开发中的应用
- spring为web应用提供了几个过滤器和监听器
- web启动,quartz 关联的servlet 启动,得到Spring的bean ,servletContext 获取数据源
- 【Spring5】使用 Spring Webflux 开发 Reactive 应用
- struts+spring+hibernate的web应用 架构搭建
- web 工程中利用Spring的 ApplicationContextAware接口自动注入bean