如何在web应用里面配置spring
1、在pom.xml文件中必须加入如下配置:1 <dependency> 2 <groupId>org.springframework</groupId> 3 <artifactId>spring-web</artifactId> 4 <version>${spring.version}</version> 5 </dependency> 6 <dependency> 7 <groupId>org.springframework</groupId> 8 <artifactId>spring-webmvc</artifactId> 9 <version>${spring.version}</version>10 </dependency>。

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 }

5、33 如果通过在web.xml中的ServletContext上下文中定义参量,那么整个web应用程序中的servlet都可调用,web.xml中的格式为:34 <context-param>35 <param-name>test</param-name>36 <param-value>Is it me</param-value>37 < context -param>38 2、调用<context-param>中的参量,调用格式为: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 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 5 version="2.5"> 6 <!-- 配置spring配置文件的名称和位置 --> 7 <context-param> 8 <param-name>configLocation</param-name> 9 <param-value>applicationContext2.xml</param-value>10 </context-param>11 <!-- 启动IOC容器的ServletContextListener -->12 <listener>13 <listener-class>com.wn.listener.SpringServletContextListener</listener-class>14 </listener>15 <!-- 配置服务器运行路径 -->16 <servlet>17 <servlet-name>testServlet</servlet-name>18 <servlet-class>com.wn.servlet.TestServlet</servlet-class>19 <init-param>20 <param-name>configLocation</param-name>21 <param-value>classpath*:applicationContext2*.xml</param-value>22 </init-param>23 <load-on-startup>1</load-on-startup>24 </servlet>25 <servlet-mapping>26 <servlet-name>testServlet</servlet-name>27 <url-pattern>/TestServlet</url-pattern>28 </servlet-mapping>29 </web-app>。
