今天看了下spring,对于其中的IOC(控制反转)有了一定的认识。使用IOC,对象是被动的接受依赖类,而不是主动的去找,容器在实例化的时候主动将它的依赖类注入给它。可以这样理解:IOC将类的主动权转移到接口上,依赖注入通过配置xml文件在类实例化时将其依赖类注入。
依赖注入:spring中通过依赖注入((Denpendency Injection)来实现IOC,依赖注入有三种实现方式:接口注入(Interface Injection)、值注入(setter Injection)、构造子注入(Constructor Injec)
下面看两个例子:
1. 使用xml配置:
(1) 构造HelloBean类:
package com.taobao.test;
public class HelloBean {
private String msg;
private String helloWorld;
private String user;
public HelloBean(String helloWorld, String user){
this.helloWorld = helloWorld; this.user = user;
}
public String sayHelloToUser() {
return helloWorld + "to:" + user;
}
public void sayHello() {
System.out.println(msg);
}
public void setMsg(String msg) {
this.msg = msg;
}
}
(2) xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="hello" class="com.taobao.test.HelloBean">
<!-- 构造子注入方式 -->
<constructor-arg index="0"><value>Welcome</value></constructor-arg>
<constructor-arg index="1"><value>yuping</value></constructor-arg>
<!-- 值注入方式 -->
<property name="msg" value="Hello World的Spring示例!"/>
</bean>
</beans>
(3) 测试类:
public class Test {
public static void main(String[] args) throws IOException {
Resource resource = new ClassPathResource("com/taobao /test/helloBean.xml");
BeanFactory factory = new XmlBeanFactory(resource);
HelloBean hello = (HelloBean) factory.getBean("hello");
System.out.println(hello.sayHelloToUser());
hello.sayHello();
System.out.println(resource.getURL());
}
}
[b]
输出[/b]:Welcometo:yuping
Hello World的Spring示例!
2.通过接口方式:
(1) 定义接口:DataBaseImp
public interface DataBaseImp {
public void getDate();
}
(2) 定义接口的三个实现类:
package com.taobao.test;
public interface DataBaseImp {
public void getDate();
}
(2) 定义接口的三个实现类:
public class MySql implements DataBaseImp {
public void getDate() {
System.out.println("调用MySql数据");
}
}
public class Oracle implements DataBaseImp {
public void getDate() {
System.out.println("调用oracle数据");
}
}
public class SqlServer implements DataBaseImp {
public void getDate() {
System.out.println("调用sqlserver数据");
}
}
(3) 定义注入类:
public class DataBaseIoc {
private DataBaseImp dataBase;
public void getDate() {
dataBase.getDate();
}
public void setDataBase(DataBaseImp dataBase) {
this.dataBase = dataBase;
}
}
(4) 测试类:
public class TestIoc {
public static void main(String[] args) {
DataBaseIoc baseIoc = new DataBaseIoc();
baseIoc.setDataBase(new MySql());
baseIoc.getDate();
}
}
输出:调用MySql数据
依赖注入:spring中通过依赖注入((Denpendency Injection)来实现IOC,依赖注入有三种实现方式:接口注入(Interface Injection)、值注入(setter Injection)、构造子注入(Constructor Injec)
下面看两个例子:
1. 使用xml配置:
(1) 构造HelloBean类:
package com.taobao.test;
public class HelloBean {
private String msg;
private String helloWorld;
private String user;
public HelloBean(String helloWorld, String user){
this.helloWorld = helloWorld; this.user = user;
}
public String sayHelloToUser() {
return helloWorld + "to:" + user;
}
public void sayHello() {
System.out.println(msg);
}
public void setMsg(String msg) {
this.msg = msg;
}
}
(2) xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="hello" class="com.taobao.test.HelloBean">
<!-- 构造子注入方式 -->
<constructor-arg index="0"><value>Welcome</value></constructor-arg>
<constructor-arg index="1"><value>yuping</value></constructor-arg>
<!-- 值注入方式 -->
<property name="msg" value="Hello World的Spring示例!"/>
</bean>
</beans>
(3) 测试类:
public class Test {
public static void main(String[] args) throws IOException {
Resource resource = new ClassPathResource("com/taobao /test/helloBean.xml");
BeanFactory factory = new XmlBeanFactory(resource);
HelloBean hello = (HelloBean) factory.getBean("hello");
System.out.println(hello.sayHelloToUser());
hello.sayHello();
System.out.println(resource.getURL());
}
}
[b]
输出[/b]:Welcometo:yuping
Hello World的Spring示例!
2.通过接口方式:
(1) 定义接口:DataBaseImp
public interface DataBaseImp {
public void getDate();
}
(2) 定义接口的三个实现类:
package com.taobao.test;
public interface DataBaseImp {
public void getDate();
}
(2) 定义接口的三个实现类:
public class MySql implements DataBaseImp {
public void getDate() {
System.out.println("调用MySql数据");
}
}
public class Oracle implements DataBaseImp {
public void getDate() {
System.out.println("调用oracle数据");
}
}
public class SqlServer implements DataBaseImp {
public void getDate() {
System.out.println("调用sqlserver数据");
}
}
(3) 定义注入类:
public class DataBaseIoc {
private DataBaseImp dataBase;
public void getDate() {
dataBase.getDate();
}
public void setDataBase(DataBaseImp dataBase) {
this.dataBase = dataBase;
}
}
(4) 测试类:
public class TestIoc {
public static void main(String[] args) {
DataBaseIoc baseIoc = new DataBaseIoc();
baseIoc.setDataBase(new MySql());
baseIoc.getDate();
}
}
输出:调用MySql数据