Java 没有定义类型 [] 的唯一 bean:预期的单个匹配 bean,但发现 2:[dbImpl, DbImpl]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21252759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No unique bean of type [] is defined: expected single matching bean but found 2: [dbImpl, DbImpl]
提问by arpho
Hi I am new to Spring: I have a problem related with the configuration of my project;
嗨,我是 Spring 新手:我有一个与项目配置相关的问题;
this is my servlet-context.xml:
这是我的 servlet-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.metmi.mmasgis" />
<beans:bean id="DbImpl" class="com.metmi.mmasgis.dao.DbImpl">
<beans:property name="dataSource" ref="dataSource" />
</beans:bean>
<beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<beans:property name="driverClassName"
value="com.mysql.jdbc.Driver">
</beans:property>
<beans:property name="username" value="root"></beans:property>
<beans:property name="password" value="vilu7240"></beans:property>
<beans:property name="url"
value="jdbc:mysql://localhost:3306/springschema">
</beans:property>
</beans:bean>
</beans:beans>
this is my controller:
这是我的控制器:
package com.metmi.mmasgis;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.metmi.mmasgis.dao.DbImpl;
import com.metmi.mmasgis.model.Db;
/**
* Handles requests for the application home page.
*/
@Controller
public class HomeController {
@Autowired
DbImpl dbs;
private static final Logger logger = LoggerFactory
.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG,
DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate);
return "home";
}
/**
* get the database list in Mysql
*/
@RequestMapping(value = "/db", method = RequestMethod.GET)
public String dbs(Locale locale, Model model) {
ArrayList<Db> dbList = dbs.getDatabases();
model.addAttribute("dbList", dbList);
return "dbs";
}
/**
* Simply shows ciao.
*/
@RequestMapping(value = "/ciao", method = RequestMethod.GET)
public String ciao(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG,
DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate);
return "ciao";
}
}
It is pretty basic, I get this error when I run it on the server:
这是非常基本的,当我在服务器上运行它时出现此错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'homeController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.metmi.mmasgis.dao.DbImpl com.metmi.mmasgis.HomeController.dbs; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.metmi.mmasgis.dao.DbImpl] is defined: expected single matching bean but found 2: [dbImpl, DbImpl]
采纳答案by Khosrow
In your servlet-context.xml
you wrote:
在你的servlet-context.xml
你写道:
<context:component-scan base-package="com.metmi.mmasgis" />
<beans:bean id="DbImpl" class="com.metmi.mmasgis.dao.DbImpl">
<beans:property name="dataSource" ref="dataSource" />
</beans:bean>
It registers a Component
with type of com.metmi.mmasgis.dao.DbImpl
and name of DbImpl
from XML configuration, and registers this exact same type, with name of dbImpl
due to Component Scanning (autodetected components naming). Just omit the DbImpl bean definition in your config.xml.
它从 XML 配置注册一个Component
类型com.metmi.mmasgis.dao.DbImpl
和名称DbImpl
,并注册这个完全相同的类型,dbImpl
由于组件扫描(自动检测到的组件命名)而使用名称。只需省略 config.xml 中的 DbImpl bean 定义。
回答by Niranjan
Basic issue is there are two beans of same type defined in your spring bean pool. There are two ways to resolve this:
基本问题是在您的 Spring bean 池中定义了两个相同类型的 bean。有两种方法可以解决这个问题:
- As "Khosrow" said, remove DBImpldeclaration from Spring config XML file.
- If you are using @Componentannotation on your DBImplclass, then give a value to that annotation (like @Component("someOtherDbImpl")) and then in your autowiring put another annotation; like:
- 正如“Khosrow”所说,从 Spring 配置 XML 文件中删除DBImpl声明。
- 如果您在DBImpl类上使用@Component注释,则为该注释指定一个值(如@Component("someOtherDbImpl")),然后在您的自动装配中放置另一个注释;喜欢:
@Autowired
@Qualifier("someOtherDbImpl")
DbImpl dbs;