2011-09-05 82 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

请解释你在做什么,而不是只发布答案。文档将会非常棒! –