1. IOC/DI
(1)什么是IOC(Inversion Of Controll 控制反转)
对象之间的依赖关系由容器来建立。
(2)什么是DI(Dependency Injection 依赖注入
容器可以通过调用set方法或者构造器来建立对象之间的依赖关系。
注:IOC是目标,DI是手段。
(3)依赖注入的两种方式
1)方式一 set方法注入
step1. 添加set方法
step2. 在配置文件中,使用<property>元素来配置。
2)方式二 构造器注入
step1. 添加构造器
step2. 在配置文件当中,使用<constructor-arg>元素来配置。
(4)自动装配 (了解)
注:自动装配,指的是容器依据某些规则,自动建立对象之间的依赖关系。
1)默认情况下,容器不会自动装配。
2)设置autowire属性
(5) 注入基本类型的值
使用value属性来注入,spring容器会帮我们做一些类型的转换工作,
比如将字符串转换成数字。
(6) 注入集合类型的值 (List,Set,Map,Properties)
方式一 直接注入
方式二 引用的方式注入
step1. 将集合类型的值先配置成一个bean。
step2. 再将这个bean注入到对应的bean里面。
代码演示:
/spring02/pom.xml
<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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>welkin</groupId><artifactId>spring02</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>3.2.8.RELEASE</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency></dependencies>
</project>
/spring02/src/main/java/ioc
A.java
package ioc;public class A {private IB b;public void setB(IB b) {System.out.println("A's setB()");this.b = b;}public A() {System.out.println("A()");}public void service(){System.out.println("A's service()");//调用B的f1()方法b.f1();}}
B.java
package ioc;public class B implements IB{
public B(){System.out.println("B()");}public void f1(){System.out.println("B's f1()");}
}
C.java
package ioc;public class C implements IB {
public void f1() {System.out.println("C's F1()");}public C() {System.out.println("C()");}}
IB.java
package ioc;public interface IB {public void f1();
}
Computer .java
package ioc;public class Computer {public Computer() {System.out.println("Computer()");}}
Maneger.java
package ioc;public class Maneger {private Computer cp;public Maneger() {System.out.println("Maneger()");}public Maneger(Computer cp) {System.out.println("Manager(cp)");this.cp = cp;}@Overridepublic String toString() {return "Maneger [cp=" + cp + "]";}}
/spring02/src/main/java/AutoWire
Bar.java
package AutoWire;public class Bar {private Foo foo;public Bar() {System.out.println("Bar()");}public void setFoo(Foo foo) {System.out.println("setFoo()");this.foo = foo;}@Overridepublic String toString() {return "Bar [foo=" + foo + "]";}}
Foo.java
package AutoWire;public class Foo {public Foo() {System.out.println("Foo()");}}
/spring02/src/main/java/value
ExampleBean.java
package value;import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;public class ExampleBean {
private List<String> interest;private Set<String> city;private Map<String,Double> score;private Properties db;@Overridepublic String toString() {return "ExampleBean [interest=" + interest + ", city=" + city + ", score=" + score + ", db=" + db + "]";}public List<String> getInterest() {return interest;}public void setInterest(List<String> interest) {this.interest = interest;}public Set<String> getCity() {return city;}public void setCity(Set<String> city) {this.city = city;}public Map<String, Double> getScore() {return score;}public void setScore(Map<String, Double> score) {this.score = score;}public Properties getDb() {return db;}public void setDb(Properties db) {this.db = db;}}
InfoBean.java
package value;public class InfoBean {private String name;private String interest;private double score;private int pageSize;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getInterest() {return interest;}public void setInterest(String interest) {this.interest = interest;}public double getScore() {return score;}public void setScore(double score) {this.score = score;}public int getPageSize() {return pageSize;}public void setPageSize(int pageSize) {this.pageSize = pageSize;}@Overridepublic String toString() {return "InfoBean [name=" + name + ", interest=" + interest + ", score=" + score + ", pageSize=" + pageSize+ "]";}}
ValueBean.java
package value;import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;public class ValueBean {
private String name;private int age;private List<String> interest;private Set<String> city;private Map<String,Double> score;private Properties db;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public List<String> getInterest() {return interest;}public void setInterest(List<String> interest) {this.interest = interest;}public Set<String> getCity() {return city;}public void setCity(Set<String> city) {this.city = city;}public Map<String, Double> getScore() {return score;}public void setScore(Map<String, Double> score) {this.score = score;}public Properties getDb() {return db;}public void setDb(Properties db) {this.db = db;}@Overridepublic String toString() {return "ValueBean [name=" + name + ", age=" + age + ", interest=" + interest + ", city=" + city + ", score="+ score + ", db=" + db + "]";}}
/spring02/src/main/resources
autowire.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:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:util="http://www.springframework.org/schema/util"xmlns:jpa="http://www.springframework.org/schema/data/jpa"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"><bean id="foo" class="AutoWire.Foo"/><!-- autowire:表示让容器自动建立对象之间的依赖关系。byName:依据属性名查找对应的bean(就是以属性名作为bean的id来查找),找到之后,调用set方法来建立依赖关系。注:如果找不到,则不注入。byType:依据属性类型来查找对应的bean,找到之后,调用set方法来建立依赖关系。注:如果找到多个,则会出错。constructor:与byType类似,只不过,调用构造器来注入。--><bean id="bar" class="AutoWire.Bar" autowire="byType" /></beans>
config.properties
pageSize=10
ioc.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:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:util="http://www.springframework.org/schema/util"xmlns:jpa="http://www.springframework.org/schema/data/jpa"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- set方式注入 --> <bean id="b1" class="ioc.B" /><!-- property:让容器调用set方法来注入依赖关系。其中name属性指定了属性名,ref指定被注入的bean的id。--><bean id="a1" class="ioc.A" > <!-- 会将属性名首字母大写,前面添加set,比如下面就是setB方法 --><property name="b" ref="c1"/></bean><bean id="c1" class="ioc.C"/><!-- 构造器方式注入 --><bean id="cp1" class="ioc.Computer"/><bean id="mg1" class="ioc.Maneger"><!-- constructor-arg:容器会采用构造器来建立依赖关系。index:指定了参数的下标ref:被注入的bean的ID--><constructor-arg index="0" ref="cp1"/></bean>
</beans>
value.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:context="http://www.springframework.org/schema/context"xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"xmlns:jpa="http://www.springframework.org/schema/data/jpa"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"><!-- 使用spring表达式读取其他的Bean的属性 --><bean id ="ib1" class="value.InfoBean"><property name="name" value="#{vb1.name}"/><property name="interest" value="#{vb1.interest[1]}"/><property name="score" value="#{vb1.score['english']}"/><property name="pageSize" value="#{config.pageSize}"/></bean><!-- 注入基本类型的值 --><bean id="vb1" class="value.ValueBean"><property name="name" value="关关" /><property name="age" value="22" /><property name="interest"><list><value>台球</value><value>钓鱼</value><value>看电视</value><value>看电视</value></list></property><property name="city"><set><value>北京</value><value>上海</value><value>武汉</value><value>武汉</value></set></property><property name="score"><map><entry key="english" value="70" /><entry key="math" value="90" /></map></property><property name="db"><props><prop key="username">Sally</prop><prop key="password">1234</prop></props></property></bean><!-- 将集合类型的值配置成一个bean --><!-- util:list 表示使用的是util命名空间下的 list元素。 命名空间:为了区分同名的元素而添加的前缀。 --><util:list id="interestBean"><value>喝酒</value><value>烫头</value><value>抽烟</value></util:list><util:set id="cityBean"><value>长沙</value><value>岳阳</value><value>华容</value></util:set><util:map id="scoreBean"><entry key="math" value="80" /><entry key="english" value="59.5" /></util:map><util:properties id="dbBean"><prop key="username">Giving</prop><prop key="password">test</prop></util:properties><!-- 采用引用的方式注入集合类型的值 --><bean id="eb1" class="value.ExampleBean"><property name="interest" ref="interestBean" /><property name="city" ref="cityBean" /><property name="score" ref="scoreBean" /><property name="db" ref="dbBean" /></bean><!-- 读取.properties文件的内容 --><!-- location:指定属性文件的位置。 注:classpath,表示让容器依据类路径 去查找属性文件。 容器会读取指定位置的文件的内容,并且 将这些内容存放到Properties对象里面 --><util:properties id="config" location="classpath:config.properties" /></beans>
/spring02/src/test/java/test/TestCase.java
package test;import java.util.Properties;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import AutoWire.Bar;
import ioc.A;
import ioc.Maneger;
import value.ExampleBean;
import value.InfoBean;
import value.ValueBean;public class TestCase {
@Test// 测试 set方法注入public void test1() {String config = "ioc.xml";ApplicationContext ac = new ClassPathXmlApplicationContext(config);A a = ac.getBean("a1", A.class);a.service();}@Test// 测试 构造器注入public void test2() {String config = "ioc.xml";ApplicationContext ac = new ClassPathXmlApplicationContext(config);Maneger mg1 = ac.getBean("mg1", Maneger.class);System.out.println(mg1);}@Test// 测试 自动装配public void test3() {String config = "autowire.xml";ApplicationContext ac = new ClassPathXmlApplicationContext(config);Bar bar = ac.getBean("bar", Bar.class);System.out.println(bar);}@Test// 测试 注入基本类型的值public void test4() {String config = "value.xml";ApplicationContext ac = new ClassPathXmlApplicationContext(config);ValueBean vb1 = ac.getBean("vb1", ValueBean.class);System.out.println(vb1);}@Test// 测试 集合类型的值的注入public void test5() {String config = "value.xml";ApplicationContext ac = new ClassPathXmlApplicationContext(config);ExampleBean eb = ac.getBean("eb1", ExampleBean.class);System.out.println(eb);}@Test// 测试 读取属性文件的内容public void test6() {String config = "value.xml";ApplicationContext ac = new ClassPathXmlApplicationContext(config);Properties props = ac.getBean("config", Properties.class);System.out.println(props);}@Test//测试 spring表达式public void test7(){String config = "value.xml";ApplicationContext ac = new ClassPathXmlApplicationContext(config);InfoBean ib1 = ac.getBean("ib1",InfoBean.class);System.out.println(ib1);}
}