IOC and AOP

依赖注入方法拓展:
1.构造注入
通过有参的构造函数注入。
构造注入是一种高内聚的体现,特别是针对有些属性需要在对象在创建时候赋值,且后续不允许修改


        哈辛
        3
    

2.设值注入
通过setter访问器注入数据

   
        
        
        
    
    
        
        

    

实体类

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class Test{
	private String specialCharacter1; 
	private String specialCharacter2; 
	private User innerBean; 
	private List list; 
	private String[] array;
	private Set set; 
	private Map map; 
	private Properties props; 
	private String emptyValue; 
	private String nullValue = "init value";

	public void setSpecialCharacter1(String specialCharacter1) {
		this.specialCharacter1 = specialCharacter1;
	}

	public void setSpecialCharacter2(String specialCharacter2) {
		this.specialCharacter2 = specialCharacter2;
	}

	public void setInnerBean(User user) {
		this.innerBean = user;
	}

	public void setList(List list) {
		this.list = list;
	}
	
	public void setArray(String[] array) {
		this.array = array;
	}

	public void setSet(Set set) {
		this.set = set;
	}

	public void setMap(Map map) {
		this.map = map;
	}

	public void setProps(Properties props) {
		this.props = props;
	}

	public void setEmptyValue(String emptyValue) {
		this.emptyValue = emptyValue;
	}

	public void setNullValue(String nullValue) {
		this.nullValue = nullValue;
	}

	public void showValue() {
		System.out.println("特殊字符1:" + this.specialCharacter1);
		System.out.println("特殊字符2:" + this.specialCharacter2);
		System.out.println("内部Bean:" + this.innerBean.getUsername());
		System.out.println("List属性:" + this.list);
		System.out.println("数组属性[0]:" + this.array[0]);
		System.out.println("Set属性:" + this.set);
		System.out.println("Map属性:" + this.map);
		System.out.println("Properties属性:" + this.props);
		System.out.println("注入空字符串:[" + this.emptyValue + "]");
		System.out.println("注入null值:" + this.nullValue);
	}
}

测试类:

@Test
public  void TestEtity(){
    ApplicationContext context = new  ClassPathXmlApplicationContext ("spring_config.xml");
    Test test=(Test)context.getBean("entity");
    test.showValue();
}
 

你可能感兴趣的:(IOC and AOP)