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

Posted 言凡

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring接口编程_设值注入和构造注入相关的知识,希望对你有一定的参考价值。

说明:

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接口编程_设值注入和构造注入的主要内容,如果未能解决你的问题,请参考以下文章

Spring学习总结

Spring核心概念

依赖注入的实现方式:设值注入和构造方法注入

Java Spring学习笔记----Bean的依赖注入

spring框架IOC设值注入

spring的注入方式有几种