多语言展示
当前在线:643今日阅读:39今日分享:10

如何在web应用里面配置spring

今天小编给大家带来的是如何在web应用里面配置spring,希望能帮助到大家!
工具/原料

PC

方法/步骤
1

1、在pom.xml文件中必须加入如下配置:1 2      org.springframework 3      spring-web 4      ${spring.version} 5 6 7      org.springframework 8      spring-webmvc 9      ${spring.version}10

2

2、WEB应用下Spring的配置文件和非WEB应用下没有不同。3、如何创建IOC容器。非web应用在main方法中直接创建。应该在WEB应用被服务器加载时就创建IOC容器:在ServletContextListener#contextInitialized(ServletContextEvent sce)方法中创建IOC容器。在web应用的其他组件中如何访问IOC容器:在ServletContextListener#contextInitialized(ServletContextEvent sce)方法中创建IOC容器后,可以把其放在ServletContext(即application域)的一个属性中。实际上,spring配置文件的名字和位置也是可配置的,将其配置到当前web应用的初始化参数中(web.xml)。

3

1)首先新建Person类。1 package com.wn.beans; 2 3 public class Person { 4     private String username; 5 6     public void setUsername(String username) { 7         this.username = username; 8    } 9 10     public void hello() {11         System.out.println('my name is:' + username);12    }13 }

4

3)创建SpringServletContextListener类使用ServletContextListener的监听器功能,在ServletContextListener#contextInitialized(ServletContextEvent sce)方法中创建IOC容器,然后将IOC容器放在ServletContext的一个属性中1 package com.wn.listener; 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 * Application Lifecycle Listener implementation class11 * SpringServletContextListener12  */13 public class SpringServletContextListener implements ServletContextListener {14     /**15     * Default constructor.16      */17     public SpringServletContextListener() {18         // TODO Auto-generated constructor stub19    }20     /**21     * @see ServletContextListener#contextDestroyed(ServletContextEvent)22      */23     public void contextDestroyed(ServletContextEvent arg0) {24         // TODO Auto-generated method stub25    }26     /**27     * @see ServletContextListener#contextInitialized(ServletContextEvent)28      */29     public void contextInitialized(ServletContextEvent arg0) {30         // 1、获取spring配置文件的名称和位置31         ServletContext servletContext=arg0.getServletContext();32         /**1、getInitParameter()方法是在GenericServlet接口中新定义的一个方法,用来调用初始化在web.xml中存放的参量。

5

33        如果通过在web.xml中的ServletContext上下文中定义参量,那么整个web应用程序中的servlet都可调用,web.xml中的格式为:34         35                test36                Is it me37         < context -param>38           2、调用中的参量,调用格式为:39        String name =getServletContext(). getInitParameter(“name”); 或40        String name = getServletConfig().getServletContext().getInitParameter(“name”);41          */42         String config=servletContext.getInitParameter('configLocation');43         // 2、创建IOC容器44         ApplicationContext ctx = new ClassPathXmlApplicationContext(config);45         // 3、将IOC容器放在ServletContext的一个属性中46         servletContext.setAttribute('ApplicationContext',ctx);47    }48 }4)在web.xml文件中配置如下信息1 2 6     7     8         configLocation 9         applicationContext2.xml10     11     12     13         com.wn.listener.SpringServletContextListener14     15     16     17         testServlet18         com.wn.servlet.TestServlet19         20             configLocation21             classpath*:applicationContext2*.xml22         23         124     25     26         testServlet27         /TestServlet28     29

注意事项
1

网络虽好,但要注意劳逸结合哦!

2

如果是青少年,小编在这里提示大家千万不能沉迷网络!

推荐信息