春天@Autowired不工作 [英] Spring @Autowired not working

查看:278
本文介绍了春天@Autowired不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问心无愧有自动装配注解一些问题。我的应用程序是这样的:

I have some problems wth autowire annotation. My app looks like this:

下面是控制器:

@Controller
public class MyController {
    @Autowired
    @Qualifier("someService")
    private SomeService someService;

    ....
}

这是一个服务层:

It's a service layer:

public interface SomeService {
    ...
}

@Service
public class SomeServiceImpl implements SomeService{    
    @Autowired
    @Qualifier("myDAO")
    private MyDAO myDAO;

    ....
}

和DAO层:

public interface MyDAO{
    ....        
}

@Repository
public class JDBCDAOImpl implements MyDAO {    
    @Autowired
    @Qualifier("dataSource")
    private DataSource dataSource;    
    ....
}

这是一个应用程序-service.xml文件:

This is a app-service.xml file:

....
<bean id="propertyConfigurer"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
      p:location="/WEB-INF/jdbc.properties" />

<bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource"
      p:driverClassName="${jdbc.driverClassName}"
      p:url="${jdbc.url}"
      p:username="${jdbc.username}"
      p:password="${jdbc.password}"/>

<bean id="SomeService" class="com.service.SomeServiceImpl" />    
<bean id="myDAO" class="com.db.JDBCDAOImpl" />    

所以...当我推出一个web应用程序,Autowires myController的正确(该someService场由SomeServiceImpl类对象正确注入),但myDAO someService的费尔德具有空值(没有正确注入)。

So... When I'm launching a web-app, MyController Autowires correctly (the someService field correctly injected by SomeServiceImpl class object), but myDAO feild of someService has null value (not injected properly).

你能帮我找到问题?

P.S。它的有趣,但是当我改变从myDAO一个豆ID到另一些(例如myDAO2),系统给我一个错误,即注射不能做到,因为豆myDAO不存在。因此,春季做打针,但它在哪里?以及它为什么不能正常工作?

P.S. Its interesting, but when I'm changing a "bean id" from myDAO to some another (e.g. myDAO2), system gives me an error, that injecting could not be done, because bean myDAO doesn't exist. So, Spring make an injection, but where it is? And why it's not work correctly?

推荐答案

我找到解决方案。正如哈维所说(非常感谢你,哈维),我必须标注DAO和服务层类与@Repository和@Service注解。现在我试着这样写:

I find the solution. As Javi said (thanks a lot for you, Javi), I have to annotate DAO and Service layer classes with @Repository and @Service annotation. Now I've tried to write like this:

@Service("someService")
public class SomeServiceImpl implements SomeService{    
    @Autowired
    @Qualifier("myDAO")
    private MyDAO myDAO;

    ....
}

@Repository("myDAO")
    public class JDBCDAOImpl implements MyDAO {    
    @Autowired
    @Qualifier("dataSource")
    private DataSource dataSource;    
    ....
}

和一切工作正常!

但我还是没有找到这个quesion答案:如果应用程序将更为复杂,而且将有更复杂的结构,其中@Repositore和@Service标注没有prefered对于某些类,如何正确地注入豆类,其中位于较低的水平(一个类的字段或类字段的字段)(与@Autowire注释,当然)?

But I still not found an answer for this quesion: if application will be more complex, and will have more complex structure, where @Repositore and @Service annotation doesn't prefered for some classes, how to inject correctly beans, which located in lower levels (in a fields of classes, or in a field of fields of classes) (with @Autowire annotation, of course)?

这篇关于春天@Autowired不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆