首先先建立一个Worker类
import java.io.Serializable;
public class Worker implements Serializable {
private String name="光头强";
private Tool tool;
public void setTool(Tool tool) {
System.out.println("注入tool对象");
this.tool=tool;
}
public void work() {
System.out.println(name+"使用"+tool+"砍树");
}
}
武器类:
import java.io.Serializable;
public class Axe implements Tool,Serializable{
private String name="开天斧";
@Override
public String toString() {
// TODO Auto-generated method stub
return name;
}
}
import java.io.Serializable;
public class Saw implements Tool,Serializable{
private String name="寒冰锯";
@Override
public String toString() {
// TODO Auto-generated method stub
return name;
}
}
Tool接口
public interface Tool {
}
beans3.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.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">
<!-- 利用Spring 调用对象的生命周期管理方法 -->
<bean id="logger" class="day02.Logger"
init-method="open" lazy-init="true" destroy-method="close"/>
<!--依赖注入DI -->
<bean id="a1" class="day02.Axe"></bean>
<bean id="qiang" class="day02.Worker">
<!--注入属性name="axe"则自动查找setAxe方法 -->
<property name="axe" ref="a1"></property>
</bean>
<!-- 解耦:spring和接口配合,实现组件之间解耦,也就是紧耦合转换为松耦合关系 -->
<bean id="ax" class="day02.demo.Axe"/>
<bean id="sw" class="day02.demo.Saw"/>
<bean id="worker" class="day02.demo.Worker">
<property name="tool" ref="sw"></property>
</bean>
测试类
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestCase4 {
ClassPathXmlApplicationContext ctx;
@Before
public void init() {
ctx=new ClassPathXmlApplicationContext("beans3.xml");
}
@Test
public void testWork() {
day02.demo.Worker worker=ctx.getBean("worker1",day02.demo.Worker.class);
worker.work();
}
}
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>cn.tedu</groupId>
<artifactId>Spring02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.25.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
</project>