2012-04-11 4 views
1

저는 첫 번째 Spring 프로젝트에서 작업하고 있으며, 성가신 문제를 발견했습니다.일부 개체는 @Autowired가 항상 null로 설정하고, 다른 개체는 정상적으로 설정합니다.

<bean id="usernameService" class="service.UsernameService" scope="session" > 
    <aop:scoped-proxy /> 
</bean> 

이 콩이 (내 클래스 중 하나에서 만든 빈 정의 :

을 :

나는 디스패처-servlet.xml에 콩으로 구성된 'UsernameService'라는 클래스를 가지고 같은

<bean id="testController" class="controller.TestController" /> 

:

@Autowired 
UsernameService uns; 

절대적으로 잘 작동합니다. 내가 LogController 시도하고 다른 클래스에서 동일한 기능을 수행 할 때, :

<bean id="logController" class="controller.LogController" /> 

가 작동하지 않습니다, 나는 다음과 같은 오류 얻을 :

SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/flexitime] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause java.lang.NullPointerException

I가 관리 한을 (저는 믿습니다)이 오류는 uns가 실제로 설정/생성되지 않고 LogController 내부에서 null로 유지된다는 사실에 기인합니다.

나는 이것을 광범위하게 봤으며 많은 '해결책'을 찾았지만 아무도 연구하지 못했습니다.

감사합니다. 제임스

+0

어떻게 액세스 할 수있는 LogController입니다. 웹 요청에 대한 응답으로 호출되는 @RequestMapping 메서드가 있습니까? 아니면 코드에서 새 LogController를 수행하고 있습니까? – gkamal

+0

injection이 작동하려면'new'에 의해 객체가 생성되지 않을 수 있습니다. –

+0

@gkamal LogController는 다른 컨트롤러에서 액세스되며 자체 자체 빈 (autowired bean)입니다 :'@Autowired (new line) LogController ls'. @thorb 클래스를 초기화하는 데 new 키워드를 사용하지 않습니다. – ymhr

답변

0

은 콩에 auto-wire 속성을 추가

<bean id="usernameService" class="service.UsernameService" scope="session" autowire="byName"> 
    <aop:scoped-proxy /> 
</bean> 
+0

그게 나에게 오류가 발생했습니다 : '속성'기본 - autowire '요소'콩 '에 게재 허용되지 않습니다 – ymhr

+0

코드를 한 번 확인 –

+0

그것을 업데이트, 죄송합니다 내 자신을 알아야 했어 :(불행히도 아직 doesn 일하지 마라. – ymhr