码迷,mamicode.com
首页 > 编程语言 > 详细

Spring接口编程_设值注入和构造注入

时间:2017-01-15 17:54:02      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:rda   sys   style   get   xmlns   pre   use   case   接口编程   

说明:

UserManagerImp是设值注入,UserManagerImp2是构造注入

接口不注入,也就是在Spring配置文件中没有接口的<bean>,但是定义的时候是用接口

private IUserDao userDao;
IUserManager userManager2=(IUserManager) factory.getBean("userManager2");

 

类都注入,①UserManagerImp和UserManagerImp2属于实现类

     ②UserDaoImp是Dao类也要注入

     前者①中有属性userDao,userDao可以通过设值和构造俩中方法注入

     后者②中没有属性,所以只写了<bean>

1.文件

技术分享

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <bean name="userManager" class="com.wangcf.manager.UserManagerImp">
        <!--设值注入   -->
        <property name="userDao" ref="userDao"></property>
    </bean>
    <bean name="userManager2" class="com.wangcf.manager.UserManagerImp2">
        <!-- 构造注入 -->
        <constructor-arg ref="userDao"/>
    </bean>
        
    <bean name="userDao" class="com.wangcf.Dao.UserDaoImp"></bean>  
</beans>

 

2.Dao

package com.wangcf.Dao;

public interface IUserDao {
    public void addUser();
}
package com.wangcf.Dao;

public class UserDaoImp implements IUserDao{
    public void addUser(){
        System.out.println("UserDaoImp addUser...");
    }
}

3.manager

package com.wangcf.manager;

public interface IUserManager {
    public void addUser();
}
package com.wangcf.manager;

import com.wangcf.Dao.IUserDao;

public class UserManagerImp implements IUserManager{
    
    private IUserDao userDao;
    //设值注入
    public void setUserDao(IUserDao userDao) {
        this.userDao = userDao;
    }
    public void addUser(){
        System.out.println("UserManagerImp  addUser...");
        this.userDao.addUser();
    }


}
package com.wangcf.manager;

import com.wangcf.Dao.IUserDao;

public class UserManagerImp2 implements IUserManager{

    private IUserDao userDao;
    
    //构造注入
    public UserManagerImp2(IUserDao userDao) {
        // TODO Auto-generated constructor stub
        this.userDao=userDao;
    }
    
    @Override
    public void addUser() {
        // TODO Auto-generated method stub
        System.out.println("UserManagerImp2 addUser...");
        this.userDao.addUser();
    }
    
}

4.test

package com.wangcf.test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.wangcf.manager.IUserManager;

import junit.framework.TestCase;

public class UserManagerTest extends TestCase{
    
    public void testAddUser(){
        BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml");
        IUserManager userManager=(IUserManager) factory.getBean("userManager");
        userManager.addUser();
        System.out.println("=============");
        IUserManager userManager2=(IUserManager) factory.getBean("userManager2");
        userManager2.addUser();
    }
}

5.输出

技术分享

 

Spring接口编程_设值注入和构造注入

标签:rda   sys   style   get   xmlns   pre   use   case   接口编程   

原文地址:http://www.cnblogs.com/-beauTiFul/p/6287381.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!