幫助類

/**
 * @program: Maven
 * @description: spring整合進tomcat
 * @author: hw
 * @create: 2018-12-04 15:18
 **/
public class SpringWebUtil {

    private static final String SPRING_CONTEXT_KEY = "spring_context_key";

    /**
     * spring放入tomcat容器中
     *
     * @Description:已經建模好的spring的上下文
     * @Param: [context, servletContext]
     * @return: void
     * @Author: hw
     * @Date: 2018/12/4
     */
    public static void setApplicationContext(ApplicationContext context, ServletContext servletContext) {
        servletContext.setAttribute(SPRING_CONTEXT_KEY, context);
    }

    /**
     * @Description: 獲取
     * @Param: [servletContext]
     * @return: org.springframework.context.ApplicationContext
     * @Author: hw
     * @Date: 2018/12/4
     */
    public static ApplicationContext getApplicationContext(ServletContext servletContext) {
        return (ApplicationContext) servletContext.getAttribute(SPRING_CONTEXT_KEY);
    }

監聽類

/**
 * @program: Maven
 * @description: ..
 * @author: hw
 * @create: 2018-12-04 15:47
 **/
public class SpringLoaderListener implements ServletContextListener {
    //預設配置名
    private String configLocation="xxx";

    /**
     * @Description: 啟動執行
     * @Param: [servletContextEvent]
     * @return: void
     * @Author: hw
     * @Date: 2018/12/4
     */
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        //獲取tomcat上下文
        ServletContext servletcont = servletContextEvent.getServletContext();

        //預設名
        String spingXml="spring-context.xml";
        String Path = servletcont.getInitParameter(configLocation);
        //如果沒配置則用預設的
        if(!(null==Path||"".equals(Path))){
            spingXml=Path;
        }
        System.out.println(spingXml);
        //建模
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
        //放入tomcat
        SpringWebUtil.setApplicationContext(context, servletcont);
    }

    /**
     * @Description: 關閉執行
     * @Param: [servletContextEvent]
     * @return: void
     * @Author: hw
     * @Date: 2018/12/4
     */
    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }

web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    version="4.0">
    <display-name>Archetype Created Web Application</display-name>
    <listener>
        <listener-class>com.hw.one.util.SpringLoaderListener</listener-class>
    </listener>
    <!--設定spring的配置檔名-->
    <context-param>
        <param-name>xxx</param-name>
        <param-value>spring-context.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>UserServlet</servlet-name>
        <servlet-class>com.hw.one.action.UserServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>UserServlet</servlet-name>
        <url-pattern>/userServlet</url-pattern>
    </servlet-mapping>
</web-app>

測試類

public class UserServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("xxx");
        //獲取spring的上下文
        ApplicationContext applicationContext = SpringWebUtil.getApplicationContext(req.getServletContext());
        StudentAction studentAction = (StudentAction) applicationContext.getBean("studentAction");
        System.out.println(studentAction.toString());
    }
    
}

 Spring的配置檔案

    <!--ioc注入的型別和方式-->
    <bean class="com.hw.one.action.StudentAction" id="studentAction">
        <!--set方法注入-->
        <property name="sid" value="123"></property>
        <property name="sname" value="蓋倫"></property>
        <!--集合-->
        <property name="list">
        <list>
        <value>吃</value>
        <value>喝</value>
        <value>玩</value>
        <value>樂</value>
        </list>
        </property>

        <!--構造器注入-->
        <constructor-arg name="sid" value="123"></constructor-arg>
        <constructor-arg name="sname" value="蓋倫"></constructor-arg>
        <!--<property name="list">-->
        <!--<list>-->
        <!--<value>吃</value>-->
        <!--<value>喝</value>-->
        <!--<value>玩</value>-->
        <!--<value>樂</value>-->
        <!--</list>-->
        <!--</property>-->
    </bean>