Spring在web应用中使用?
1.jar的不同需要把jar拷贝到工程中
Spring-web-4.1.2.RELEASE.jar
Spring-webmvc-4.1.2.RELEASE.jar
2.Spring的配置文件一样
3.如何创建IOC容器?
A:非web项目在main方法中直接创建ApllicationContext app= new ClassPathXmlApplicationContext(IOC容器的地址);
B:web应用在什么时候创建IOC的实例?在web应用被服务器加载时候就创建IOC容器
在ServletContextListener( interface(接口)ServletContextListener)监听器的方法中创建IOC容器(利用接口中的方法public void contextInitialized ( ServletContextEvent sce );)
C:在web应用中其他组件如何访问IOC容器?在方法中创建IOC容器以后把其IOC应用的实例放在application域属性中(其他组件可以访问)
代码:
/**
* @see ServletContextListener#contextInitialized(ServletContextEvent)
*/
public void contextInitialized(ServletContextEvent servletContextEvent) {
//获取Servlet的配置文件的名称
ServletContext sc= servletContextEvent.getServletContext();
String cfg=sc.getInitParameter("configLocation");
System.out.println(cfg);
//创建IOC容器
ApplicationContext app= new ClassPathXmlApplicationContext(cfg);
//把IOC容器放在ServletContext的一个属性中
sc.setAttribute("ApplicationContext", app);
}
D:spring配置文件的名字和位置应该也是可配置的!将其配置到当前web应用的初始化参数中较为合适
在web.xml中配置
<!-- spring配置文件的名称和位置 -->
<context-param>
<param-name>configLocation</param-name>(对应的C步骤的获取全局初始化参数)
<param-value>applicationContext.xml</param-value>
</context-param>
<!-- 启动ServletContextListener -->
<listener>
<listener-class>com.spring.listeners.SpringServletContextListener</listener-class>
</listener>
E:创建一个Servlet继承HttpServlet得到IOC容器然后在获取bean
/**
* Servlet implementation class TestServlet
*/
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//从域中得到引用
ServletContext sc =getServletContext();
ApplicationContext app=(ApplicationContext) sc.getAttribute("ApplicationContext");
//得到容器的bean
Person person=app.getBean(Person.class);
person.hello();
}
}
最后启动Servlet的服务器Tomcat
编写一个index.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="<%=request.getContextPath() %>/TestServlet">TestServlet</a>
</body>
</html>
在控制台输出Person类的方法
public class Person {
private String oldname;
public String getOldname() {
return oldname;
}
public void setOldname(String oldname) {
this.oldname = oldname;
}
//写构造方法
public void hello(){
System.out.println("My name is"+oldname);
}
}
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.xsd">
<!-- 配置Bean -->
<bean id="person" class="com.spring.beans.Person">
<property name="oldname" value="Tom"></property>
</bean>
</beans>
总结:
Spring框架提供了一个监听器----
org.springframework.web.context.ContextLoaderListener在web.xml容器中配置监听器
<!-- 配置spring的名称和位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 启动ServletContextListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>