Spring设置注入-set方法
实体类domain
package cn. zhc. domain;
public class EmpInfo {
private int empid;
private String empname;
private String empsex;
private Address address;
public int getEmpid ( ) {
return empid;
}
public void setEmpid ( int empid) {
this . empid = empid;
}
public String getEmpname ( ) {
return empname;
}
public void setEmpname ( String empname) {
this . empname = empname;
}
public String getEmpsex ( ) {
return empsex;
}
public void setEmpsex ( String empsex) {
this . empsex = empsex;
}
public Address getAddress ( ) {
return address;
}
public void setAddress ( Address address) {
this . address = address;
}
public EmpInfo ( int empid, String empname, String empsex, Address address) {
this . empid = empid;
this . empname = empname;
this . empsex = empsex;
this . address = address;
}
public EmpInfo ( ) {
}
}
package cn. zhc. domain;
public class Address {
private String homeAddress;
private String schoolAddress;
public String getHomeAddress ( ) {
return homeAddress;
}
public void setHomeAddress ( String homeAddress) {
this . homeAddress = homeAddress;
}
public String getSchoolAddress ( ) {
return schoolAddress;
}
public void setSchoolAddress ( String schoolAddress) {
this . schoolAddress = schoolAddress;
}
public Address ( String homeAddress, String schoolAddress) {
this . homeAddress = homeAddress;
this . schoolAddress = schoolAddress;
}
public Address ( ) {
}
}
接口dao
package cn. zhc. dao;
public interface EmpInfoDao {
public void addEmp ( ) ;
}
实现类Impl
package cn. zhc. dao. Impl;
import cn. zhc. dao. EmpInfoDao;
public class EmpInfoDaoImpl implements EmpInfoDao {
@Override
public void addEmp ( ) {
System. out. println ( "迪丽迪丽热巴呀" ) ;
}
}
Service层
package cn. zhc. service;
import cn. zhc. dao. EmpInfoDao;
public class EmpService {
private EmpInfoDao empInfoDao;
public EmpInfoDao getEmpInfoDao ( ) {
return empInfoDao;
}
public void setEmpInfoDao ( EmpInfoDao empInfoDao) {
this . empInfoDao = empInfoDao;
}
public void addEmp ( ) {
empInfoDao. addEmp ( ) ;
}
}
action层
package cn. zhc. action;
import cn. zhc. service. EmpService;
public class EmpAction {
private EmpService empService;
public EmpService getEmpService ( ) {
return empService;
}
public void setEmpService ( EmpService empService) {
this . empService = empService;
}
public String execute ( ) {
empService. addEmp ( ) ;
return null;
}
}
XML文件
<?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: context= " http://www.springframework.org/schema/context"
xsi: schemaLocation= " http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd" >
< bean id = " address" class = " cn.zhc.domain.Address" >
< property name = " homeAddress" value = " 淅川" > </ property>
< property name = " schoolAddress" value = " 郑州" > </ property>
</ bean>
< bean id = " empInfo" class = " cn.zhc.domain.EmpInfo" >
< property name = " empid" value = " 1000" > </ property>
< property name = " empname" value = " az" > </ property>
< property name = " empsex" value = " 男" > </ property>
< property name = " address" ref = " address" > </ property>
</ bean>
< bean id = " empInfoDao" class = " cn.zhc.dao.Impl.EmpInfoDaoImpl" > </ bean>
< bean id = " empService" class = " cn.zhc.service.EmpService" autowire = " byType" > </ bean>
< bean id = " empAction" class = " cn.zhc.action.EmpAction" autowire = " byType" > </ bean>
</ beans>
测试类
package cn. zhc. test;
import cn. zhc. action. EmpAction;
import cn. zhc. domain. EmpInfo;
import cn. zhc. service. EmpService;
import org. springframework. context. ApplicationContext;
import org. springframework. context. support. ClassPathXmlApplicationContext;
public class Test {
public static void main ( String[ ] args) {
test4 ( ) ;
}
private static void test1 ( ) {
ApplicationContext ac = new ClassPathXmlApplicationContext ( "spring.xml" ) ;
EmpInfo empInfo= ( EmpInfo) ac. getBean ( "empInfo" ) ;
System. out. println ( empInfo. getAddress ( ) . getHomeAddress ( ) ) ;
}
private static void test2 ( ) {
ApplicationContext ac = new ClassPathXmlApplicationContext ( "spring.xml" ) ;
EmpService es = ( EmpService) ac. getBean ( "empService" ) ;
es. addEmp ( ) ;
}
private static void test3 ( ) {
ApplicationContext ac = new ClassPathXmlApplicationContext ( "spring.xml" ) ;
EmpService es1 = ( EmpService) ac. getBean ( "empService" ) ;
EmpService es2 = ( EmpService) ac. getBean ( "empService" ) ;
System. out. println ( es1== es2) ;
}
private static void test4 ( ) {
ApplicationContext ac = new ClassPathXmlApplicationContext ( "spring.xml" ) ;
EmpAction ea = ( EmpAction) ac. getBean ( "empAction" ) ;
ea. execute ( ) ;
}
}
运行结果: