Spring学习之xml配置Bean总结
本篇文章已重新总结到新博客,欢迎访问:https://ladybug.top/Spring/review-spring---spring-ioc.html
学习Spring时,我用的是Maven来管理jar包,先看看maven的pom.xml:
pom.xml
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>spring</groupId> 5 <artifactId>spring_TencentKe_Tonggang-5</artifactId> 6 <packaging>war</packaging> 7 <version>1.4</version> 8 <name>spring_TencentKe_Tonggang-5 Maven Webapp</name> 9 <url>http://maven.apache.org</url> 10 <dependencies> 11 <dependency> 12 <groupId>junit</groupId> 13 <artifactId>junit</artifactId> 14 <version>3.8.1</version> 15 <scope>test</scope> 16 </dependency> 17 <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> 18 <dependency> 19 <groupId>org.springframework</groupId> 20 <artifactId>spring-core</artifactId> 21 <version>4.3.7.RELEASE</version> 22 </dependency> 23 <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> 24 <dependency> 25 <groupId>org.springframework</groupId> 26 <artifactId>spring-context</artifactId> 27 <version>4.3.7.RELEASE</version> 28 </dependency> 29 <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> 30 <dependency> 31 <groupId>org.springframework</groupId> 32 <artifactId>spring-beans</artifactId> 33 <version>4.3.7.RELEASE</version> 34 </dependency> 35 <!-- https://mvnrepository.com/artifact/c3p0/c3p0 --> 36 <dependency> 37 <groupId>c3p0</groupId> 38 <artifactId>c3p0</artifactId> 39 <version>0.9.1.2</version> 40 </dependency> 41 <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> 42 <dependency> 43 <groupId>mysql</groupId> 44 <artifactId>mysql-connector-java</artifactId> 45 <version>5.1.38</version> 46 </dependency> 47 48 <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --> 49 <dependency> 50 <groupId>org.aspectj</groupId> 51 <artifactId>aspectjweaver</artifactId> 52 <version>1.8.9</version> 53 </dependency> 54 <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> 55 <dependency> 56 <groupId>org.springframework</groupId> 57 <artifactId>spring-jdbc</artifactId> 58 <version>4.3.7.RELEASE</version> 59 </dependency> 60 </dependencies> 61 <build> 62 <finalName>spring_TencentKe_Tonggang-5</finalName> 63 </build> 64 </project>
先看看写的三个bean:
Car.java:
package spring.beans; public class Car { private String cname;//车名字 private int price;//价格 private double max;//最大速度 public Car() { super(); // TODO Auto-generated constructor stub } public Car(String cname, double max) { super(); this.cname = cname; this.max = max; } public Car(String cname, int price) { super(); this.cname = cname; this.price = price; } public void setCname(String cname) { this.cname = cname; } public void setPrice(int price) { this.price = price; } @Override public String toString() { return "Car [cname=" + cname + ", price=" + price + ", max=" + max + "]"; } }
Person.java:
package spring.beans; import java.util.List; import java.util.Map; public class Person { private String Pname;//姓名 private int age;//年龄 private Car car;//一辆车 private List<Car> cars;//很多车 private Map<String, String> des;//描述 public Person() { } public void setDes(Map<String, String> des) { this.des = des; } public void setCars(List<Car> cars) { this.cars = cars; } public void setPname(String pname) { Pname = pname; } public void setAge(int age) { this.age = age; } public void setCar(Car car) { this.car = car; } @Override public String toString() { return "Person [Pname=" + Pname + ", age=" + age + ", car=" + car + ", cars=" + cars + ", des=" + des + "]"; } }
Person1.java:
package spring.beans; import java.util.List; public class Person1 { private String pname; private int age; private List<Car> cars; public void setPname(String pname) { this.pname = pname; } public void setAge(int age) { this.age = age; } public void setCars(List<Car> cars) { this.cars = cars; } @Override public String toString() { return "Person1 [pname=" + pname + ", age=" + age + ", list=" + cars + "]"; } }
xml配置中,看下面xml文件配置:
applicationContext.xml:
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd"> <bean id="person" class="spring.beans.Person"><!--将这个bean加入到spring的ioc容器--> <property name="pname" value="张三"></property><!--给bean的pname属性赋值--> <property name="age" value="20"></property> <property name="cars" ><!--给bean中的list<Car>类型属性赋值--> <list> <ref bean="car1"/><!--配置第一个car--> <ref bean="car2"/><!--配置第二个car--> </list> </property> <property name="des"><!--给bean中map<String,String>类型的属性赋值--> <map> <entry key="a" value="a1"></entry><!--第一个值--> <entry key="b" value="b1"></entry><!--第二个值--> </map> </property> </bean> <util:list id="cars"><!--利用util标签,配置多个car--> <ref bean="car1"/> <ref bean="car2"/> </util:list> <bean id="person1" class="spring.beans.Person1" p:pname="王五" p:age="23" p:cars-ref="cars"></bean><!--配置bean到IOC容器,并用p标签直接给属性赋值,p:cars-ref就用到了上面的util标签中的id值--> <bean id="car1" class="spring.beans.Car"><!--配置第一个car--> <property name="cname" value="奥迪"></property> <property name="price" value="300000"></property> </bean> <bean id="car2" class="spring.beans.Car"><!--配置第二个car--> <property name="cname" value="长安"></property> <property name="price" value="100000"></property> </bean> </beans>
测试:
package spring.beans.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import spring.beans.Person; import spring.beans.Person1; public class Main { public static void main(String[] args) { ApplicationContext cxt= new ClassPathXmlApplicationContext("applicationContext.xml"); Person p=(Person) cxt.getBean("person"); System.out.println(p); Person1 p1=(Person1) cxt.getBean("person1"); System.out.println(p1); } }
输出:
Person [Pname=张三, age=20, car=null, cars=[Car [cname=奥迪, price=300000, max=0.0], Car [cname=长安, price=100000, max=0.0]], des={a=a1, b=b1}] Person1 [pname=王五, age=23, list=[Car [cname=奥迪, price=300000, max=0.0], Car [cname=长安, price=100000, max=0.0]]]