entorno web integrado de primavera

1, capas de dao

package com.tuy.dao;

public interface UserDao {

    public void save();

}
package com.tuy.dao.impl;

import com.tuy.dao.UserDao;

public class UserDaoImpl implements UserDao {
    public void save() {
        System.out.println("save running....");
    }
}

2. Capa de servicio (capa empresarial)

package com.tuy.service;

public interface UserService {
    public void save();
}
package com.tuy.service.impl;

import com.tuy.dao.UserDao;
import com.tuy.service.UserService;

public class UserServiceImpl implements UserService {

    private UserDao userDao;
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void save() {
        userDao.save();
    }
}

3. La capa web

package com.tuy.web;

import com.tuy.service.UserService;
import com.tuy.service.impl.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

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;

//@WebServlet("UserServlet")
public class UserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext-trans.xml");
//        UserService userService= app.getBean(UserServiceImpl.class);
//        userService.save();

       // request.getServletContext();
        ServletContext servletContext = this.getServletContext();
       //  ApplicationContext app=(ApplicationContext) servletContext.getAttribute("app");
       // WebApplicationContextUtils.getWebApplicationContext(servletContext); //自己实现的
        WebApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext); //spring-web框架的
        UserService userService= app.getBean(UserServiceImpl.class);
        userService.save();

    }
}

//maven tomcat:run

4. Archivo de configuración en recursos

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">



    <!--配置Dao-->
    <bean id="userDao" class="com.tuy.dao.impl.UserDaoImpl"></bean>

    <!--配置service-->
    <bean id="userService" class="com.tuy.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"> </property>
    </bean>



</beans>

5. El archivo de configuración web.xml en WEB-INF

<?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">

<!-- 全局初始化参数   -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext-trans.xml</param-value>
    </context-param>

    <!--配置监听器-->
    <listener>
<!--        <listener-class>com.tuy.listener.ContextLoaderListener</listener-class>-->
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>MyServlet</servlet-name>
        <servlet-class>com.tuy.web.UserServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>MyServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
</web-app>

6 、 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.tuy</groupId>
    <artifactId>learn_spring_mvc</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <!-- 注意加 packaging 打包  -->

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>



    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>

        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.9</version>
        </dependency>

        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>

        <!--        spring集成junit需要的包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.0.3.RELEASE</version>
        </dependency>

        <!--用到事务的时候需要-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.0.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.13</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>

<!--        <dependency>-->
<!--            <groupId>org.apache.tomcat</groupId>-->
<!--            <artifactId>tomcat-servlet-api</artifactId>-->
<!--            <version>7.0.62</version>-->
<!--        </dependency>-->


    </dependencies>



    <build>
        <finalName>testProject</finalName>

        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>

                <!--可以在Setting-Livetemplate 把下面添加成模板-->
<!--                <plugin>-->

<!--                    &lt;!&ndash;jdk插件&ndash;&gt;-->
<!--                    <groupId>org.apache.maven.plugins</groupId>-->
<!--                    <artifactId>maven-compiler-plugin</artifactId>-->
<!--                    <version>3.1</version>-->
<!--                    <configuration>-->
<!--                        <source>1.8</source>-->
<!--                        <target>1.8</target>-->
<!--                        <encoding>utf-8</encoding>-->
<!--                    </configuration>-->
<!--                </plugin>-->
                <!--  要使用tomcat7,  运行的时候 tomcat7:run  -->
<!--                <plugin>-->
<!--                    <groupId>org.apache.tomcat.maven</groupId>-->
<!--                    <artifactId>tomcat7-maven-plugin</artifactId>-->
<!--                    <version>2.1</version>-->
<!--                    <configuration>-->
<!--                        <port>8082 </port>-->
<!--                    </configuration>-->
<!--                </plugin>-->

            </plugins>
        </pluginManagement>
    </build>




</project>

7. Inicie la depuración de maven tomcat: ejecute

 

// ----------------------------------------------

Oyente implementado por ti mismo

package com.tuy.listener;

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

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

//需要在web.xml里面配置监听器
public class
ContextLoaderListener implements ServletContextListener {

    //初始化  服务器一启动就执行
    public void contextInitialized(ServletContextEvent sce) {

        ServletContext servletContext = sce.getServletContext();

        //读取web.xml中的全局参数
        String contextConfigLocation= servletContext.getInitParameter("contextConfigLocation");

        ApplicationContext app=new ClassPathXmlApplicationContext(contextConfigLocation);
         //将spring上下文对象 存储最大的域ServletContext中

        servletContext.setAttribute("app",app);
        System.out.println("spring 容器创建完毕");
    }

    public void contextDestroyed(ServletContextEvent sce) {

    }

}
package com.tuy.listener;

import org.springframework.context.ApplicationContext;

import javax.servlet.ServletContext;

public class WebApplicationContextUtils {

    //获取存储的 spring容器上下文
    public static ApplicationContext getWebApplicationContext(ServletContext servletContext){
        return (ApplicationContext) servletContext.getAttribute("app");
    }

}

 

 

 

 

Supongo que te gusta

Origin blog.csdn.net/m0_37981386/article/details/111695981
Recomendado
Clasificación