寫在以前,做爲正在學習的程序員,對於Spring理解比較差,給兩個簡單的定義你們看一下。java
控制反轉(Inversion of Control),是一個重要的面向對象編程的法則來削減計算機程序的耦合問題,也是輕量級的Spring框架的核心,beans。程序員
DI依賴注入:說的是建立對象實例時,爲這個對象注入屬性值或其它對象實例,側重於實現。spring
今天的博客主要寫的是DI,因此咱們看的是實現,而不是思想。編程
首相咱們先來看最簡單的set方法注入app
咱們準備一個實體類就能夠作第一個例子,框架
publi class Users(){ private String name; private Integer age;
}
<?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:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- <context:component-scan base-package="cn.curry"></context:component-scan> --> <bean id="users" class="cn.curry.spring.Users"> <property name="name" value="呵呵"></property> </bean>
上面是咱們的部分配置文件。學習
最後咱們看簡單的測試測試
@Test public void test01(){ Resource resource=new ClassPathResource("applicationContext.xml"); BeanFactory bf=new XmlBeanFactory(resource); Users users=(Users)bf.getBean("users"); Users userss=(Users)bf.getBean("users"); System.out.println(users==userss); System.out.println(users.getName()); }
而後咱們再看域屬性注入和P命名空間注入this
這時咱們會添加第二個實體類,而且在Users類里加一個構造spa
public Users() {
System.out.println("Object Init");
}
public Users(String name, Integer age, Car car) {
this.name = name;
this.age = age;
this.car = car;
}
public class Car {private String color; public String getColor() { return color; } public void setColor(String color) { this.color = color; } }
配置文件
<bean id="us2" class="cn.curry.spring.Users"> <constructor-arg index="0" value="花花"></constructor-arg> <constructor-arg index="1" value="12"></constructor-arg> <constructor-arg index="2" ref="car"></constructor-arg> </bean> <bean id="usersP" class="cn.curry.spring.Users" p:name="花花" p:age="12" p:car-ref="car"></bean>
看測試
@Test public void test02(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); Users users=(Users) context.getBean("us2"); System.out.println(users.getCar().getColor()+" "+users.getName()); } @Test public void test03(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); Users users=(Users) context.getBean("usersP"); System.out.println(users.getCar().getColor()+" "+users.getName()); }
而後看集合屬性注入
先修改Users,添加集合屬性
private List<Car> list; private Set<Car> set; private Map<Integer,Car> map;
在看配置文件
<bean id="uslist" class="cn.curry.spring.Users"> <property name="list"> <list> <ref bean="car"></ref> </list> </property> </bean> <bean id="usset" class="cn.curry.spring.Users"> <property name="set"> <set> <ref bean="car"></ref> </set> </property> </bean> <bean id="usmap" class="cn.curry.spring.Users"> <property name="map"> <map> <entry key="1" value-ref="car"></entry> </map> </property> </bean>
在看測試
@Test public void test04(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); Users users=(Users) context.getBean("uslist"); List<Car> list = users.getList(); for (Car item : list) { System.out.println(item.getColor()); } } @Test public void test05(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); Users users=(Users) context.getBean("usset"); Set<Car> set = users.getSet(); for (Car item : set) { System.out.println(item.getColor()); } } @Test public void test06(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); Users users=(Users) context.getBean("usmap"); Map<Integer, Car> map = users.getMap(); for (Integer item : map.keySet()) { System.out.println(map.get(item).getColor()); } }
最後咱們看一下註解這時配置只需一句話
<context:component-scan base-package="cn.curry"></context:component-scan>
而後看咱們的實體類
package cn.curry.spring; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.util.List; import java.util.Map; import java.util.Set; /** * Created by Curry on 2017/3/25. */ @Component("usss") public class Users { @Value("赫爾") private String name; @Value("18") private Integer age; //@Resource(name="car") @Autowired @Qualifier("car") private Car car; private List<Car> list; private Set<Car> set; private Map<Integer,Car> map; public Users() { System.out.println("Object Init"); } public Users(String name, Integer age, Car car) { this.name = name; this.age = age; this.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; } public List<Car> getList() { return list; } public void setList(List<Car> list) { this.list = list; } public Set<Car> getSet() { return set; } public void setSet(Set<Car> set) { this.set = set; } public Map<Integer, Car> getMap() { return map; } public void setMap(Map<Integer, Car> map) { this.map = map; } }
package cn.curry.spring; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.stereotype.Controller; import org.springframework.stereotype.Repository; import org.springframework.stereotype.Service; /** * Created by Curry on 2017/3/25. */ @Component("car") //@Service //@Controller //@Repository public class Car { @Value("黃色") private String color; public String getColor() { return color; } public void setColor(String color) { this.color = color; } }
最後一個測試
package cn.curry.test; import cn.curry.spring.Car; import cn.curry.spring.Users; import org.junit.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import java.util.List; import java.util.Map; import java.util.Set; /** * Created by Curry on 2017/3/25. */ public class MyTest { @Test public void test01(){ Resource resource=new ClassPathResource("applicationContext.xml"); BeanFactory bf=new XmlBeanFactory(resource); Users users=(Users)bf.getBean("users"); Users userss=(Users)bf.getBean("users"); System.out.println(users==userss); System.out.println(users.getName()); } @Test public void test02(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); Users users=(Users) context.getBean("us2"); System.out.println(users.getCar().getColor()+" "+users.getName()); } @Test public void test03(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); Users users=(Users) context.getBean("usersP"); System.out.println(users.getCar().getColor()+" "+users.getName()); } @Test public void test04(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); Users users=(Users) context.getBean("uslist"); List<Car> list = users.getList(); for (Car item : list) { System.out.println(item.getColor()); } } @Test public void test05(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); Users users=(Users) context.getBean("usset"); Set<Car> set = users.getSet(); for (Car item : set) { System.out.println(item.getColor()); } } @Test public void test06(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); Users users=(Users) context.getBean("usmap"); Map<Integer, Car> map = users.getMap(); for (Integer item : map.keySet()) { System.out.println(map.get(item).getColor()); } } @Test public void test07(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); Users users=(Users) context.getBean("usss"); System.out.println(users.getName()+" "+users.getCar().getColor()); } }