1: 在给对象提供构造器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 |
public
class PersonServiceImpl implements PersonService { private
PersonDao personDao; private
String name; public
PersonServiceImpl(PersonDao personDao, String name) { super(); this .personDao = personDao; this .name = name; } public
void save() { personDao.save(); System. out .println( "name = "
+ name); System. out .println( "service : "
+ " save 方法" ); } } |
2: 配置XML文件:
1
2
3
4
5
6
7
8
9
10
11
12
13 |
<?xml version= "1.0"
encoding= "UTF-8" ?> <beans xmlns= "http://www.springframework.org/schema/beans" xsi:schemaLocation="http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id= "personDaoImpl"
class = "cn.gbx.dao.PersonDaoImpl" ></bean> <bean id= "personServiceImpl"
class = "cn.gbx.serviceimpl.PersonServiceImpl"
> <constructor-arg index= "0"
type= "cn.gbx.daoimpl.PersonDao"
ref = "personDaoImpl" > </constructor-arg> <constructor-arg index= "1"
value= "Myname" ></constructor-arg> </bean> </beans> |
3: 测试即可。
原文:http://www.cnblogs.com/E-star/p/3559008.html