spring容器在web项目中的使用
第一:在jsp、servlet、action中使用
jsp中
BeanFactory beanFactory = (BeanFactory)pageContext.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
servlet中
WebApplicationContext appContext1 = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
WebApplicationContext appContext2 = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
action中一般是组件之间已连接好,直接使用注入的bean
在其它普通类中使用
先建一个servlet获得这个WebApplicationContext然后再使用
这个servlet在web.xml中一定要设置启动容器时候初始化
例如:
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>ParamConfigServlet</servlet-name>
<servlet-class>com.intohotel.webim.ParamConfigServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ParamConfigServlet</servlet-name>
<url-pattern>/servlet/ParamConfigServlet</url-pattern>
</servlet-mapping>
public class ParamConfigServlet extends HttpServlet {
public void init() throws ServletException {
WebApplicationContext appContext1 = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
WebApplicationContext appContext2 = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
TestSpringFactory.setAppContext1(appContext1);
TestSpringFactory.setAppContext1(appContext2);
}
//一定要在init方法中,其它方法要么就是初始化太早取不到值,要么就是太晚或者根本不会调用
}
public class TestSpringFactory {
public static WebApplicationContext appContext1 = null;
public static WebApplicationContext appContext2 = null;
public void testBean(){
MessageService messageService = (MessageService)TestSpringFactory.appContext1.getBean("messageService");
Message message = new Message();
message.setFromName("fruitking");
message.setMessage("XuGuo is a super man!");
message.setOperatedDate(new Date());
message.setToName("mary");
messageService.save(message);
}
public static WebApplicationContext getAppContext1() {
return appContext1;
}
public static void setAppContext1(WebApplicationContext appContext1) {
TestSpringFactory.appContext1 = appContext1;
}
public static WebApplicationContext getAppContext2() {
return appContext2;
}
public static void setAppContext2(WebApplicationContext appContext2) {
TestSpringFactory.appContext2 = appContext2;
}
}
第一:在jsp、servlet、action中使用
jsp中
BeanFactory beanFactory = (BeanFactory)pageContext.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
servlet中
WebApplicationContext appContext1 = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
WebApplicationContext appContext2 = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
action中一般是组件之间已连接好,直接使用注入的bean
在其它普通类中使用
先建一个servlet获得这个WebApplicationContext然后再使用
这个servlet在web.xml中一定要设置启动容器时候初始化
例如:
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>ParamConfigServlet</servlet-name>
<servlet-class>com.intohotel.webim.ParamConfigServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ParamConfigServlet</servlet-name>
<url-pattern>/servlet/ParamConfigServlet</url-pattern>
</servlet-mapping>
public class ParamConfigServlet extends HttpServlet {
public void init() throws ServletException {
WebApplicationContext appContext1 = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
WebApplicationContext appContext2 = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
TestSpringFactory.setAppContext1(appContext1);
TestSpringFactory.setAppContext1(appContext2);
}
//一定要在init方法中,其它方法要么就是初始化太早取不到值,要么就是太晚或者根本不会调用
}
public class TestSpringFactory {
public static WebApplicationContext appContext1 = null;
public static WebApplicationContext appContext2 = null;
public void testBean(){
MessageService messageService = (MessageService)TestSpringFactory.appContext1.getBean("messageService");
Message message = new Message();
message.setFromName("fruitking");
message.setMessage("XuGuo is a super man!");
message.setOperatedDate(new Date());
message.setToName("mary");
messageService.save(message);
}
public static WebApplicationContext getAppContext1() {
return appContext1;
}
public static void setAppContext1(WebApplicationContext appContext1) {
TestSpringFactory.appContext1 = appContext1;
}
public static WebApplicationContext getAppContext2() {
return appContext2;
}
public static void setAppContext2(WebApplicationContext appContext2) {
TestSpringFactory.appContext2 = appContext2;
}
}