java - Spring cannot use AOP with embedded database scripts -
spring cannot initialize bean "datasource", while bean declaration contains <jdbc:script location="...."/>
line, when running unit test.
the path script seems correct, script seems correct.
the initialization goes way: applicationcontext.xml imports db-h2-config.xml, finds "datasource" bean.
if bean contains of <jdbc:script location>
part, fails attached stacktrace. (for full stacktrace please see below)
caused by: org.springframework.beans.factory.beancreationexception: error creating bean name '(inner bean)#3427b02d': cannot create inner bean '(inner bean)#41fbdac4' of type [org.springframework.jdbc.config.sortedresourcesfactorybean] while setting bean property 'scripts'; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name '(inner bean)#41fbdac4': post-processing of factorybean's object failed; nested exception java.lang.assertionerror
if remove <jdbc:script location>
part, there no error.
update: have added sayaop.java , trackcoach.java code below.
sayaop.java:
package hu.springdemo; public class sayaop { public void shoutaopbefore() { system.out.println("here aop before"); } public void shoutaopafter(){ system.out.println("here aop after"); } }
trackcoach.java:
package hu.springdemo; public class trackcoach implements coachif { @override public void getdailyworkout() { system.out.println("trackcoach says: 'go , run 5k!' "); }
}
apptest.java:
package hu.apptest; import hu.springdemo.baseballathlete; import hu.springdemo.coachif; import hu.springdemo.coachspringconfig; import org.junit.test; import org.junit.runner.runwith; import org.springframework.beans.factory.annotation.autowired; import org.springframework.beans.factory.annotation.qualifier; import org.springframework.beans.factory.parsing.location; import org.springframework.context.support.classpathxmlapplicationcontext; import org.springframework.test.context.contextconfiguration; import org.springframework.test.context.junit4.springjunit4classrunner; import static org.junit.assert.*; @runwith(springjunit4classrunner.class) @contextconfiguration(locations = "/applicationcontext.xml") public class apptest { @autowired coachif mybaseballguy; @test public void baseballathleteshouldnotbenull() { assertnotnull(mybaseballguy); mybaseballguy.getdailyworkout(); } }
db-h2-config.xml:
<beans xmlns:jdbc="http://www.springframework.org/schema/jdbc" 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-4.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd"> <jdbc:embedded-database id="datasource" type="h2"> <jdbc:script location="classpath:db/sql/create-db.sql"/> </jdbc:embedded-database> </beans>
applicationcontext.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd "> <import resource="classpath:db-h2-config.xml"/> <bean id="mytrackcoach" class="hu.springdemo.trackcoach"> </bean> <bean id="sayaop" class="hu.springdemo.sayaop"> </bean> <aop:config> <aop:aspect id="mtc" ref="sayaop"> <aop:pointcut id="pmtc" expression="execution(* hu.springdemo.trackcoach.getdailyworkout(..))"/> <aop:before pointcut-ref="pmtc" method="shoutaopbefore"/> <aop:after pointcut-ref="pmtc" method="shoutaopafter"/> </aop:aspect> </aop:config> </beans>
create-db.sql:
create table users ( id integer primary key, name varchar(30), email varchar(50) );
stacktrace:
java.lang.illegalstateexception: failed load applicationcontext @ org.springframework.test.context.cache.defaultcacheawarecontextloaderdelegate.loadcontext(defaultcacheawarecontextloaderdelegate.java:124) @ org.springframework.test.context.support.defaulttestcontext.getapplicationcontext(defaulttestcontext.java:83) @ org.springframework.test.context.support.dependencyinjectiontestexecutionlistener.injectdependencies(dependencyinjectiontestexecutionlistener.java:117) @ org.springframework.test.context.support.dependencyinjectiontestexecutionlistener.preparetestinstance(dependencyinjectiontestexecutionlistener.java:83) @ org.springframework.test.context.testcontextmanager.preparetestinstance(testcontextmanager.java:230) @ org.springframework.test.context.junit4.springjunit4classrunner.createtest(springjunit4classrunner.java:228) @ org.springframework.test.context.junit4.springjunit4classrunner$1.runreflectivecall(springjunit4classrunner.java:287) @ org.junit.internal.runners.model.reflectivecallable.run(reflectivecallable.java:12) @ org.springframework.test.context.junit4.springjunit4classrunner.methodblock(springjunit4classrunner.java:289) @ org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:247) @ org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:94) @ org.junit.runners.parentrunner$3.run(parentrunner.java:290) @ org.junit.runners.parentrunner$1.schedule(parentrunner.java:71) @ org.junit.runners.parentrunner.runchildren(parentrunner.java:288) @ org.junit.runners.parentrunner.access$000(parentrunner.java:58) @ org.junit.runners.parentrunner$2.evaluate(parentrunner.java:268) @ org.springframework.test.context.junit4.statements.runbeforetestclasscallbacks.evaluate(runbeforetestclasscallbacks.java:61) @ org.springframework.test.context.junit4.statements.runaftertestclasscallbacks.evaluate(runaftertestclasscallbacks.java:70) @ org.junit.runners.parentrunner.run(parentrunner.java:363) @ org.springframework.test.context.junit4.springjunit4classrunner.run(springjunit4classrunner.java:191) @ org.junit.runner.junitcore.run(junitcore.java:137) @ com.intellij.junit4.junit4ideatestrunner.startrunnerwithargs(junit4ideatestrunner.java:117) @ com.intellij.junit4.junit4ideatestrunner.startrunnerwithargs(junit4ideatestrunner.java:42) @ com.intellij.rt.execution.junit.junitstarter.preparestreamsandstart(junitstarter.java:253) @ com.intellij.rt.execution.junit.junitstarter.main(junitstarter.java:84) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:498) @ com.intellij.rt.execution.application.appmain.main(appmain.java:147) caused by: org.springframework.beans.factory.beancreationexception: error creating bean name 'datasource': cannot create inner bean '(inner bean)#72f926e6' of type [org.springframework.jdbc.datasource.init.compositedatabasepopulator] while setting bean property 'databasepopulator'; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name '(inner bean)#72f926e6': cannot create inner bean '(inner bean)#3427b02d' of type [org.springframework.jdbc.datasource.init.resourcedatabasepopulator] while setting bean property 'populators' key [0]; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name '(inner bean)#3427b02d': cannot create inner bean '(inner bean)#41fbdac4' of type [org.springframework.jdbc.config.sortedresourcesfactorybean] while setting bean property 'scripts'; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name '(inner bean)#41fbdac4': post-processing of factorybean's object failed; nested exception java.lang.assertionerror @ org.springframework.beans.factory.support.beandefinitionvalueresolver.resolveinnerbean(beandefinitionvalueresolver.java:313) @ org.springframework.beans.factory.support.beandefinitionvalueresolver.resolvevalueifnecessary(beandefinitionvalueresolver.java:129) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.applypropertyvalues(abstractautowirecapablebeanfactory.java:1481) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.populatebean(abstractautowirecapablebeanfactory.java:1226) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:543) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:482) @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:306) @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:230) @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:302) @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:197) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.preinstantiatesingletons(defaultlistablebeanfactory.java:756) @ org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:861) @ org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:541) @ org.springframework.test.context.support.abstractgenericcontextloader.loadcontext(abstractgenericcontextloader.java:128) @ org.springframework.test.context.support.abstractgenericcontextloader.loadcontext(abstractgenericcontextloader.java:60) @ org.springframework.test.context.support.abstractdelegatingsmartcontextloader.delegateloading(abstractdelegatingsmartcontextloader.java:108) @ org.springframework.test.context.support.abstractdelegatingsmartcontextloader.loadcontext(abstractdelegatingsmartcontextloader.java:251) @ org.springframework.test.context.cache.defaultcacheawarecontextloaderdelegate.loadcontextinternal(defaultcacheawarecontextloaderdelegate.java:98) @ org.springframework.test.context.cache.defaultcacheawarecontextloaderdelegate.loadcontext(defaultcacheawarecontextloaderdelegate.java:116) ... 29 more caused by: org.springframework.beans.factory.beancreationexception: error creating bean name '(inner bean)#72f926e6': cannot create inner bean '(inner bean)#3427b02d' of type [org.springframework.jdbc.datasource.init.resourcedatabasepopulator] while setting bean property 'populators' key [0]; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name '(inner bean)#3427b02d': cannot create inner bean '(inner bean)#41fbdac4' of type [org.springframework.jdbc.config.sortedresourcesfactorybean] while setting bean property 'scripts'; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name '(inner bean)#41fbdac4': post-processing of factorybean's object failed; nested exception java.lang.assertionerror @ org.springframework.beans.factory.support.beandefinitionvalueresolver.resolveinnerbean(beandefinitionvalueresolver.java:313) @ org.springframework.beans.factory.support.beandefinitionvalueresolver.resolvevalueifnecessary(beandefinitionvalueresolver.java:129) @ org.springframework.beans.factory.support.beandefinitionvalueresolver.resolvemanagedlist(beandefinitionvalueresolver.java:382) @ org.springframework.beans.factory.support.beandefinitionvalueresolver.resolvevalueifnecessary(beandefinitionvalueresolver.java:157) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.applypropertyvalues(abstractautowirecapablebeanfactory.java:1481) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.populatebean(abstractautowirecapablebeanfactory.java:1226) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:543) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:482) @ org.springframework.beans.factory.support.beandefinitionvalueresolver.resolveinnerbean(beandefinitionvalueresolver.java:299) ... 47 more caused by: org.springframework.beans.factory.beancreationexception: error creating bean name '(inner bean)#3427b02d': cannot create inner bean '(inner bean)#41fbdac4' of type [org.springframework.jdbc.config.sortedresourcesfactorybean] while setting bean property 'scripts'; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name '(inner bean)#41fbdac4': post-processing of factorybean's object failed; nested exception java.lang.assertionerror @ org.springframework.beans.factory.support.beandefinitionvalueresolver.resolveinnerbean(beandefinitionvalueresolver.java:313) @ org.springframework.beans.factory.support.beandefinitionvalueresolver.resolvevalueifnecessary(beandefinitionvalueresolver.java:129) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.applypropertyvalues(abstractautowirecapablebeanfactory.java:1481) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.populatebean(abstractautowirecapablebeanfactory.java:1226) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:543) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:482) @ org.springframework.beans.factory.support.beandefinitionvalueresolver.resolveinnerbean(beandefinitionvalueresolver.java:299) ... 55 more caused by: org.springframework.beans.factory.beancreationexception: error creating bean name '(inner bean)#41fbdac4': post-processing of factorybean's object failed; nested exception java.lang.assertionerror @ org.springframework.beans.factory.support.factorybeanregistrysupport.getobjectfromfactorybean(factorybeanregistrysupport.java:133) @ org.springframework.beans.factory.support.beandefinitionvalueresolver.resolveinnerbean(beandefinitionvalueresolver.java:302) ... 61 more caused by: java.lang.assertionerror @ org.aspectj.weaver.unresolvedtype.nametosignature(unresolvedtype.java:726) @ org.aspectj.weaver.unresolvedtype.forname(unresolvedtype.java:231) @ org.aspectj.weaver.world.resolve(world.java:423) @ org.aspectj.weaver.internal.tools.pointcutexpressionimpl.couldmatchjoinpointsintype(pointcutexpressionimpl.java:80) @ org.springframework.aop.aspectj.aspectjexpressionpointcut.matches(aspectjexpressionpointcut.java:250) @ org.springframework.aop.support.aoputils.canapply(aoputils.java:220) @ org.springframework.aop.support.aoputils.canapply(aoputils.java:279) @ org.springframework.aop.support.aoputils.findadvisorsthatcanapply(aoputils.java:311) @ org.springframework.aop.framework.autoproxy.abstractadvisorautoproxycreator.findadvisorsthatcanapply(abstractadvisorautoproxycreator.java:118) @ org.springframework.aop.framework.autoproxy.abstractadvisorautoproxycreator.findeligibleadvisors(abstractadvisorautoproxycreator.java:88) @ org.springframework.aop.framework.autoproxy.abstractadvisorautoproxycreator.getadvicesandadvisorsforbean(abstractadvisorautoproxycreator.java:69) @ org.springframework.aop.framework.autoproxy.abstractautoproxycreator.wrapifnecessary(abstractautoproxycreator.java:347) @ org.springframework.aop.framework.autoproxy.abstractautoproxycreator.postprocessafterinitialization(abstractautoproxycreator.java:299) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.applybeanpostprocessorsafterinitialization(abstractautowirecapablebeanfactory.java:422) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.postprocessobjectfromfactorybean(abstractautowirecapablebeanfactory.java:1723) @ org.springframework.beans.factory.support.factorybeanregistrysupport.getobjectfromfactorybean(factorybeanregistrysupport.java:130) ... 62 more process finished exit code 255
different problem it's solution solved issue well, causes still not clear, seems somehow running of scripts has clashed spring aop:
populating h2 database spring , jdbc datasourcetransactionmanager
i changed embedded database bean follows remove script elements:
<jdbc:embedded-database id="embeddedh2database" type="h2"/>
and added @sql annotation before test create table , load table.
@sql({"classpath:db/sql/create-db.sql", "classpath:db/sql/insert-data.sql"})
Comments
Post a Comment