applicationContext是ApplicaionContext实例
如何配置可以在服务器开始时就自动加载spring配置文件,并实例里面的bean
而不需要 ApplicionContext applicationContext = new ClasspathApplicionContext(“..”)
applicationContext.getBean("..");
如何配置可以在服务器开始时就自动加载spring配置文件,并实例里面的bean
而不需要 ApplicionContext applicationContext = new ClasspathApplicionContext(“..”)
applicationContext.getBean("..");
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
不知道是不是你说的那样
package com.bean;public class User {
private String name;
private String pass;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public void print(){
System.out.print("av");
}
}第二个Bean public class LoginAction extends Action {
private User user;
public void setUser(User user) {
this.user = user;
}
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
user.print();
return null;
}
}
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="user"
class="com.bean.User">
<property name="name"
value="123">
</property>
<property name="pass" value="123">
</property>
</bean>
<bean id ="loginAction" class="com.yourcompany.struts.action.LoginAction">
<property name="user" ref="user"></property>
</bean>
</beans>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
protected static Object getBean(ServletContext servletContext,String beanName){
WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(servletContext);
return wac.getBean(beanName);
}
servletContext这个你因该在你的web项目中能得到的 这样就很方便的直接返回你要的bean了 HOHO~