Spring配置优化_构造器注入+自动装配

2014-05-16 09:01:08上课内容:
依赖注入的第二种注入方式:构造器注入 

创建带参数的构造方法,参数类型为注入类的类型

项目要先添加Spring支持;

package com;

public class Computer {
	private Host host;
	private Display display;

	//public Computer(){}
	public Computer(Host host, Display display) {
		this.host = host;
		this.display = display;
	}

	public void run() {
		System.out.println(host.run() + "; " + display.run());
	}
	/*public void setHost(Host host) {
		this.host = host;
	}
	public void setDisplay(Display display) {
		this.display = display;
	}*/

}

package com;

public class Display {
	public String run(){
		return "我是显示器,我在运行";
	}
}

package com;

public class Host {
	public String run() {
		return "我是主机,我在运行";
	}

}




	
	
	
	 
		
		
		
		
		
		
			
		
		
	

TestComputer

package com;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestComputer {
	@Test
	public void testRun(){
		ApplicationContext ac =new ClassPathXmlApplicationContext("applicationContext.xml");
		Computer computer = (Computer) ac.getBean("computer");
		computer.run();
	}
}
自动装配:Spring可以自动根据属性类型、名称等进行注入
autowire属性可以设置为no、byType或byName
 
byName 一个都没找到,不报错;采用byName方式,将根据属性名称在Spring Bean Factory中找,找到即自动注入,否则,什么都不做
byType 找到一个以上报错;
Spring提供了依赖检查功能
default-dependency-check属性 spring3.0以后没有了;

package com;

public class Computer {
	private Host host;
	private Display display;

	public Computer(){}
	public Computer(Host host, Display display) {
		this.host = host;
		this.display = display;
	}

	public void run() {
		System.out.println(host.run() + "; " + display.run());
	}
	public void setHost(Host host) {
		this.host = host;
	}
	public void setDisplay(Display display) {
		this.display = display;
	}

}

package com;

public class Display {
	public String run(){
		return "我是显示器,我在运行";
	}
}

package com;

public class Host {
	public String run() {
		return "我是主机,我在运行";
	}

}



	
	
	
	
	
	
	 
	
	
	
	
		
	

package com;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestComputer {
	@Test
	public void testRun(){
		ApplicationContext ac =new ClassPathXmlApplicationContext("applicationContext.xml");
		Computer computer = (Computer) ac.getBean("computer");
		computer.run();
	}
}

拆分配置文件:
新建Dao Service Action的配置文件,修改web.xml使用通配符*;
测试类测试 EmployeeServiceTest  

拆分配置文件两种方法
1.配制Spring集成时:配制ContextLoadListener的contextConfigLocation属性,配置多个配置文件用,逗号隔开;或者使用通配符
2.在公用配置文件使用方式





	
		
	
	
	
	
		
	
	
	
		
			
			 
			 
			 
			 
			 
		
	
	
	
		
		
		
         
	
	
	
	
	
	
	
	
	

web.xml



	
	
		login.jsp
	
	
	
		contextConfigLocation
		
		classpath:*ApplicationContext.xml
		
		
	

	
		org.springframework.web.context.ContextLoaderListener
	
	
	
		struts2
		org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
	
	
		struts2
		/*
	

然后执行测试类测试:

package com.jboa.service;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jboa.model.Department;
import com.jboa.model.Employee;
import com.jboa.model.Postion;

public class EmployeeServiceTest {
	@Test
	public void testAdd() {
		ApplicationContext ac = new ClassPathXmlApplicationContext("/*ApplicationContext.xml");
		EmployeeService employeeService = (EmployeeService) ac.getBean("employeeService");
		Employee employee = new Employee();
		employee.setSn("user111111");
		employee.setPassword("user111111");
		employee.setStatus("1");
		employee.setName("user111111");
		Postion p = new Postion();
		p.setId(2);
		employee.setPostion(p);
		Department d = new Department();
		d.setId(1);
		employee.setDepartment(d);
		employeeService.add(employee);
	}
}


你可能感兴趣的:(JavaEE)