第一步、导包(注意红线画的两个包,是一定要有的)
第二步, 创建普通的类(有属性有方法)
package cn.com.beans;
public class Person {
// 这个类存在的意义,在Spring的配置文件中被初始化,然后IOC容器调用,
private String name;
public void setName(String name) {
this.name = name;
}
public void hello(){
System.out.println("my name is:"+name);
}
}
第三步、创建applicationContext.xml文件,并且给Person里面的属性初始化赋值
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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean class="cn.com.beans.Person" id="person">
<property name="name" value="tjn"></property>
</bean>
</beans>
前面的都是我们学习Spring的时候都会的,关键是这里
在java web资源里面我们可以直接new ClassPathXmlContext();但web资源就不一样了
我们需要用到一个ServletContextListener接口的一个方法contextInitialized
在这个方法里面我们需要实现以下三个内容
1、获取资源文件的名称
2、创建ioc容器
3、把ioc容器放在上下文中
我们需要在web.xml文件中配置资源文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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_3_0.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 给Spring配置文件在web资源中调用的设置 -->
<context-param>
<!--这个是给配置文件的重命名 -->
<param-name>ApplicationIoc</param-name>
<!-- 配置文件的地址,位置 -->
<param-value>applicationContext.xml</param-value>
</context-param>
<!-- 当这个web项目被加载的时候就开始调用或者说是创建ioc容器 -->
<listener>
<listener-class>cn.com.ioc.ServletContextListenImpl</listener-class>
</listener>
</web-app>
现在新建一个类,继承ServletContextListener方法
为什么用ServletContextListener接口?
因为当一个WEB资源项目被加载的时候,就会调用ServletContextLitener接口
为什么用的是contextInitialized()方法?
contextInitialized是初始化方法,是实现这个接口的执行的第一个方法
为什么在方法里面这样写?
在ServletContextEvent
类中只定义了一个构造函数。Web容器在ServletContext
实例之后创建ServletContextEvent
的实例。
String ioc=oct.getInitParameter("ApplicationIoc");就是根据web.xml文件中的
<!-- 给Spring配置文件在web资源中调用的设置 -->
<context-param>
<!--这个是给配置文件的重命名 -->
<param-name>ApplicationIoc</param-name>
<!-- 配置文件的地址,位置 -->
<param-value>applicationContext.xml</param-value>
</context-param>
来的
package cn.com.ioc;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ServletContextListenImpl implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
//1、获取Spring配置文件的名称
ServletContext oct=arg0.getServletContext();
String ioc=oct.getInitParameter("ApplicationIoc");
//2.创建ioc容器
ApplicationContext app=new ClassPathXmlApplicationContext(ioc);
//3.把ioc容器放在ServletContext里面
oct.setAttribute("app", app);
}
}
然后新建一个servlet类
package cn.com.servlet;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import cn.com.beans.Person;
public class StrutsServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//从WEB资源里面获取ioc容器
ServletContext sc=getServletContext();
ApplicationContext ioc=(ApplicationContext) sc.getAttribute("app");
Person peo=ioc.getBean(Person.class);
peo.hello();
System.out.println("hhahah");
}
}
Index.jsp
<body>
<a href="/SpringStruts2/StrutsServlet">点我,点我,快点</a>
</body>
总结,最重要的就是把applicationContext.xml资源文件进行属性化,在ServletContextLIstener方法里面创建ioc容器,并且把ioc容器设置成了属性 ,在servlet里面获取