相关疑难解决方法(0)

模拟CGLIB代理服务的属性不起作用

尝试在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 mocking cglib proxies

17
推荐指数
1
解决办法
3871
查看次数

将Spring的Proxy对象转换为实际的运行时类

我正在使用Spring,有一点我想将对象转换为它的实际运行时实现.

例:

Class MyClass extends NotMyClass {
    InterfaceA a;
    InterfaceA getA() { return a; }

    myMethod(SomeObject o) { ((ImplementationOfA) getA()).methodA(o.getProperty()); }
}
Run Code Online (Sandbox Code Playgroud)

那喊一个ClassCastException,因为a是一个$ProxyN对象.虽然在beans.xml中我注入了一个类的bean ImplementationOfA.

编辑1 我扩展了一个类,我需要调用一个方法ImplementationOfA.所以我想我需要施展.该方法接收参数.

编辑2

我最好扯掉目标类:

private T getTargetObject(Object proxy, Class targetClass) throws Exception {
    while( (AopUtils.isJdkDynamicProxy(proxy))) {
        return (T) getTargetObject(((Advised)proxy).getTargetSource().getTarget(), targetClass);
    }
    return (T) proxy; // expected to be cglib proxy then, which is simply a specialized class
}
Run Code Online (Sandbox Code Playgroud)

我知道它不是很优雅但有效.

所有学分归http://www.techper.net/2009/06/05/how-to-acess-target-object-behind-a-spring-proxy/ 谢谢!

java spring

12
推荐指数
4
解决办法
2万
查看次数

在自定义注释的Aspect中传递方法参数

我正在尝试使用类似的东西org.springframework.cache.annotation.Cacheable:

自定义注释:

@Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface CheckEntity {
        String message() default "Check entity msg";
        String key() default "";
    }
Run Code Online (Sandbox Code Playgroud)

方面:

@Component
@Aspect
public class CheckEntityAspect {
    @Before("execution(* *.*(..)) && @annotation(checkEntity)")
    public void checkEntity(JoinPoint joinPoint, CheckEntitty checkEntity) {
        System.out.println("running entity check: " + joinPoint.getSignature().getName());
    }
}
Run Code Online (Sandbox Code Playgroud)

服务:

@Service
@Transactional
public class EntityServiceImpl implements EntityService {

    @CheckEntity(key = "#id")
    public Entity getEntity(Long id) {
        return new Entity(id);
    }
}    
Run Code Online (Sandbox Code Playgroud)

我的IDE(IntelliJ)没有看到任何与key = "#id"使用有关的特殊情况,与使用Cacheable不同颜色而不是纯文本的类似用法相比.我提到IDE部分只是作为一个提示,如果有帮助,看起来IDE预先知道这些注释或它只是意识到我的例子中不存在的一些连接.

checkEntity.key中的值为"#id"而不是预期的数字.我尝试使用ExpressionParser但可能不是以正确的方式. …

spring annotations aspectj spring-el

12
推荐指数
2
解决办法
8081
查看次数

自定义注释中的Spring表达式语言

我想在自定义注释中使用Spring Expression Language.此注释将由自定义Aspect使用.

看一下这个:

@StatisticEventTrigger(value = TestStatisticEvent.class, expression = "#p1")
public void someOtherMethod(String arg1, Long arg2) {
Run Code Online (Sandbox Code Playgroud)

如您所见,我想使用表达式(在本例中)来检索一些特定的参数.

当我有我的Aspect,触发带注释的方法时,我想评估spring表达式(以编程方式)来检索用于进一步业务的值;)

有任何想法吗?到目前为止谷歌不是我的朋友!

java aop spring spring-el

8
推荐指数
1
解决办法
5223
查看次数

Spring AOP Aspect不使用Mockito

我有一个@Aspect编织所有控制器动作方法的执行.它在我运行系统时工作正常,但在单元测试中没有.我用以下方式使用Mockito和junit:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:**/spring-context.xml")
@WebAppConfiguration
public class UserControllerTest {        
    private MockMvc mockMvc;

    @Mock
    private RoleService roleService;

    @InjectMocks
    private UserController userController;

    @Before
    public void setUp() {
       MockitoAnnotations.initMocks(this);                    
       ...    
       mockMvc = MockMvcBuilders.standaloneSetup(userController).build();
    }    
    ...
}
Run Code Online (Sandbox Code Playgroud)

一些@Test使用mockMvc.perform().

我的看点是:

@Pointcut("within(@org.springframework.stereotype.Controller *)")
public void controller() { }

@Pointcut("execution(* mypackage.controller.*Controller.*(..))")
public void methodPointcut() { }

@Around("controller() && methodPointcut()")
...
Run Code Online (Sandbox Code Playgroud)

junit aop spring mockito

7
推荐指数
2
解决办法
7324
查看次数

Spring代理中的调试问题

因此,我正在尝试修复一个相当复杂的基于 Spring 的 Web 应用程序上的错误。该错误是一个空指针异常,但是,它没有返回堆栈跟踪或任何类似的有用信息来让我确切地知道它发生的位置,这毫无帮助。

使用 IntelliJ 逐步执行程序显示异常发生在 Spring 代理对象: $Proxy338 中,但不显示代理代码。

我很抱歉这么含糊:有关此错误的错误报告毫无帮助。欢迎就如何从这里继续进行任何建议:我是否应该寻找产生失败代理的 AOP/事务代码?我是否可以使用 IntelliJ 收集更多信息,让我准确地查明正在发生的情况?

java spring

4
推荐指数
1
解决办法
2571
查看次数

标签 统计

spring ×6

java ×3

aop ×2

spring-el ×2

annotations ×1

aspectj ×1

cglib ×1

junit ×1

mocking ×1

mockito ×1

proxies ×1