- 浏览: 1517132 次
- 性别:
- 来自: 厦门
博客专栏
-
Spring 3.x企业实...
浏览量:461547
文章分类
最新评论
-
JyeChou:
学习Spring必学的Java基础知识(1)----反射 -
hhzhaoheng:
...
《Spring4.x企业应用开发实战》光盘资料下载 -
renlongnian:
//assertReflectionEquals(user1, ...
单元测试系列之3:测试整合之王Unitils -
骑着蜗牛超F1:
huang_yong 写道我的经验是,只需定义三层:1.ent ...
Spring的事务管理难点剖析(2):应用分层的迷惑 -
wangyudong:
工具地址貌似更新了哦https://github.com/Wi ...
几种常用的REST webservice客户端测试工具
Case
请看下面的IOC实例:
1)AaaService实现AaaaInterface接口
2)在BaaService中Autowired AaaService
Code
Spring XML配置
启动Spring容器
结果报了以下这堆异常:
上面的异常告诉我们,BbbService Bean创建不了,因为无法Autowired其aaaService的成员变量,说在Spring容器中不存在这个AaaService的Bean.
分析
以上是Spring IoC中一个经典的错误,其原因是Spring对Bean的动态代理引起的:
1)由于在applicationContext.xml中,通过<aop>对所有Service进行事务增强,因此Spring容器会对所有所有XxxService的Bean进行动态代理;
2)默认情况下,Spring使用基于接口的代理,也即:
a)如果Bean类有实现接口,那么Spring自动使用基于接口的代理创建该Bean的代理实例;
b)如果Bean类没有实现接口,那么则使用基于子类扩展的动态代理(即CGLib代理);
3)由于我们的AaaService实现了AaaInterface,所以Spring在生成AaaService类的动态代理Bean时,采用了
基于接口的动态代理,该动态代理实例只实现AaaInterface,且该实例不能强制转换成AaaService。换句话说,AaaService生成的Bean不能赋给AaaService的实例,而只能赋给AaaInterface的实例。
4)因此,当BbbService希望注入AaaService的成员时,Spring找不到对应的Bean了(因为只有AaaInterface的Bean).
解决
方法1
既然AaaService Bean是被Spring动态代理后改变成了类型,那如果取消引起动态代理的配置,使Spring不会对AaaService动态代理,那么AaaService的Bean类型就是原始的AaaService了:
引起Spring对AaaService进行动态代理的<aop>配置注释后,重新启动容器,即可正常启动了。
但是这里AaaService的方法就不会被事务增强了,这将违背了我们的初衷,因此这种解决方法仅是为了让大家了解到引起IOC问题的根源所在,并没有真正解决问题。
方法2
将BbbService的AaaService成员改成AaaInterface:
既然AaaService Bean被植入事务增强动态代理后就变成了AaaInterface的实例,那么干脆我们更改BbbService的成员属性类型,也是可以解决问题的。
但是这样的话,只能调用接口中拥有的方法 ,在AaaService中定义的方法(如method2())就调用不到了,因为这个代理后的Bean不能被强制转换成AaaService。
因此,就引出了我们的终极解决办法,请看方法3:
方法3
刚才我们说“Spring在默认情况下,对实现接口的Bean采用基于接口的代理”,我们可否改变Spring这一“默认的行为”呢?答案是肯定的,那就是通过proxy-target-class="true"这个属性,Spring植入增强时,将不管Bean有没有实现接口,统统采用基于扩展子类的方式进行动态代理,也即生成的动态代理是AaaService的子类,那当然就可以赋给AaaService有实例了:
注意,你需要将CGLib包放到类路径下,因为Spring用它来动态生成代理!以下是我这个小例子的Maven:
请看下面的IOC实例:
1)AaaService实现AaaaInterface接口
2)在BaaService中Autowired AaaService
Code
//1.AaaInterface package com.test; public interface AaaInterface { void method1(); } //2.AaaService package com.test; public class AaaService implements AaaInterface { @Override public void method1() { System.out.println("hello"); } public void method2() { System.out.println("hello"); } } //3.BbbService package com.test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; public class BbbService { @Autowired private AaaService aaaService; public void method2(){ System.out.println("hello method2"); } }
Spring 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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <context:property-placeholder location="classpath*:conf.properties"/> <!--扫描除Controller外的Bean,Controller在MVC层配置--> <context:component-scan base-package="com.test"/> <!--数据源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.SingleConnectionDataSource" p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}" p:username="${jdbc.username}" p:password="${jdbc.password}"/> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"/> <!-- 通过AOP配置的事务管理增强 --> <aop:config > <aop:pointcut id="serviceMethod" expression="execution(public * com.test..*Service.*(..))"/> <aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice"/> </aop:config> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- //////////////////////////////////////////////// --> </beans>
启动Spring容器
package com.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainStarter { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); BbbService bbbService = applicationContext.getBean("bbbService", BbbService.class); } }
结果报了以下这堆异常:
2013-7-25 13:54:08 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons 信息: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@687bc899: defining beans [org.springframework.context.support.PropertySourcesPlaceholderConfigurer#0,aaaService,bbbService,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,dataSource,txManager,org.springframework.aop.config.internalAutoProxyCreator,serviceMethod,org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0,txAdvice,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy //重要的信息在这儿!! Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bbbService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.test.AaaService com.test.BbbService.aaaService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.test.AaaService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288) at ... 50 more
上面的异常告诉我们,BbbService Bean创建不了,因为无法Autowired其aaaService的成员变量,说在Spring容器中不存在这个AaaService的Bean.
分析
以上是Spring IoC中一个经典的错误,其原因是Spring对Bean的动态代理引起的:
1)由于在applicationContext.xml中,通过<aop>对所有Service进行事务增强,因此Spring容器会对所有所有XxxService的Bean进行动态代理;
2)默认情况下,Spring使用基于接口的代理,也即:
a)如果Bean类有实现接口,那么Spring自动使用基于接口的代理创建该Bean的代理实例;
b)如果Bean类没有实现接口,那么则使用基于子类扩展的动态代理(即CGLib代理);
3)由于我们的AaaService实现了AaaInterface,所以Spring在生成AaaService类的动态代理Bean时,采用了
基于接口的动态代理,该动态代理实例只实现AaaInterface,且该实例不能强制转换成AaaService。换句话说,AaaService生成的Bean不能赋给AaaService的实例,而只能赋给AaaInterface的实例。
4)因此,当BbbService希望注入AaaService的成员时,Spring找不到对应的Bean了(因为只有AaaInterface的Bean).
解决
方法1
既然AaaService Bean是被Spring动态代理后改变成了类型,那如果取消引起动态代理的配置,使Spring不会对AaaService动态代理,那么AaaService的Bean类型就是原始的AaaService了:
<beans> ... <!-- 把以下的配置注释掉 --> <!-- <aop:config > <aop:pointcut id="serviceMethod" expression="execution(public * com.test..*Service.*(..))"/> <aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice"/> </aop:config> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="*"/> </tx:attributes> </tx:advice>--> <!-- //////////////////////////////////////////////// --> </beans>
引起Spring对AaaService进行动态代理的<aop>配置注释后,重新启动容器,即可正常启动了。
但是这里AaaService的方法就不会被事务增强了,这将违背了我们的初衷,因此这种解决方法仅是为了让大家了解到引起IOC问题的根源所在,并没有真正解决问题。
方法2
将BbbService的AaaService成员改成AaaInterface:
@Service("bbbService") public class BbbService { @Autowired private AaaInterface aaaService;//注意这儿,成员类型从AaaService更改为AaaInterface public void method2(){ System.out.println("hello method2"); } }
既然AaaService Bean被植入事务增强动态代理后就变成了AaaInterface的实例,那么干脆我们更改BbbService的成员属性类型,也是可以解决问题的。
但是这样的话,只能调用接口中拥有的方法 ,在AaaService中定义的方法(如method2())就调用不到了,因为这个代理后的Bean不能被强制转换成AaaService。
因此,就引出了我们的终极解决办法,请看方法3:
方法3
刚才我们说“Spring在默认情况下,对实现接口的Bean采用基于接口的代理”,我们可否改变Spring这一“默认的行为”呢?答案是肯定的,那就是通过proxy-target-class="true"这个属性,Spring植入增强时,将不管Bean有没有实现接口,统统采用基于扩展子类的方式进行动态代理,也即生成的动态代理是AaaService的子类,那当然就可以赋给AaaService有实例了:
<!-- 注意这儿的proxy-target-class="true" --> <aop:config proxy-target-class="true"> <aop:pointcut id="serviceMethod" expression="execution(public * com.test..*Service.*(..))"/> <aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice"/> </aop:config> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="*"/> </tx:attributes> </tx:advice>
注意,你需要将CGLib包放到类路径下,因为Spring用它来动态生成代理!以下是我这个小例子的Maven:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>onlyTest</groupId> <artifactId>onlyTest</artifactId> <version>1.0-SNAPSHOT</version> <properties> <spring.version>3.2.3.RELEASE</spring.version> <mysql.version>5.1.25</mysql.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.10</version> </dependency> </dependencies> </project>
发表评论
-
mybatis3.1分页自动添加总数
2013-07-08 21:11 22700问题 1.mybatis默认分页是内存分页的,谁用谁崩溃啊! ... -
学习Spring必学的Java基础知识(9)----HTTP请求报文
2012-06-09 16:02 13858引述要学习Spring框架的技术内幕,必须事先掌握一些基本的J ... -
学习Spring必学的Java基础知识(4)----XML基础知识
2012-05-12 15:33 8480引述要学习Spring框架的 ... -
学习Spring必学的Java基础知识(3)----PropertyEditor
2012-05-12 15:13 16753引述要学习Spring框架的 ... -
学习Spring必学的Java基础知识(2)----动态代理
2012-05-02 13:03 9604引述要学习Spring框架的 ... -
学习Spring必学的Java基础知识(1)----反射
2012-04-25 13:57 89551引述要学习Spring框架的技术内幕,必须事先掌握一些基本的J ... -
透透彻彻IoC(你没有理由不懂!)
2012-04-18 11:01 94036引述:IoC(控制反转:I ... -
单元测试系列之5:使用unitils测试Service层
2012-04-14 10:48 18315引述:Spring 的测试框架为我们提供一个强大的测试环境,解 ... -
如何用Spring读取JAR中的文件
2012-04-13 17:22 18278使用如下方式读取JAR中的文件出错 类路径下 ... -
单元测试系列之3:测试整合之王Unitils
2012-04-09 14:11 15525引述:程序测试对保障应用程序正确性而言,其重要性怎么样强调都不 ... -
单元测试系列之1:开发测试的那些事儿
2012-03-28 12:52 9952引述:程序测试对保障应用程序正确性而言,其重要性怎 ... -
单元测试的那些事儿
2012-03-28 12:48 2------------------------------- ... -
Spring 3.0的新功能
2012-03-26 09:30 71182009年9月发布Spring ... -
Spring的事务管理难点剖析(7):数据连接泄漏
2012-03-07 10:53 6754底层连接资源的访问问题 对于应用开发者来说,数据连接泄 ... -
Spring的事务管理难点剖析(6):特殊方法成漏网之鱼
2012-03-07 09:28 4494哪些方法不能实施Spring ... -
Spring的事务管理难点剖析(5):联合军种作战的混乱
2012-03-07 09:10 7804Spring事务管理器的应对 Spring抽象的DA ... -
Spring的事务管理难点剖析(4):多线程的困惑
2012-03-06 17:30 16844Spring通过单实例化Bean简化多线程问题 由于 ... -
Spring的事务管理难点剖析(3):事务方法嵌套调用的迷茫
2012-03-06 17:23 10517Spring事务传播机制回顾 ... -
Spring的事务管理难点剖析(2):应用分层的迷惑
2012-03-06 16:59 5036Web、Service及DAO三 ... -
Spring的事务管理难点剖析(1):DAO和事务管理的牵绊
2012-03-06 16:54 7111有些人很少使用Spring而不使用Spring事务管理器 ...
相关推荐
spring ioc指的是控制反转,IOC容器负责实例化、定位、配置应用程序中的对象及建立这些对象间的依赖。交由Spring容器统一进行管理,从而实现松耦合
spring ioc容器初始化流程图 spring ioc容器依赖注入流程图 spring aop实现原理流程图
springIOC手写框架分析springIOC手写框架分析springIOC手写框架分析springIOC手写框架分析springIOC手写框架分析springIOC手写框架分析springIOC手写框架分析springIOC手写框架分析springIOC手写框架分析springIOC...
spring ioc模块手写demospring ioc模块手写demospring ioc模块手写demospring ioc模块手写demospring ioc模块手写demospring ioc模块手写demospring ioc模块手写demospring ioc模块手写demospring ioc模块手写demo...
SpringIoc示例代码,SpringIoc示例代码,SpringIoc示例代码,SpringIoc示例代码
spring Ioc容器配置 IOC容器数据源配置 <!-- 配置数据源 --> destroy-method="close"> <value>org.gjt.mm.mysql.Driver <value>jdbc:mysql://localhost:3306/demo <value>root ...
Spring ioc Spring ioc
spring ioc思维导图,让自己快速、详尽地了解spring ioc知识。
详细讲解了springioc的各种注入方式以及对应的java方式。讲解了springioc和DI的区别,以及注入特殊复杂的属性
主要为大家详细解析了Spring中IoC优点与缺点,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
以注解方式模拟Spring IoC AOP,了解Spring是如何实现的.
简单的模仿Spring Ioc 配置文件属性只支持id class name ref,其他的没做解析
这是个模拟spring ioc的学习例子,是个java工程。有助于对aop和ioc的理解
Spring IoC加载流程讲解以及IoC思想和依赖倒置原则
控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种...通过控制反转,对象在被创建的时候,由一个调控系统内所有对象的外界实体,将其所依赖的对象的引用传递给它。也可以说,依赖被注入到对象中。
第一个spring IOC例子(非maven) 方法步骤: 1、下载spring包,提取core,bean,context,expression 四大基础jar http://repo.spring.io/release/org/springframework/spring/ 2、下载common-logging jar ...
Spring Ioc 注解 依赖注入
SpringIOC经典Demo,绝对的经典.
spring ioc开发最小依赖包4个,还包含两个辅助包commons-logging.jar和log4j.jar
spring的ioc容器详细讲解,理解springioc容器