下图是service层的一个类,类中的方法调用了代理类对象accountMapper调用了dao层的相关方法
package com.itheima.service;
import com.itheima.dao.AccountMapper;
import com.itheima.pojo.Account;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("accountServiceImpl")
public class AccountServiceImpl implements AccountService {
private AccountMapper accountMapper;
public void setAccountMapper(AccountMapper accountMapper) {
this.accountMapper = accountMapper;
}
public void save(Account account) {
accountMapper.save(account);
}
public void update(Account account) {
accountMapper.update(account);
}
public void delete(Integer id) {
accountMapper.delete(id);
}
public Account findById(Integer id) {
Account account = accountMapper.findById(id);
return account;
}
public List<Account> findAll() {
List<Account> list = accountMapper.findAll();
return list;
}
}
而在spring 的核心配置文件中,没有明显的对该动态代理类创建对象,在配置service对象的相关信息时,对其属性accountMapper的依赖注入,关联引用的直接是“accountMapper”该对象,但配置文件中并没有对该对象进行配置,但代码却可以正常运行,其原因是什么呢?
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--service层对象,其中的方法需要用mapper对象调用,在该对象中注入依赖-->
<bean id="accountServiceImpl" scope="prototype" class="com.itheima.service.AccountServiceImpl">
<!--此处的ref="accountMapper"中 accountMapper并没有配置相关创建对象的方式,但仍可以正常运行的原因是因为我们
配置了MapperScannerConfigurer这个spring提供的可以自动为dao层创建动态代理类的相关配置信息,该配置使得spring
在加载时,会自动读取需要扫描的包中的Mapper类,并创建代理类对象存入IOC容器中供取用,默认的取用id为首字母小写的该映
射接口的类名称
-->
<property name="accountMapper" ref="accountMapper"></property>
</bean>
<!--配置解析properties配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--配置创建数据源,并存放到IOC容器中-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!--注入数据配置连接池信息-->
<property name="url" value="${jdbc.url}"></property>
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--spring整合mybatis后提供的工厂bean对象,将创建好的工厂对象放入IOC容器中-->
<bean id="factoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--创建工厂类对象时,需要依赖数据源-->
<property name="dataSource" ref="dataSource"></property>
<!--配置包别名-->
<property name="typeAliasesPackage" value="com.itheima.pojo"></property>
</bean>
<!--配置创建AccountMapper接口的代理类对象-->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--扫描mapper接口所在的包,自动创建接口的代理类对象,并将其放入IOC容器 -->
<property name="basePackage" value="com.itheima.dao"></property>
</bean>
</beans>
主要原因在于该配置
<!--配置创建AccountMapper接口的代理类对象-->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--扫描mapper接口所在的包,自动创建接口的代理类对象,并将其放入IOC容器 -->
<property name="basePackage" value="com.itheima.dao"></property>
</bean>
在spring的配置文件中,并没有为accounMapper配置相关创建对象的方式,但仍可以正常运行的原因是因为我们配置了MapperScannerConfigurer这个spring提供的可以自动为dao层创建动态代理类的相关配置信息,该配置使得spring在加载时,自动读取需要扫描的包中的映射接口,并创建对应的代理类对象存入IOC容器中供取用,默认的key(即该对象存在IOC容器中的id)为该映射接口首字母小写的的类名称。