使用构造方法注入和setter注入的配置文件

复制代码
<?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"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

    <bean id="test" class="dao.TestDaoImpl" />

    <!-- 将指定类DIServiceImpl配置给spring,让spring创建其实例 -->
    <bean id="myTestDao" class="dao.TestDaoImpl"></bean>
    <!-- 使用构造方法注入 -->
    <bean id="diService" class="service.DIServiceImpl">
        <!-- 将myTestDao注入到DIServiceImpl类的属性ITestDao上 -->
        <constructor-arg index="0" ref="myTestDao" />
    </bean>

    <!-- 使用setter方法的注入 -->
    <bean id="testDIService1" class="service.DIServiceImpl1">
        <!-- 调用DIServiceImpl1类的setter方法,将myTestDao注入到DIServiceImpl1类的属性iTestDao上 -->
        <property name="iTestDao" ref="myTestDao" />
    </bean>

</beans>
复制代码

构造方法实现类:

复制代码
package service;
/**
 * IDIService接口的实现类
 *
 * Title: DIServiceImpl
 *
 * Description: 
 *
 * @author Ethan
 *
 * @date 2019年6月25日
 *
 */

import dao.ITestDao;

public class DIServiceImpl implements IDIService{
    private ITestDao iTestDao;
    //构造方法,用于实现依赖注入接口对象iTestDao
    public DIServiceImpl(ITestDao iTestDao) {
        super();
        this.iTestDao = iTestDao;
    }
    public void sayHello() {
        //调用iTestDao中的sayHello方法
        iTestDao.sayHello();
        System.out.println("IDIService构造方法注入");
    }
    
}
复制代码

setter方式实现类:

复制代码
package service;

import dao.ITestDao;

public class DIServiceImpl1 implements IDIService{
    private ITestDao iTestDao;
    //添加iTestDao的setter方法,用于实现依赖注入

    public ITestDao getiTestDao() {
        return iTestDao;
    }

    public void setiTestDao(ITestDao iTestDao) {
        this.iTestDao = iTestDao;
    }

    public void sayHello() {
        // TODO Auto-generated method stub
        //调用iTestDao中的sayHello方法
        iTestDao.sayHello();
        System.out.println("TestDaoImpl setter方法注入");
    }
    
    
}
复制代码

测试类:

复制代码
package test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import service.IDIService;

public class Tests2 {
    
    /**
     * 构造方法注入测试类
     *
     * Title: Test1
     *
     * Description: 
     * 
     *
     */
    @Test
    public void Test1() {
        ApplicationContext appCon = new ClassPathXmlApplicationContext("applicationContext.xml");
        IDIService ts = (IDIService) appCon.getBean("diService");
        ts.sayHello();
    }
    
    /**
     * 使用setter方式注入测试类
     *
     * Title: Test2
     *
     * Description: 
     * 
     *
     */
    @Test
    public void Test2() {
        ApplicationContext appCon = new ClassPathXmlApplicationContext("applicationContext.xml");
        IDIService ts = (IDIService) appCon.getBean("testDIService1");
        ts.sayHello();
    }
}
复制代码

 

posted @   WaterGe  阅读(1101)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
点击右上角即可分享
微信分享提示