2011-09-05 81 views
1

在我的A類中,我自動接線了具有@Service註釋的B類。在我的B類我是自動佈線C類,並使用參考@Transactional方法內該類類B.@Autowire註釋問題

,看來,自動佈線沒有做任何事情,因爲我得到java.lang.NullPointerException

一類的例子:

@Controller 
public Class controller{ 
    @Autowired 
    private MyService myservice; 
} 

B類

@Service 
public Class serviceImpl{ 
    @Autowired 
    private DAOInterface dao; 
    //nullpointer 

    @Transactional 
    public String getItem(){ 
    return dao.getItem(2); 
    } 

} 

任何幫助?

+1

您可以發佈您的context.xml(或同等學歷)的內容? –

回答

4

如果您想使用@Autowired註釋爲您進行彈簧佈線,則需要註冊合適的BeanPostProcessor以提供幫助。你可以有春天通過在Spring配置以下元素爲你做這個:

<context:annotation-config/> 

看看Section 3.9 in the Spring 3.0 Documentation更多這方面的信息。另外,由於看起來您正在使用原型註釋(@Component,@Service,@Controller),您可能會試圖放棄Spring XML接線(或減少它)。您需要確保您在Spring XML中包含了組件掃描元素。

注意:如果您包括component-scan,則不需要使用annotation-config元素。

<context:component-scan base-package="your.package.name"/> 

看看Section 3.10 in the Spring 3.0 Documentation更多這方面的信息。

1

確保您的DAO以某種方式進行了配置......無論是在註釋(@Service,@Component,@Repository)中,在xml配置中還是通過其他方式。

如果這沒有幫助,我們將需要更多信息。

0

服務類

@Service 
public Class serviceImpl implements MyService { 
    @Autowired 
    private DAOInterface dao; 
    //nullpointer 

    @Transactional 
    public String getItem(){ 
    return dao.getItem(2); 
    } 

} 

爲spring-servlet.xml

<context:annotation-config/>

+0

請解釋你在做什麼,而不是隻發佈答案。文檔將會非常棒! –