2017-02-14 2 views
0

JUnit 테스트를 작성하는 동안 이상한 문제가 있습니다. 하나의 서비스 구현 클래스를 Autowired 할 수 있지만 다른 하나는 Autowired 할 수 없습니다. ServiceImpl1과 ServiceImpl2의 applicationContext 구성은 비슷합니다. Java의 프록시 인스턴스에서 구현 클래스 인스턴스를 얻는 방법

@Autowired 
private ServiceImpl1 serviceImpl1; //This one works. 

@Autowired 
private ServiceImpl2 serviceImpl2; //This one doesn't work. 

하지만이 사람이 여기에 ServiceImpl2

@Autowired 
private Service2 service2; //This one works. 

작동은 서비스 2의 구현 클래스입니다. service2에서 ServiceImpl2 인스턴스를 가져올 수 있습니까?

인터페이스 Service2에없는 ServiceImpl2의 몇 가지 메소드를 테스트하고 싶습니다.

또는 ServiceImpl2 클래스 용 자동 와이어 작업을 어떻게 만들 수 있는지 알고 있다면?

+0

에서 작성된 콩 솔루션 나를 위해 좋은 있나요 발견? – Stultuske

+0

예. ServiceImpl1과 ServiceImpl2가 동일한 applicationContext.xml에 구성되어 있습니다. –

+0

어떤 오류가 발생합니까? – Stultuske

답변

0

다른 게시물의 답변을 찾았습니다.

나는 당신이 그 유형에 autowire하기 수있는 구성/ http://www.techper.net/2009/06/05/how-to-acess-target-object-behind-a-spring-proxy/

@SuppressWarnings({"unchecked"}) 
protected <T> T getTargetObject(Object proxy, Class<T> targetClass) throws Exception { 
    if (AopUtils.isJdkDynamicProxy(proxy)) { 
    return (T) ((Advised)proxy).getTargetSource().getTarget(); 
    } else { 
    return (T) proxy; // expected to be cglib proxy then, which is simply a specialized class 
    } 
} 

사용

@Override 
protected void onSetUp() throws Exception { 
    getTargetObject(fooBean, FooBeanImpl.class).setBarRepository(new MyStubBarRepository()); 
} 
관련 문제