温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

模拟Spring如何在WEB中运行

发布时间:2020-07-23 19:42:55 来源:网络 阅读:556 作者:一笑江湖了 栏目:开发技术

Spring在web中配置和普通的Java程序中有所区别,总结一下主要表现在以下几个方面:

①jar包不同,需要引入两个web的jar包

②需要考虑IOC容器创建的时间

非 WEB 应用在 main 方法中直接创建

在web应用中为了保证spring的运行,所以必须在程序刚在容器中启动时就必须创建IOC容器,这样的时间人为不好把握,我们可以通过Listener来监听程序的运行,保证在刚开始时就创建,具体采用的是

创建一个实现ServletContextListener接口的类,并在重写的contextInitialized(ServletContextEvent sce)方法中创建IOC容器,创建了如何让其他的组件来访问可以通过把IOC放入servletContext的一个域属性中,这样其他组件就可以直接通过servlet.getAttribute()来取得。具体代码如下:

package com.marry.spring.struts2.listeners; /**
 * Created by Administrator on 2016/8/26.
 */

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener()
public class SpringServletContextListener implements ServletContextListener {

    // Public constructor is required by servlet spec
    public SpringServletContextListener() {
    }

    // -------------------------------------------------------
    // ServletContextListener implementation
    // -------------------------------------------------------
    public void contextInitialized(ServletContextEvent sce) {

//        1获取spring配置文件的名称
        ServletContext servletContext = sce.getServletContext();
        String config = servletContext.getInitParameter("ConfigLocation");
//1.创建IOC容器
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
//        2.将IOC容器放入servletContext一个属性中
        servletContext.setAttribute("applicationContext",ctx);
    }

    public void contextDestroyed(ServletContextEvent sce) {
      /* This method is invoked when the Servlet Context
         (the Web application) is undeployed or
         Application Server shuts down.
      */
    }
}

相应的需要在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"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

    <!--配置spring配置文件的名称和位置-->
    <context-param>
        <param-name>ConfigLocation</param-name>
        <param-value>applicationContext.xml</param-value>
    </context-param>

    <!--启动IOC容器的servletContextListener-->
    <listener>
        <listener-class>com.marry.spring.struts2.listeners.SpringServletContextListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>TestServlet</servlet-name>
        <servlet-class>com.marry.spring.struts2.servlets.testServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>TestServlet</servlet-name>
        <url-pattern>/TestServlet</url-pattern>
    </servlet-mapping>
</web-app>

为检验成果,我们可以创建一个类并配置bean

package com.marry.spring.struts2.beans;

/**
 * Created by Administrator on 2016/8/26.
 */
public class Person {
    private String username;

    public void setUsername(String username) {
        this.username = username;
    }

    public void hello(){
        System.out.println("my name is:"+username);
    }
}
<?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 id="person" class="com.marry.spring.struts2.beans.Person">
        <property name="username" value="malin"/>
    </bean>
</beans>

创建一个servlet进行测试

package com.marry.spring.struts2.servlets;

import com.marry.spring.struts2.beans.Person;
import org.springframework.context.ApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Administrator on 2016/8/26.
 */
@WebServlet(name = "testServlet")
public class testServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//            1.从application域对象中获取IOC容器
        ServletContext servletContext=getServletContext();
        ApplicationContext ctx= (ApplicationContext) servletContext.getAttribute("applicationContext");
//        2.从IOC容器中获取需要的bean
        Person person= (Person) ctx.getBean("person");
        person.hello();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
             doPost(request,response);
    }
}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI