zoukankan      html  css  js  c++  java
  • Spring 在Web中的应用

    Spring 在Web中的应用

      在web项目开发中,不会直接实例化ApplicationContext对象,如果想用到ApplicationContext,一般的步骤:

    1. 配置一个监听器ContextLoaderListener,这个监听器是ServletContext的监听器

      <listener>
              <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      
      <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:applicationContext.xml</param-value>
      </context-param>
    2. 可选的,设定你的xml配置文件的路径

      1. 如果文件在web-info 下面,并且名字为applicationContext.xml,就不需要再额外配置

      2. 如果文件不符合上面的规则,就需要配置context-param、

    3. 利用一个WebApplicationContextUtils来读取第二部创建的ApplicationContext对象


    导相关依赖包

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <project xmlns="http://maven.apache.org/POM/4.0.0"
     3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     5     <modelVersion>4.0.0</modelVersion>
     6 
     7     <groupId>com</groupId>
     8     <artifactId>spring-demo</artifactId>
     9     <version>1.0-SNAPSHOT</version>
    10     <packaging>war</packaging>
    11 
    12 
    13     <dependencies>
    14         <dependency>
    15             <groupId>org.springframework</groupId>
    16             <artifactId>spring-core</artifactId>
    17             <version>5.0.2.RELEASE</version>
    18         </dependency>
    19         <dependency>
    20             <groupId>org.springframework</groupId>
    21             <artifactId>spring-context</artifactId>
    22             <version>5.0.2.RELEASE</version>
    23         </dependency>
    24 
    25         <dependency>
    26             <groupId>org.springframework</groupId>
    27             <artifactId>spring-jdbc</artifactId>
    28             <version>5.0.2.RELEASE</version>
    29         </dependency>
    30         <dependency>
    31             <groupId>com.microsoft.sqlserver</groupId>
    32             <artifactId>mssql-jdbc</artifactId>
    33             <version>6.2.1.jre8</version>
    34         </dependency>
    35 
    36         <!-- hibernate -->
    37 
    38         <dependency>
    39             <groupId>org.hibernate</groupId>
    40             <artifactId>hibernate-core</artifactId>
    41             <version>5.2.10.Final</version>
    42         </dependency>
    43 
    44         <!-- 下面的依赖里面至少有LocalSessionFactoryBean -->
    45         <dependency>
    46             <groupId>org.springframework</groupId>
    47             <artifactId>spring-orm</artifactId>
    48             <version>5.0.2.RELEASE</version>
    49         </dependency>
    50         <dependency>
    51             <groupId>org.springframework</groupId>
    52             <artifactId>spring-beans</artifactId>
    53             <version>5.0.2.RELEASE</version>
    54         </dependency>
    55         <!--Web依赖-->
    56         <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
    57         <dependency>
    58             <groupId>javax.servlet</groupId>
    59             <artifactId>javax.servlet-api</artifactId>
    60             <version>3.1.0</version>
    61             <scope>provided</scope>
    62         </dependency>
    63         <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
    64         <dependency>
    65             <groupId>org.springframework</groupId>
    66             <artifactId>spring-web</artifactId>
    67             <version>5.0.1.RELEASE</version>
    68         </dependency>
    69 
    70 
    71     </dependencies>
    72     <build>
    73         <plugins>
    74             <plugin>
    75                 <artifactId>maven-war-plugin</artifactId>
    76                 <version>2.2</version>
    77                 <configuration>
    78                     <warSourceDirectory>web</warSourceDirectory>
    79                 </configuration>
    80             </plugin>
    81         </plugins>
    82     </build>
    83 </project>

      配置web.xml文件

     1     <listener>
     2         <!--配置一个监听器ContextLoaderListener,这个监听器是ServletContext的监听器-->
     3         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     4     </listener>
     5 
     6     <context-param>
     7         <!--spring的配置文件路径-->
     8         <param-name>contextConfigLocation</param-name>
     9         <param-value>classpath:Springweb.xml</param-value>
    10     </context-param>

      配置Spring的依赖注入:SpringWeb.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
     5 
     6 
     7 
     8     <bean id="dao" class="web.UserDaoImpl"></bean>
     9     <bean id="service" class="web.Service">
    10         <property name="userDao" ref="dao"></property>
    11     </bean>
    12 </beans>

      dao接口

    1 public interface UserDao {
    2     void add();
    3 }

      dao实现

    1 public class UserDaoImpl implements UserDao
    2 {
    3     public void add() {
    4         System.out.println("aaaaaaaa");
    5     }
    6 }

      service

     1 public class Service {
     2     private UserDao userDao;
     3 
     4     public UserDao getUserDao() {
     5         return userDao;
     6     }
     7 
     8     public void setUserDao(UserDao userDao) {
     9         this.userDao = userDao;
    10     }
    11     public void insert(){
    12         userDao.add();
    13     }
    14 }

      servlet

     1 @WebServlet("/fist")
     2 public class Servlet extends HttpServlet {
     3     @Override
     4     protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     5        /* ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Springweb.xml");
     6         Service service = applicationContext.getBean("service",Service.class);
     7         service.insert();*/
     8         ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(req.getServletContext());
     9         Service service = applicationContext.getBean("service",Service.class);
    10         service.insert();
    11     }
    12 }
  • 相关阅读:
    验证码图片不刷新解决方法
    表单验证
    Thinkphp显示系统常量信息的方法(php的用法)
    原生sql语句执行
    Python中的模块(2)
    Python 正则表达式中级
    正则表达式 和 原生字符串 r
    collections模块
    时间模块
    random模块
  • 原文地址:https://www.cnblogs.com/sunduge/p/8315962.html
Copyright © 2011-2022 走看看