对象之间的依赖关系由容器来建立。
容器可以通过调用set方法或者构造器来建立对象之间的依赖关系。
注:
IOC是目标,DI是手段。
1)方式一 set方法注入
step1. 添加set方法
step2. 在配置文件中,使用元素来配置。
2)方式二 构造器注入
step1. 添加构造器
step2. 在配置文件当中,使用元素来配置。
注:自动装配,指的是容器依据某些规则,自动建立对象之间的依赖关系。
1)默认情况下,容器不会自动装配。
2)设置autowire属性
使用value属性来注入,spring容器会帮我们做一些类型的转换工作,
比如将字符串转换成数字。
方式一 直接注入
方式二 引用的方式注入
step1. 将集合类型的值先配置成一个bean。
step2. 再将这个bean注入到对应的bean里面。
<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.0modelVersion>
<groupId>welkingroupId>
<artifactId>spring02artifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>warpackaging>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>3.2.8.RELEASEversion>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
dependencies>
project>
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();
}
}
package ioc;
public class B implements IB{
public B(){
System.out.println("B()");
}
public void f1(){
System.out.println("B's f1()");
}
}
package ioc;
public class C implements IB {
public void f1() {
System.out.println("C's F1()");
}
public C() {
System.out.println("C()");
}
}
package ioc;
public interface IB {
public void f1();
}
package ioc;
public class Computer {
public Computer() {
System.out.println("Computer()");
}
}
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;
}
@Override
public String toString() {
return "Maneger [cp=" + cp + "]";
}
}
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;
}
@Override
public String toString() {
return "Bar [foo=" + foo + "]";
}
}
package AutoWire;
public class Foo {
public Foo() {
System.out.println("Foo()");
}
}
package value;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class ExampleBean {
private List interest;
private Set city;
private Map score;
private Properties db;
@Override
public String toString() {
return "ExampleBean [interest=" + interest + ", city=" + city + ", score=" + score + ", db=" + db + "]";
}
public List getInterest() {
return interest;
}
public void setInterest(List interest) {
this.interest = interest;
}
public Set getCity() {
return city;
}
public void setCity(Set city) {
this.city = city;
}
public Map getScore() {
return score;
}
public void setScore(Map score) {
this.score = score;
}
public Properties getDb() {
return db;
}
public void setDb(Properties db) {
this.db = db;
}
}
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;
}
@Override
public String toString() {
return "InfoBean [name=" + name + ", interest=" + interest + ", score=" + score + ", pageSize=" + pageSize
+ "]";
}
}
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 interest;
private Set city;
private Map 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 getInterest() {
return interest;
}
public void setInterest(List interest) {
this.interest = interest;
}
public Set getCity() {
return city;
}
public void setCity(Set city) {
this.city = city;
}
public Map getScore() {
return score;
}
public void setScore(Map score) {
this.score = score;
}
public Properties getDb() {
return db;
}
public void setDb(Properties db) {
this.db = db;
}
@Override
public String toString() {
return "ValueBean [name=" + name + ", age=" + age + ", interest=" + interest + ", city=" + city + ", score="
+ score + ", db=" + db + "]";
}
}
<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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<bean id="foo" class="AutoWire.Foo"/>
<bean id="bar" class="AutoWire.Bar" autowire="byType" />
beans>
pageSize=10
<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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<bean id="b1" class="ioc.B" />
<bean id="a1" class="ioc.A" >
<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="0" ref="cp1"/>
bean>
beans>
<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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<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">Sallyprop>
<prop key="password">1234prop>
props>
property>
bean>
<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">Givingprop>
<prop key="password">testprop>
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>
<util:properties id="config" location="classpath:config.properties" />
beans>
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);
}
}