我有一个Spring bean,让我们说:
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class AImpl implements A {
public void setSomeDependency(D dependency) {
// This setter DOES NOT BELONG to interface A
}
}
<bean id="aImpl" class="AImpl"/>
Run Code Online (Sandbox Code Playgroud)
现在我想集成测试它,但首先我需要模拟依赖D,因为它做了太多的东西.由于AImpl实现了一个接口并包含一个事务注释,生成的代理只与接口兼容A,所以我可以这样做:
@Inject @Named("aImpl")
private A a;
Run Code Online (Sandbox Code Playgroud)
但不能:
@Inject @Named("aImpl")
private AImpl a;
Run Code Online (Sandbox Code Playgroud)
结果,我无法模仿我的依赖.
请注意,添加void setSomeDependency(D dependency)到界面A不是一个选项,因为它没有商业含义.它都没有使用proxy-target-class="true",因为它打破了很多其他bean(这个属性会影响上下文中的所有bean).
有没有办法解开注入的bean A,所以我可以把它投射到AImpl?
我正在使用Java Spring Mvc和Spring AOP从用户那里查找参数名称.
我有一个控制器,它从用户获取参数并调用服务.
我有一个方面,在服务之前运行.
方面应检查username和apiKey参数是否存在.
这是我的代码:
控制器:
@RequestMapping(method = RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody String getDomainWithFoundIn(@RequestParam (value="domain") String domain, @RequestParam (value="user") String user, @RequestParam (value="apiKey") String apiKey) throws JsonGenerationException, JsonMappingException, IOException {
return domainService.getDomainDataWithFoundIn(domain, user, apiKey);
}
Run Code Online (Sandbox Code Playgroud)
域服务接口:
public interface IDomainService {
public String getDomainDataWithFoundIn(String domainStr, String user, String apiKey);
}
Run Code Online (Sandbox Code Playgroud)
DomainService:
@Override
@ApiAuthentication
public String getDomainDataWithFoundIn(String domainStr, String user, String apiKey) {
//Do stuff here
}
Run Code Online (Sandbox Code Playgroud)
而我的AOP课程:
@Component
@Aspect
public class AuthAspect {
@Before("@annotation(apiAuthentication)")
public void printIt (JoinPoint …Run Code Online (Sandbox Code Playgroud)