IoC type 1 interface injection
要求实作接口 对象所在的容器也要实现这个接口
代码: |
public interface IDependencyInjection { public void createDependency(Map dependObjects); } public class BusinessObject implement IDependencyInjection { public void createDependency(Map dependObjects) { public void save() { |
太依赖容器 这样的耦合程度较高
IoC type 2 setter injection
例子程序
HelloBean.java
public class HelloBean
...{
private String helloworld;
public void setHelloworld(String helloworld)
...{
this.helloworld=helloworld;
}
public String getHelloworld()
...{
return this.helloworld;
}
}
SpringDemo.java
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
public class SpringDemo
...{
public static void main(String args[])
...{
Resource rs=new FileSystemResource("beans-config.xml");
BeanFactory factory=new XmlBeanFactory(rs);
HelloBean hello=(HelloBean)factory.getBean("hellobean");
System.out.println(hello.getHelloworld());
}
}
beans-config.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="hellobean"
class="com.ergal.spring.HelloBean">
<property name="helloworld">
<value>Hello! Rainytooo!!</value>
</property>
</bean>
</beans>
运行结果:
2006-9-27 5:18:34 org.springframework.core.CollectionFactory <clinit>
信息: JDK 1.4+ collections available
2006-9-27 5:18:35 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from file [D:/workspace/springdemo/beans-config.xml]
Hello! Rainytooo!!
IoC type 3 constructor injection
例子程序
HelloBean.java
public class HelloBean
...{
private String helloworld;
private String name;
public HelloBean()...{}
public HelloBean(String helloworld, String name)
...{
this.helloworld=helloworld;
this.name=name;
}
public void setHelloworld(String helloworld)
...{
this.helloworld=helloworld;
}
public String getHelloworld()
...{
return this.helloworld;
}
public void setName(String name)
...{
this.name=name;
}
public String getName()
...{
return this.name;
}
}
SpringDemo.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class SpringDemo
...{
public static void main(String args[])
...{
ApplicationContext context=new FileSystemXmlApplicationContext("beans-config.xml");
HelloBean hello=(HelloBean)context.getBean("hellobean");
System.out.println(hello.getHelloworld());
System.out.println(hello.getName());
}
}
beans-config.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="hellobean"
class="com.ergal.spring.HelloBean">
<constructor-arg index="0">
<value>Hi guy !</value>
</constructor-arg>
<constructor-arg index="1">
<value>Rainytooo</value>
</constructor-arg>
</bean>
</beans>
BeanFactory
BeanFactory 负责读取Bean定义文件 管理对象的加载 生成并维护Bean对象之间的依赖关系 负责生命周期
ApplicationContext
有更简洁的获取资源方法 提供文字消息的解析方法 支持国际化 可以发布事件 对事件感兴趣的bean 可以接受到这些事情
常使用的三个类
org.springframework.context.support.FileSystemXmlApplicationContext
制定路径来读取定义文件
org.springframework.context.support.ClassPathXmlApplicationContext
从classpath中来读取XML定义文件
org.springframework.web.context.support.XmlWebApplicationContext
在web应用中 指定位置来读取定义文件
Bean的基本管理
属性参考
直接参考一个已经定义的bean
用<bean> 里的<ref bean=" " />
例子程序
HelloBean.java
import java.util.Date;
public class HelloBean
...{
private String helloworld;
private String name;
private Date date;
public HelloBean()...{}
public HelloBean(String helloworld, String name)
...{
this.helloworld=helloworld;
this.name=name;
}
public void setHelloworld(String helloworld)
...{
this.helloworld=helloworld;
}
public String getHelloworld()
...{
return this.helloworld;
}
public void setName(String name)
...{
this.name=name;
}
public String getName()
...{
return this.name;
}
public Date getDate()
...{
return this.date;
}
public void setDate(Date date)
...{
this.date=date;
}
}
SpringDemo.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class SpringDemo
...{
public static void