嗨,我是Spring的新手:我有一个与我的项目配置相关的问题;
<?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>
这是我的控制器:
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";
}
}
它是非常基本的,当我在服务器上运行它时,我得到了这个错误:
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]
在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>
它注册一个组件
,其类型为com.metmi.mmasgis.dao.dbimpl
,名称为dbimpl
,XML配置中的名称为dbimpl
,由于组件扫描(自动检测到的组件命名),它注册这个完全相同的类型,名称为dbimpl
。只需省略config.xml中的DbImpl bean定义即可。
现在在我的适配器项目中,我有CommonAdapter.java的实现类一个是EndecaAdapetr.java,另一个是SolarAdapter.java 现在在我的服务项目中,希望基于输入调用上述两个类的服务方法。
问题内容: 我是spring spring 3.2。这是我的配置文件 这是我的课 现在,当我朗读代码时,出现以下错误 为什么我收到此错误。我该如何解决? 谢谢 问题答案: 我今天有同样的问题。解决了以下问题: 首先,我将参数unitName添加到@PersistenceContext到两个实体管理器属性: 在我的配置文件中,我向bean定义添加了一个属性persistenceUnitName:
我在MyBatis中使用了Spring,它在单个数据库中运行得非常好。我在尝试添加另一个数据库时遇到了困难(请参见Github上的可复制示例)。 你能看出我哪里出了问题吗?
问题内容: 我正在尝试使用Spring为webapp自动装配一些bean(用于依赖注入)。一个控制器bean包含另一个bean,而另一个bean又持有另一组bean的hashmap。目前,该地图只有一个条目。当我在tomcat中运行并调用服务时,我收到一条错误消息,说第二个bean(保存在控制器中)不是唯一的 我看不到我在两次定义bean的地方,但是这是Spring的新知识,并且是自动装配的,因此
我有两个实体管理器配置用于两个单独的数据库,但当我尝试自动连接实体管理器来配置GraphQLExecutor bean时,我得到一个异常,指出有两个bean符合条件,尽管我在PersistenceContext中指定了一个单元名。 例外情况 注射 pom.xml