maven的pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 <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/maven-v4_0_0.xsd" > <modelVersion > 4.0.0</modelVersion > <groupId > com.swb</groupId > <artifactId > helloSpring</artifactId > <packaging > war</packaging > <version > 0.0.1-SNAPSHOT</version > <name > helloSpring Maven Webapp</name > <url > http://maven.apache.org</url > <dependencies > <dependency > <groupId > junit</groupId > <artifactId > junit</artifactId > <version > 3.8.1</version > <scope > test</scope > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-web</artifactId > <version > ${springVersion}</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-webmvc</artifactId > <version > ${springVersion}</version > </dependency > </dependencies > <build > <finalName > helloSpring</finalName > </build > <properties > <springVersion > 3.2.5.RELEASE</springVersion > </properties > </project >
配置resources/applicationContext.xml 包含了所有bean对象的创建和注入 spring使用配置文件通过控制反转和依赖注入使得将类和类之间解耦 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns ="http://www.springframework.org/schema/beans" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > <bean name ="user" class ="com.tr.bean.User" scope ="prototype" init-method ="init" destroy-method ="destory" > </bean > <bean name ="user2" class ="com.tr.bean.User" > <property name ="name" value ="tom" /> <property name ="age" value ="18" /> <property name ="car" ref ="car" /> </bean > <bean name ="car" class ="com.tr.bean.Car" > <property name ="name" value ="奔驰" > </property > <property name ="color" value ="white" > </property > </bean > <bean name ="cu" class ="com.tr.bean.ComplexUser" > <property name ="arr" > <array > <value > tom</value > <value > jerry</value > </array > </property > <property name ="list" > <list > <value > test</value > <value > test</value > </list > </property > <property name ="map" > <map > <entry key ="tr" value ="1" /> <entry key ="car" value-ref ="car2" /> <entry key-ref ="car2" value-ref ="car2" /> </map > </property > <property name ="properties" > <props > <prop key ="tr" > 111</prop > <prop key ="tr" > 111</prop > </props > </property > </bean > <bean name ="car2" class ="com.tr.bean.Car" > <constructor-arg name ="color" value ="white" /> <constructor-arg name ="name" value ="benz" /> </bean > </beans >
创建和使用容器对象 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public class Demo { @Test public void func1 () { ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml" ); User user = (User) ac.getBean("user2" ); System.out.println(user); Car car2 = (Car) ac.getBean("car2" ); ComplexUser cu = (ComplexUser) ac.getBean("cu" ); System.out.println(cu.getArr()[0 ]); System.out.println(cu.getArr()[1 ]); ac.close(); } }
在Action或controller中使用
需要将ClassPathXmlApplication对象配置在Application域中,生命周期和项目一致,放在action对象里会导致创建n个
这需要在web.xml中配置监听器
1 2 3 4 5 6 7 8 9 <listener > <listener-class > org.springframework.web.context.ContextLoaderListener</listener-class > </listener > <context-param > <para-name > contextConfigLocation</para-name > <para-value > classpath:applicationContext.xml</para-value > </context-param >
struts 项目 在把spring放入容器于项目application后,再取出使用
1 2 3 4 5 ServletContext sc = ServletActionContext.getServletContext(); WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc); ac.getBean("customerService" );
Welcome to my other publishing channels