创建带参数的构造方法,参数类型为注入类的类型
项目要先添加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 "我是主机,我在运行";
}
}
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可以自动根据属性类型、名称等进行注入
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();
}
}
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);
}
}