spring在WEB中的应用。

1:创建IOC容器。在WEB应用程序启动的时候就创建。利用到监听器。

ServletContextListener类的contextInitialized方法中

 1 package com.struts2.listeners;
 2 
 3 import javax.servlet.ServletContext;
 4 import javax.servlet.ServletContextEvent;
 5 import javax.servlet.ServletContextListener;
 6 
 7 import org.springframework.context.ApplicationContext;
 8 import org.springframework.context.support.ClassPathXmlApplicationContext;
 9 
10 public class SpringServletContextListener implements ServletContextListener{
11 
12     public void contextDestroyed(ServletContextEvent arg0) {
13         // TODO Auto-generated method stub
14         
15     }
16 
17     public void contextInitialized(ServletContextEvent arg0) {
18         //1:applicationContext.xml在web.xml中进行创建。然后利用ServletContext获取到。
19         ServletContext sc=arg0.getServletContext();
20         String config=sc.getInitParameter("configLocation");
21         //创建IOC容器
22         ApplicationContext act=new ClassPathXmlApplicationContext(config);
23         //把创建的IOC容器放到ServletContext(即application域)中
24         sc.setAttribute("ApplicationContext", act);
25     }
26 
27 }

在web.xml中创建监听器和applicationContext.xml

<context-param>
      <param-name>configLocation</param-name>
      <param-value>applicationContext.xml</param-value>
  </context-param>
  
  <listener>
    <listener-class>com.struts2.listeners.SpringServletContextListener</listener-class>
  </listener>

然后创建一个实体:Person

package com.struts2.entyties;

public class Person {
    
    private String username;
    
    public void setUsername(String username) {
        this.username = username;
    }
    
    public void hello(){
        System.out.println("My name is " + username);
    }
    
}

然后创建bean

    <bean id="person" class="com.struts2.entyties.Person">
            <property name="username" value="陆伟"></property>
        </bean>

然后写个servlet去使用:

package com.struts2.servlet;

import java.io.IOException;

import javax.servlet.ServletContext;
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 com.struts2.entyties.Person;

public class TestServlet extends HttpServlet{
    private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 从 application 域对象中得到 IOC 容器的引用
        ServletContext sc=getServletContext();
        ApplicationContext act=(ApplicationContext) sc.getAttribute("ApplicationContext");
        
        //2. 从 IOC 容器中得到需要的 bean
        Person person = act.getBean(Person.class);
        person.hello();
    }

}

在web.xml中加载servlet

<servlet>
    <description></description>
    <display-name>TestServlet</display-name>
    <servlet-name>TestServlet</servlet-name>
    <servlet-class>com.struts2.servlet.TestServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>TestServlet</servlet-name>
    <url-pattern>/TestServlet</url-pattern>
  </servlet-mapping>

然后写个页面进行访问:

<a href="TestServlet">TestServlet</a>

 

posted @ 2017-12-07 20:29  陆伟  阅读(222)  评论(0编辑  收藏  举报