控制反转——Spring通过一种称作控制反转(IoC)的技术促进了松耦合。当应用了IoC,一个对象依赖的其它对象会通过被动的方式传递进来,而不是这个对象自己创建或者查找依赖对象。你可以认为IoC与JNDI相反——不是对象从容器中查找依赖,而是容器在对象初始化时不等对象请求就主动将依赖传递给它。
IOC
高层模块不应该依赖低层模块,而是模块都必须依赖于抽象.
业务与抽象结合,不依赖于实现.
通用的Bean工厂,通过各种方式获得Bean对象以及Bean对象的管理。
接口Dao.java
package bean;
public interface Dao {
public void save();
public void delete();
public void update();
}
package bean;
public class UserDao implements Dao {
@Override
public void save() {
System.out.println("user Save");
}
@Override
public void delete() {
System.out.println("user delete");
}
@Override
public void update() {
System.out.println("user update");
}
}
package bean;
public class BoardDao implements Dao {
@Override
public void save() {
System.out.println("board Save");
}
@Override
public void delete() {
System.out.println("board delete");
}
@Override
public void update() {
System.out.println("board update");
}
}
StaticFactory.java
package bean;
public class StaticFactory {
//静态工厂
public static Dao getDao(String name){
//統一管理 如读取资源,记录日志,事务管理
if(name.equals("user")){
return new UserDao();
}else{
return new BoardDao() ;
}
}
//普通工厂
public Dao getComDao(String name){
//统一管理
if(name.equals("user")){
return new UserDao();
}else{
return new BoardDao() ;
}
}
}
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<!-- 静态工厂 -->
<bean name="dao" class="bean.StaticFactory" factory-method="getDao">
<constructor-arg>
<value>user</value>
</constructor-arg>
</bean>
<!-- 普通工厂 -->
<!-- Dao dao=(Dao) ctx.getBean("userDao"); -->
<bean name="factory" class="bean.StaticFactory"></bean>
<bean name="userDao" factory-bean="factory" factory-method="getComDao">
<constructor-arg>
<value>board</value>
</constructor-arg>
</bean>
</beans>
测试代码
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
Dao dao=StaticFactory.getDao("board");//通过静态工厂
// Dao dao=(Dao) ctx.getBean("userDao");//通过普通工厂
dao.update();
Bean的生命周期
在UserDao中加上两个方法
public void init(){
System.out.println("useDao被初始化");
}
public void destory(){
System.out.println("useDao被销毁");
}
<!-- 声明周期 -->
<bean name="user1" class="bean.UserDao" init-method="init"
destroy-method="destory">
</bean>
package util;
public class Topic {
private String id;
private String title;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
package util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class User {
private String id;
private String name;
private List list=new ArrayList();
private Set sets=new HashSet();
private Map maps=new HashMap();
private Properties pros=new Properties();
String[] strs;
Topic[] topics;//对象数组
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Set getSets() {
return sets;
}
public void setSets(Set sets) {
this.sets = sets;
}
public Map getMaps() {
return maps;
}
public void setMaps(Map maps) {
this.maps = maps;
}
public Properties getPros() {
return pros;
}
public void setPros(Properties pros) {
this.pros = pros;
}
public String[] getStrs() {
return strs;
}
public void setStrs(String[] strs) {
this.strs = strs;
}
public Topic[] getTopics() {
return topics;
}
public void setTopics(Topic[] topics) {
this.topics = topics;
}
}
<!-- 装配各种类型对象 -->
<bean name="topic1" class="util.Topic">
<property name="id">
<value>1</value>
</property>
<property name="title">
<value>java</value>
</property>
</bean>
<bean name="topic2" class="util.Topic">
<property name="id">
<value>2</value>
</property>
<property name="title">
<value>c#</value>
</property>
</bean>
<bean name="user" class="util.User">
<property name="id">
<value>1</value>
</property>
<property name="name">
<value>csdn</value>
</property>
<property name="list">
<list>
<ref bean="topic1" />
<ref bean="dao" />
<value>123</value>
<value>465</value>
<value></value>
<null></null>
</list>
</property>
<property name="sets">
<set>
<bean id="date" class="java.util.Date"></bean>
<ref bean="topic2" />
</set>
</property>
<property name="maps">
<map>
<entry>
<key>
<value>java</value>
</key>
<ref bean="topic1" />
</entry>
<entry>
<key>
<value>sql</value>
</key>
<value>oracle</value>
</entry>
</map>
</property>
<property name="pros">
<props>
<prop key="java">
hibernate
</prop>
<prop key="jsp">
struts
</prop>
</props>
</property>
<property name="strs">
<list>
<value>javax</value>
<value>jspx</value>
</list>
</property>
<property name="topics">
<array>
<ref bean="topic1" />
<ref bean="topic2" />
</array>
</property>
</bean>
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
Topic dao=(Topic) ctx.getBean("topic1");
System.out.println(dao.getId()+"--"+dao.getTitle());
//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
Topic dao1=(Topic) ctx.getBean("topic2");
System.out.println(dao1.getId()+"--"+dao1.getTitle());
User user=(User) ctx.getBean("user");
System.out.println(user.getId()+"-user-"+user.getName());
for(int i=0;i<user.getList().size();i++){
System.out.println(user.getList().get(i));
}
Set us=user.getSets();
Iterator iterator=us.iterator();
while(iterator.hasNext()){
System.out.println("set集合"+iterator.next());
}
Map map=user.getMaps();
Set set=map.keySet();
Iterator iterator2=set.iterator();
while(iterator2.hasNext()){
String k=(String) iterator2.next();
System.out.println("map集合"+k);
System.out.println("map集合"+map.get(k));
}
System.out.println("pros:"+user.getPros());
//System.out.println(user.getStrs());
String s[]=user.getStrs();
for(String x:s){
System.out.println("strs:"+x);
}
Topic[] sx=user.getTopics();
for(Topic xx:sx){
System.out.println("topics:"+xx);
}
原文:http://blog.csdn.net/yantingmei/article/details/21868119