使用@Autowired注释

时间:2016-01-12 13:55:34

标签: java jsp spring-mvc implementation

在Spring中连接组件的正确方法是什么?现在我有两个实现接口的类,如果我@Autowire两个实现类中的接口,Spring会对哪个类感到困惑它应该使用的实现类。所以首先我尝试在两个不同的实现中使用@Component(“name”)注释给它们不同的名称(如果我理解正确的话),但它仍然失败。

我最终做的是在Service类中声明特定的Dao实现(我有多个Dao实现),因为我得到一个错误,说我有两个实现,并且它只期望一个。 ( NoUniqueBeanDefinitionException:没有定义类型的限定bean:期望的单个匹配bean但找到2

问题涉及的代码(如果有帮助,请提出更多请求)

弹簧servlet.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">

    <context:annotation-config />

    <context:component-scan base-package="studyeasy"/>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
          <property name="prefix">
              <value>/WEB-INF/jsp/</value>
           </property>
          <property name="suffix">
             <value>.jsp</value>
          </property>
    </bean>

    <!-- datasource bean -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="***" />
        <property name="username" value="***" />
        <property name="password" value="***" />
    </bean>

    <mvc:resources mapping="/resources/**" location="/resources/" />
    <mvc:annotation-driven/>

</beans>

DatabaseDao.java:

package studyeasy.dao;

import ...;

public interface DatabaseDao {
    public void insertObject(Object obj);
    public List<Object> getObjectList();
    public void updateObject(Object obj);
    public void deleteObject(String id);
    public Object getObject(String id);
}

PersonDaoImpl.java:

package studyeasy.dao;

import javax.sql.DataSource;
import ...;

@Repository
public class PersonDaoImpl implements DatabaseDao {

    @Autowired
    DataSource dataSource;

    @Override
    public void insertObject(Object obj) {
        ...
    }

    ...

}

UtilityDaoImpl.java:

package studyeasy.dao;

import javax.sql.DataSource;
import ...;

@Repository
public class UtilityDaoImpl implements DatabaseDao {
    @Autowired
    DataSource dataSource;

    @Override
    public void insertObject(Object obj) {
        ...
    }

    ...
}

DatabaseService.java:

package studyeasy.services;

import ...;

public interface DatabaseService {
    public void insertObject(Object object);
    public List<Object> getObjectList();
    public void deleteObject(String id);
    public Object getObject(String id);
    public void updateObject(Object object);
}

PersonServiceImpl.java:

package studyeasy.services;

import studyeasy.dao.PersonDaoImpl;
import ...;

@Service
public class PersonServiceImpl implements DatabaseService {

    @Autowired
    PersonDaoImpl personDao;

    @Override
    public void insertObject(Object object) {
        ...
    }

    ...
}

UtilityServiceImpl.java:

package studyeasy.services;

import studyeasy.dao.UtilityDaoImpl;
import ...;

@Service
public class UtilityServiceImpl implements DatabaseService {

    @Autowired
    UtilityDaoImpl utilityDao;

    @Override
    public void insertObject(Object obj) {
        ...
    }

    ...
}

有什么建议吗?

修改

将@ Qualifier添加到两个ServiceImpl类后,我收到此错误:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [studyeasy.dao.DatabaseDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=personDao)}

任何想法为什么?

3 个答案:

答案 0 :(得分:0)

命名您的bean并使用org.springframework.beans.factory.annotation.Qualifier注释来定义要通过其名称连接的bean

示例:

@Autowired
@Qualifier("someDataSource")
DataSource dataSource;

答案 1 :(得分:0)

您需要使用@Qualifier来区分使用哪个候选bean:

@Autowired @Qualifier("fooBean") MyBean bean;

请注意,构造函数注入比字段注入要好得多,如果使用Java配置,则可以使用适当的实现来调用构造函数。

(作为最后一点:如果您使用Spring Data,您可以完全跳过实现DAO并为您自动生成它们。)

答案 2 :(得分:-1)

您需要使用@qualifier,请检查以下链接: https://spring.io/blog/2014/11/04/a-quality-qualifier