
依赖注入是指运行时将容器内对象利用反射赋给其他对象的操作。
基于setter方法注入对象
基于构造方法注入对象

将Apple实体类的setTitle修改为
public void setTitle(String title) {
System.out.println("title属性设置为:"+title);
this.title = title;
}
然后在applicationContext.xml中添加配置为
<!--在IoC容器启动时,自动由Spring实例化Apple对象,取名sweetApple放入到容器中-->
<bean id="sweetApple" class="com.ql.spring.ioc.entity.Apple">
<!--IoC容器自动利用反射机制运行时调用setXXX方法为属性赋值-->
<property name="title" value="红富士"/>
<property name="origin" value="欧洲"/>
<property name="color" value="红色"/>
</bean>
最后从IoC容器中获取对象
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
Apple sweetApple = context.getBean("sweetApple", Apple.class);
System.out.println(sweetApple.getTitle());
/*
title属性设置为:红富士
红富士
*/

将Apple、Child实体类修改为
public Apple() {
System.out.println("Apple对象已创建,"+this);
}
public Child() {
System.out.println("正在创建Child对象:"+this);
}
public void setApple(Apple apple) {
System.out.println("注入的Apple对象:"+apple);
this.apple = apple;
}
然后在applicationContext.xml中添加配置为
<bean id="lily" class="com.ql.spring.ioc.entity.Child">
<property name="name" value="莉莉"/>
<!--利用ref注入依赖对象-->
<property name="apple" ref="sweetApple"/>
</bean>
最后从IoC容器中获取对象
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
Child lily = context.getBean("lily", Child.class);
/*
Apple对象已创建,com.ql.spring.ioc.entity.Apple@4bec1f0c
正在创建Child对象:com.ql.spring.ioc.entity.Child@6c3708b3
注入的Apple对象:com.ql.spring.ioc.entity.Apple@4bec1f0c
*/
首先保证Child实体类有带参构造方法
public Child(String name, Apple apple) {
System.out.println("构造方法参数apple:"+apple);
this.name = name;
this.apple = apple;
}
然后在applicationContext.xml中添加配置为
<bean id="sourApple" class="com.ql.spring.ioc.entity.Apple">
<property name="title" value="青苹果"/>
<property name="origin" value="中亚"/>
<property name="color" value="绿色"/>
</bean>
<bean id="andy" class="com.ql.spring.ioc.entity.Child">
<constructor-arg name="name" value="安迪"/>
<constructor-arg name="apple" ref="sourApple"/>
</bean>
最后从IoC容器中获取对象
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
Child andy = context.getBean("andy", Child.class);
andy.eat();
/**
Apple对象已创建,com.ql.spring.ioc.entity.Apple@67784306
title属性设置为:青苹果
构造方法参数apple:com.ql.spring.ioc.entity.Apple@67784306
安迪吃到了中亚种植的青苹果
*/
打开IDEA创建一个新的maven工程

打开pom.xml添加spring依赖
<repositories>
<repository>
<id>aliyun</id>
<name>aliyun</name>
<url>https://maven.aliyun.com/repository/public</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
</dependencies>
在com.ql.spring.ioc.bookshop.dao包下创建BookDao接口和它的实现类BookDaoImpl
package com.ql.spring.ioc.bookshop.dao;
public interface BookDao {
public void insert();
}
package com.ql.spring.ioc.bookshop.dao;
public class BookDaoImpl implements BookDao{
@Override
public void insert() {
System.out.println("向MySQL Book表插入一条数据");
}
}
在com.ql.spring.ioc.bookshop.service包下创建BookService业务类
package com.ql.spring.ioc.bookshop.service;
import com.ql.spring.ioc.bookshop.dao.BookDao;
public class BookService {
private BookDao bookDao;
public void purchase(){
System.out.println("正在执行图书采购业务方法");
bookDao.insert();
}
public BookDao getBookDao() {
return bookDao;
}
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
}
在resources下创建两个配置文件applicationContext-dao.xml
<?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.xsd">
<bean id="bookDao" class="com.ql.spring.ioc.bookshop.dao.BookDaoImpl">
</bean>
</beans>
applicationContext-service.xml
<?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.xsd">
<bean id="bookService" class="com.ql.spring.ioc.bookshop.service.BookService">
<!--id=bookDao-->
<property name="bookDao" ref="bookDao"/>
</bean>
</beans>
最后在com.ql.spring.ioc.bookshop包创建应用入口类BookShopApplication
package com.ql.spring.ioc.bookshop;
import com.ql.spring.ioc.bookshop.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BookShopApplication {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext-*.xml");
BookService bookService = context.getBean("bookService", BookService.class);
bookService.purchase();
}
}
/*
正在执行图书采购业务方法
向MySQL Book表插入一条数据
*/
如果当前应用把MySQL数据库更换为Oracle该怎么变动?
在com.ql.spring.ioc.bookshop.dao包下创建Oracle数据库实现类BooKDaoOracleImpl
package com.ql.spring.ioc.bookshop.dao;
public class BooKDaoOracleImpl implements BookDao{
@Override
public void insert() {
System.out.println("向Oracle Book表插入一条数据");
}
}
之后需要变化的地方只有一个,那就是applicationContext-dao.xml配置文件里把原有MySQL实现类替换为Oracle实现类
<?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.xsd">
<bean id="bookDao" class="com.ql.spring.ioc.bookshop.dao.BooKDaoOracleImpl">
</bean>
</beans>
<!--
正在执行图书采购业务方法
向Oracle Book表插入一条数据
-->
这里展示了通过Spring IoC控制反转容器对对象之间解耦,导致开发数据库交互层人员和业务层人员的解耦,这对大型软件开发公司非常重要,可以节约很多时间成本的节约。