我现在已经调试了一段时间了,我希望有人能在这里说清楚.
我有一个使用JDK 1.6添加到Jenkins的Maven项目.我在这个项目中使用AOP来处理数据库事务.
当我在Jenkins中运行构建时,我的测试用例失败,但有以下例外: -
Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'dataHandlerClassificationImpl':
Injection of resource dependencies failed; nested exception is
org.springframework.beans.factory.BeanNotOfRequiredTypeException:
Bean named 'writerDataLocationImpl' must be of type [xxx.script.WriterData],
but was actually of type [$Proxy17]
...
...
Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException:
Bean named 'writerDataLocationImpl' must be of type [xxx.script.WriterData],
but was actually of type [$Proxy17]
...
...
Run Code Online (Sandbox Code Playgroud)
该DataHandlerClassificationImpl课程看起来像这样: -
@Service
public class DataHandlerClassificationImpl extends DataHandler {
@Resource(name="writerDataLocationImpl")
private WriterData writerData;
...
}
Run Code Online (Sandbox Code Playgroud)
WriterData 是一个具有多个实现的接口.
我能够在没有问题的情况下从IDE执行代码.为了确定它是Maven问题还是Jenkins问题,我使用命令行导航到Jenkins的项目作业文件夹,我能够毫无错误地运行mvn test …
尝试在Junit测试中模拟服务的属性时遇到问题:
@ContextConfiguration("classpath:application-config.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class FooServiceTests {
@Autowired
private FooServiceImpl fooService;
@Test
public void testFoo() {
String str = fooService.foo();
assertEquals("Var", str);
}
@Before
public void mockFooDao() throws Exception {
FooDao mockFooDao = Mockito.mock(FooDao.class);
Mockito.when(mockFooDao.foo()).thenReturn("Var");
ReflectionTestUtils.setField(fooService, "fooDao", mockFooDao);
}
}
Run Code Online (Sandbox Code Playgroud)
嘲弄fooDao没有效果,因为结果不是预期的.这是服务和dao的代码:
@Service("fooService")
public class FooServiceImpl implements FooService {
@Autowired
protected FooDao fooDao;
@Override
public String foo() {
return fooDao.foo();
}
}
@Repository
public class FooDaoImpl implements FooDao {
@Override
public String foo() {
return "foo";
}
}
Run Code Online (Sandbox Code Playgroud)
正如我们所看到的,实际服务意味着返回"foo",但是测试会模仿dao,因此服务返回"var".我知道这是一个与CGLIB代理相关的东西,但我无法弄清楚如何在不使用fooDao属性的setter的情况下使其工作.任何帮助,将不胜感激.
提前问候并表示感谢.
这个线程与我在这里遇到的有关访问受建议类的受保护方法的需求有关.我正在使用Spring 3.0.6,并创建了一个Spring分析方面,我正在使用JDK代理应用于大量的bean.
但是,由于需要访问一个特定bean中的受保护方法,我想使用CGLIB建议它.所有其他bean我想继续使用JDK Proxies.
我使用了注释和xml配置的混合,但这个特定方面是在XML配置中定义的.
我知道有<aop:scoped-proxy>标签,但据我所知,这适用于所有方面.
无论如何要定义单个方面来使用CGLIB吗?
<aop:config>
<aop:aspect id="Profiler" ref="lendingSimulationServiceProfilerInterceptor">
<!-- info -->
<aop:around method="infoProfiler"
pointcut="execution(* com.cws.cs.lendingsimulationservice.service.LendingSimulationServiceImpl.calculate*(..))" />
<!-- debug -->
<aop:around method="infoProfiler"
pointcut="execution(* com.cws.cs.lendingsimulationservice.process.LendingSimulationProcessImpl.calculate(..))" />
<aop:around method="infoProfiler"
pointcut="execution(* com.blaze.BlazeEngine.invokeService(..))" />
<!-- trace -->
<aop:around method="traceProfiler"
pointcut="execution(* com.calculator.dao.impl.LendingSimulationDaoImpl.*(..))" />
<!-- NEED TO DEFINE THIS PARTICULAR ASPECT AS CGLIB -->
<aop:around method="traceProfiler"
pointcut="execution(* com.cws.cs.lendingsimulationservice.util.pool.JAXBPoolImpl.*(..))" />
</aop:aspect>
</aop:config>
Run Code Online (Sandbox Code Playgroud)
我试图将配置拆分为两个,并且对于一个配置指定target-class="true"和另一个target-class="false",但它似乎在那时将CGLIB应用于所有.
有没有办法实现这个目标?
谢谢,
埃里克
只是为了学习和理解代理,我想看看Spring AOP生成的代理类。它在Eclipse生成的classes文件夹中不存在。
有人可以告诉我它的位置吗?