![](/img/trans.png)
[英]Spring: Mixing of wiring in JavaConfig with wiring using @Autowired
[英]Wiring implementations in Spring MVC using @Autowired annotation
在Spring中連接組件的正確方法是什么? 現在,我有兩個實現接口的類,如果我在兩個實現類中使用@Autowire接口,則Spring對於應該使用哪個實現類感到困惑。 因此,首先,我嘗試在兩個不同的實現中使用@Component(“ name”)批注給它們提供不同的名稱(如果我正確理解的話),但是仍然失敗。
最后,我最后要做的是在Service類中聲明特定的Dao實現(我有多個Dao實現),因為我遇到一個錯誤,說我有兩個實現,而且只期望一個實現。 ( NoUniqueBeanDefinitionException:未定義類型的合格Bean:預期的單個匹配Bean,但找到2 )
問題中涉及的代碼(如果有幫助,請要求更多)
spring-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)}
有什么想法嗎?
為您的bean命名,並使用org.springframework.beans.factory.annotation.Qualifier批注定義其名稱連接的bean
例:
@Autowired
@Qualifier("someDataSource")
DataSource dataSource;
您需要使用@Qualifier
來區分要使用的候選bean:
@Autowired @Qualifier("fooBean") MyBean bean;
請注意,構造函數注入遠勝於字段注入,如果使用Java配置,則可以使用適當的實現調用構造函數。
(最后一點:如果您使用Spring Data,則可能完全跳過實現DAO,並為您自動生成它們。)
您需要使用@qualifier,請檢查此鏈接: https ://spring.io/blog/2014/11/04/a-quality-qualifier
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.