问题的出现是因为有人问我,为什么他在学习Spring框架的时候,他在xml文件当中定义了一个Bean,最后在调用getBean()方法获取这个Bean的时候,必须转换成这个Bean对应的接口,而不能转换成这个接口的实现类。
我在网上一查,发现也有对应的问题,但是感觉对应的答案都不够正确,或者不够解答我的疑惑
现在我们开始重现这个问题
第一步:创建接口和对应的实现类
//对应接口
public interface ICar {
void move();
}
//对应的实现类
@Transactional
public class MyBenz implements ICar {
@Override
public void move() {
System.out.println("my car");
}
}
第二步:编写对应的配置文件
<?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:myname="http://www.example.org/schema/user" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.example.org/schema/user http://www.example.org/schema/user.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置HikariDataSource数据源 -->
<bean id = "dataSource" class = "com.zaxxer.hikari.HikariDataSource">
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3307/my_oracle?serverTimezone=GMT"></property>
</bean>
<!-- 定义事务 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" /><!-- ref:引入数据源 -->
</bean>
<!-- 配置 Annotation 驱动,扫描@Transactional注解的类定义事务启动事物注解 transaction-manager的值必须和上面这个bean的id一样-->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="false" />
<!--配置bean,class属性我们设置为实现类-->
<bean id="myBenz" class="com.fsl.springbootjunit.spring.test3.MyBenz"></bean>
</beans>
第三步:进行测试
public static void main(String[] args) {
ApplicationContext bf = new ClassPathXmlApplicationContext("spring/applicationContext-beans.xml");
MyBenz myBenz = (MyBenz)bf.getBean("myBenz");
myBenz.move();
}
结果如下,会出现java.lang.ClassCastException:
我们在配置文件当中配置的Bean的class属性明明就是com.fsl.springbootjunit.spring.test3.MyBenz,但是为什么我们getBean()的时候,转成对应的类型会报类型转换错误呢?
问题解答
首先我们要知道的是,Spring的AOP的实现底层是使用的代理模式,它就是通过创建对应的代理对象,通过对代理对象的方法运行前后进行处理来达到目的。我们使用的事务也是如此,给创建一个代理对象,在这个方法运行之前,Spring帮助开启事务,方法运行之后,Spring帮助我们提交事务或者回滚事务。
Spring创建代理对象的代码如下:
@Override
public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
//在创建代理对象之前进行一系列判断,来决定是使用JDK代理还是CGLIB代理
//isOptimize:使用cglib代理是否使用激进的优化策略
//isProxyTargetClass:是否对目标本身进行代理,而不是对目标类的接口进行代理
//hasNoUserSuppliedProxyInterfaces:是否不存在代理接口
if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {
Class<?> targetClass = config.getTargetClass();
if (targetClass == null) {
throw new AopConfigException("TargetSource cannot determine target class: " +
"Either an interface or a target is required for proxy creation.");
}
if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
return new JdkDynamicAopProxy(config);
}
return new ObjenesisCglibAopProxy(config);
}
else {
return new JdkDynamicAopProxy(config);
}
}
在创建对象开始之前,会进行判断来决定使用JDK动态代理还是CGLIB动态代理,因为这两种代理模式对代理对象的要求不同,比如JDK动态代理就要求被代理的对象实现了某个接口。
其中有一个属性很重要,isProxyTargetClass,这个就用来判断,是否对目标本身进行代理,而这个属性,默认是false
现在我们就能够知道,原来代理对象在生成的过程当中,是去找到被代理对象的接口,依据那个接口来生成的对象,而不是依据目标本身生成的。
ApplicationContext bf = new ClassPathXmlApplicationContext("spring/applicationContext-beans.xml");
Object myBenz = bf.getBean("myBenz");
//这里返回true,因为是依据这个接口生成的代理对象
System.out.println(myBenz instanceof ICar);
//这里返回false,因为这个是代理目标类,但是Spring默认的不以这个去生成代理对象
System.out.println(myBenz instanceof MyBenz);
那假如我们就想以目标类去生成代理对象呢?
通过上面的分析,大家肯定也能够回答出来,我把proxyTargetClass属性设置为true就可以了。
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
在执行下面的这个代码,我们就能够看到对应的效果
ApplicationContext bf = new ClassPathXmlApplicationContext("spring/applicationContext-beans.xml");
Object myBenz = bf.getBean("myBenz");
//返回true
System.out.println(myBenz instanceof ICar);
//返回true
System.out.println(myBenz instanceof MyBenz);