spring学习(三)属性注入
用的是IDEA的maven工程,pom.xml文件导包依赖省略
本文主要写set方式注入
(一)、一般类型注入
一、写两个实体类Car、User
public class Car { private String color; private String carName; public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getCarName() { return carName; } public void setCarName(String carName) { this.carName = carName; } @Override public String toString() { return "Car{" + "color='" + color + '\'' + ", carName='" + carName + '\'' + '}'; } }
public class User { private String name; private Integer age; private Car car; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + ", car=" + car + '}'; } }
二、书写配置文件applicationContext.xml(由于用的是IDEA,配置文件放在resources文件夹中),并注意约束的导入
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd "> <!--set方式注入--> <bean name="user" class="dyh.so.User"> <!--为User对象的name与age注入Lily和12--> <property name="name" value="lily"></property> <property name="age" value="12"></property> <!--为car属性注入下面的car对象--> <property name="car" ref="car"></property> </bean> <bean name="car" class="dyh.so.Car"> <!--为Car对象注入属性值--> <property name="carName" value="兰博基尼"></property> <property name="color" value="red"></property> </bean> </beans>
三、测试
import dyh.so.User; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; @Test public void fun(){ //1、创建容器对象 ApplicationContext ct = new ClassPathXmlApplicationContext("applicationContext.xml"); //2、向容器要User对象 User user = (User) ct.getBean("user"); //3、打印在容器中获得的对象 System.out.println(user); } }
测试结果:
(二)、复杂类型注入
一、写出要注入的实体类
import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Properties; public class CollectionBean { private List list;//list/set 类型注入 private Map map;//map类型注入 private Properties prop;//properties类型注入 public List getList() { return list; } public void setList(List list) { this.list = list; } public Map getMap() { return map; } public void setMap(Map map) { this.map = map; } public Properties getProp() { return prop; } public void setProp(Properties prop) { this.prop = prop; } @Override public String toString() { return "CollectionBean{" + ", list=" + list + ", map=" + map + ", prop=" + prop + '}'; } }
二、书写配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd "> <!--set方式注入--> <bean name="user" class="dyh.so.User"> <!--为User对象的name与age注入Lily和12--> <property name="name" value="lily"></property> <property name="age" value="12"></property> <!--为car属性注入下面的car对象--> <property name="car" ref="car"></property> </bean> <bean name="car" class="dyh.so.Car"> <!--为Car对象注入属性值--> <property name="carName" value="兰博基尼"></property> <property name="color" value="red"></property> </bean> <!--复杂类型注入--> <bean name="collbean" class="dyh.ss.CollectionBean"> <!--list 注入--> <property name="list"> <list> <value>张三</value> <value>李四</value> <ref bean="car"/> <ref bean="user"/> </list> </property> <!--map注入--> <property name="map"> <map> <entry key="1" value="一"></entry> <entry key="2" value="二"></entry> <entry key="3" value="三"></entry> <entry key-ref="user" value-ref="car"></entry> </map> </property> <!-- prperties 类型注入 --> <property name="prop" > <props> <prop key="driverClass">com.jdbc.mysql.Driver</prop> <prop key="userName">root</prop> <prop key="password">1234</prop> </props> </property> </bean> </beans>
三、测试
@Test public void fun2(){ //创建容器 ApplicationContext ct = new ClassPathXmlApplicationContext("applicationContext.xml"); //向容器中要ConnectionBean对象 Object collbean = ct.getBean("collbean"); //打印出来在spring中获得的对象 System.out.println(collbean); }
测试结果