[英]Spring Dependency Injection from JAR
I have dao layer which is in project A and project B is application layer which uses project A as it's dependency 我有项目A中的dao层,项目B是使用项目A作为依赖项的应用层
I can see the classes from project A and coded perfectly in project B using those classes. 我可以看到项目A中的类,并使用这些类在项目B中完美编码。
Now I want to do JUnit testing 现在我想做JUnit测试
in A (MyTest.java) 在A(MyTest.java)
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@ContextConfiguration(locations = { "classpath*:reb/spring/persistenceContextTest.xml", "classpath*:rev/spring/modelContextTest.xml" })
public class MyTest extends AbstractTransactionalJUnit4SpringContextTests {
@Autowired
protected HibernateTemplate hibernateTemplate;
@Autowired
protected DAOImpl dao;
//CODE here
}
Everything is fine here. 这里一切都很好。 All properties are injected properly and no issues.
所有属性都正确注入,没有问题。
Now I need to test Application Layer {project b} 现在我需要测试Application Layer {project b}
@ContextConfiguration(locations = { "classpath*:reb/spring/applicationContextTest.xml" })
public class MyAnotherTest extends AbstractTransactionalJUnit4SpringContextTests {
@Autowired
protected HibernateTemplate hibernateTemplate;
@Autowired
protected DAOImpl dao;
}
and in applicationContextTest.xml 并在applicationContextTest.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:jaxws="http://cxf.apache.org/jaxws"
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-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<import resource="classpath*:reb/spring/persistenceContextTest.xml" />
<import resource="classpath*:reb/spring/modelContextTest.xml" />
</beans>
but when I run the B test, Spring is unable to find the properties defined in project A. How do i achieve this? 但是当我运行B测试时,Spring无法找到项目A中定义的属性。我如何实现这一目标?
Update I moved test xmls to Src folder so that maven recognize in Project B and indeed it recognized. 更新我将测试xmls移动到Src文件夹,以便maven在项目B中识别并确实识别它。 however I am getting below error still
但是我仍然低于错误
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:220)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:301)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:303)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.apache.cxf.transport.servlet.ServletTransportFactory' defined in class path resource [META-INF/cxf/cxf-servlet.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (2) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'bus' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL [file:/C:/Users/Public/CC_DATA/OSS_Mar-2013_SharedDev/OSS/Rosy/JComponents/Rebecca/Rebecca-access/target/classes/rebecca/spring/persistenceContextDump.xml]: Invocation of init method failed; nested exception is org.hibernate.cache.CacheException: net.sf.ehcache.CacheException: Error configuring from null. Initial cause was null
PropertyAccessException 2: org.springframework.beans.MethodInvocationException: Property 'transportIds' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL [file:/C:/Users/Public/CC_DATA/OSS_Mar-2013_SharedDev/OSS/Rosy/JComponents/Rebecca/Rebecca-access/target/classes/rebecca/spring/persistenceContextDump.xml]: Invocation of init method failed; nested exception is org.hibernate.cache.CacheException: net.sf.ehcache.CacheException: Error configuring from null. Initial cause was null
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1361)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:84)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:280)
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:304)
... 24 more
Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (2) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'bus' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL [file:/C:/Users/Public/CC_DATA/OSS_Mar-2013_SharedDev/OSS/Rosy/JComponents/Rebecca/Rebecca-access/target/classes/rebecca/spring/persistenceContextDump.xml]: Invocation of init method failed; nested exception is org.hibernate.cache.CacheException: net.sf.ehcache.CacheException: Error configuring from null. Initial cause was null
PropertyAccessException 2: org.springframework.beans.MethodInvocationException: Property 'transportIds' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL [file:/C:/Users/Public/CC_DATA/OSS_Mar-2013_SharedDev/OSS/Rosy/JComponents/Rebecca/Rebecca-access/target/classes/rebecca/spring/persistenceContextDump.xml]: Invocation of init method failed; nested exception is org.hibernate.cache.CacheException: net.sf.ehcache.CacheException: Error configuring from null. Initial cause was null
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:102)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1358)
... 38 more
if you are using maven, then any resources and sources in src/test
are local the individual module, ie anything in projectA/src/test
will be hidden from projectB
. 如果您正在使用maven,那么
src/test
中的任何资源和源都是本地的单个模块,即projectA/src/test
任何内容都将从projectB
隐藏。 There are multiple options available: 有多种选择:
Move Test Resources: I would discourage you from doing this, but basically you can move your applicationContextTest.xml
to the src/main
folder, but then you have test code in your binary deliverables. 移动测试资源:我不鼓励你这样做,但基本上你可以将
applicationContextTest.xml
移动到src/main
文件夹,但是你的二进制交付项中有测试代码。
Replicate Test Resources: This is not as bad as the previous suggestion, when you copy any bean setup from projectA
to projectB
. 复制测试资源:当您将任何bean设置从
projectA
复制到projectB
时,这并不像以前的建议那么projectB
。 This usually implies that you are creating test application contexts for your unit tests and that you have different application contexts for your real application. 这通常意味着您要为单元测试创建测试应用程序上下文,并且您的实际应用程序具有不同的应用程序上下文。
Delete all Test Application Contexts: This would be the cleanest solution, if you dont have any test context you are forced to test the real thing. 删除所有测试应用程序上下文:这将是最干净的解决方案,如果您没有任何测试上下文,您将被迫测试真实的东西。 If you need environment specific properties you can use PropertyPlaceHolders, if you need different beans (Connection Pools, etc.) you can use Profiles.
如果需要特定于环境的属性,可以使用PropertyPlaceHolders,如果需要不同的bean(连接池等),则可以使用“个人档案”。
Have a look at the new Spring 3.1 Feature for Profiles: http://blog.springsource.org/2011/02/14/spring-3-1-m1-introducing-profile/ 查看配置文件的新Spring 3.1功能: http : //blog.springsource.org/2011/02/14/spring-3-1-m1-introducing-profile/
您的测试持久性上下文xml名称在项目A和项目B中是相同的。您是否可以尝试将项目A中的名称persistenceContextTest.xml
更改为persistenceContextTestA.xml
,以确保问题不是因为文件名重复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.