<?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(); } }
原文地址:https://www.cnblogs.com/WaterGe/p/11267164.html
时间: 2024-10-04 01:11:31