首页 > 其他 > 详细

Spring的DI(Ioc) - 利用构造器注入

时间:2014-02-21 20:28:56      阅读:337      评论:0      收藏:0      [点我收藏+]

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"?>
       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: 测试即可。

Spring的DI(Ioc) - 利用构造器注入

原文:http://www.cnblogs.com/E-star/p/3559008.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!