现在的位置: 首页 > 综合 > 正文

Action 1 Foundation

2014年02月14日 ⁄ 综合 ⁄ 共 4984字 ⁄ 字号 评论关闭

IoC type 1 interface injection

要求实作接口 对象所在的容器也要实现这个接口

代码:
public interface IDependencyInjection {
    public void createDependency(Map dependObjects);
}

public class BusinessObject implement IDependencyInjection {
    private Map dependObjects;

    public void createDependency(Map dependObjects) {
        this.dependObject = dependObjects;
        // 在这边实现与BusinessObject的依赖关系 
        ......
    }

    public void save() {
        ....
        writer.saveToDevice();
    }
}

太依赖容器 这样的耦合程度较高

IoC type 2 setter injection

例子程序

HelloBean.java

package com.ergal.spring;

public class HelloBean 
{
    
private String helloworld;
    
public void setHelloworld(String helloworld)
    
{
        
this.helloworld=helloworld;
    }

    
public String getHelloworld()
    
{
        
return this.helloworld;
    }

}

SpringDemo.java

package com.ergal.spring;

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

<?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="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

package com.ergal.spring;

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

package com.ergal.spring;

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

<?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="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

package com.ergal.spring;

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

package com.ergal.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;



public class SpringDemo 
{
    
public static void

抱歉!评论已关闭.