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

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

问题描述

我在自动装配注释方面遇到了一些问题.我的应用程序如下所示:

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

这是控制器:

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

    ....
}

这是一个服务层:

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;    
    ....
}

这是一个 app-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 应用程序时,MyController 自动装配正确(SomeServiceImpl 类对象正确注入了 someService 字段),但是 someService 的 myDAO 字段具有空值(未正确注入).

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).

你能帮我找出问题吗?

附言这很有趣,但是当我将bean id"从 myDAO 更改为另一个(例如 myDAO2)时,系统给了我一个错误,无法完成注入,因为 bean myDAO 不存在.那么,Spring 进行了注入,但它在哪里呢?为什么它不能正常工作?

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?

推荐答案

我找到了解决方案.正如 Javi 所说(非常感谢你,Javi),我必须用 @Repository@Service 注释来注释 DAO 和服务层类.现在我试着这样写:

I found 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;    
    ....
}

一切正常!!!

但是我仍然没有找到这个问题的答案:如果应用程序会更复杂,并且会有更复杂的结构,@Repositore@Service 注释是对于某些类来说不是首选,如何正确注入位于较低级别(在类的字段中,或在类的字段中)的 bean(当然,带有 @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 are not preferred 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)?

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

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