我无法解决以下异常。请您解决。谢谢。
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.v1forum.service.CoreService com.v1forum.controller.LoginController.coreService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'anCoreService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.v1forum.dao.CoreDao com.v1forum.service.CoreServiceImpl.coreDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.v1forum.dao.CoreDao] is defined: expected single matching bean but found 2: [anCoreDAOImpl, anLoginDAOImpl]
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.v1forum.service.CoreService com.v1forum.controller.LoginController.coreService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'anCoreService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.v1forum.dao.CoreDao com.v1forum.service.CoreServiceImpl.coreDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.v1forum.dao.CoreDao] is defined: expected single matching bean but found 2: [anCoreDAOImpl, anLoginDAOImpl]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'anCoreService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.v1forum.dao.CoreDao com.v1forum.service.CoreServiceImpl.coreDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.v1forum.dao.CoreDao] is defined: expected single matching bean but found 2: [anCoreDAOImpl, anLoginDAOImpl]
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.v1forum.dao.CoreDao com.v1forum.service.CoreServiceImpl.coreDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.v1forum.dao.CoreDao] is defined: expected single matching bean but found 2: [anCoreDAOImpl, anLoginDAOImpl]
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.v1forum.dao.CoreDao] is defined: expected single matching bean but found 2: [anCoreDAOImpl, anLoginDAOImpl]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:800)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
atorg.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject
AutowiredAnnotationBeanPostProcessor.java:478)
Jan 28, 2016 5:16:22 AM org.apache.catalina.core.ApplicationContext log
SEVERE: StandardWrapper.Throwable
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.v1forum.service.CoreService com.v1forum.controller.LoginController.coreService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'anCoreService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.v1forum.dao.CoreDao com.v1forum.service.CoreServiceImpl.coreDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.v1forum.dao.CoreDao] is defined: expected single matching bean but found 2: [anCoreDAOImpl, anLoginDAOImpl]
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.v1forum.service.CoreService com.v1forum.controller.LoginController.coreService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'anCoreService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.v1forum.dao.CoreDao com.v1forum.service.CoreServiceImpl.coreDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.v1forum.dao.CoreDao] is defined: expected single matching bean but found 2: [anCoreDAOImpl, anLoginDAOImpl]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues
(AutowiredAnnotationBeanPostProcessor.java:284)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'anCoreService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.v1forum.dao.CoreDao com.v1forum.service.CoreServiceImpl.coreDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.v1forum.dao.CoreDao] is defined: expected single matching bean but found 2: [anCoreDAOImpl, anLoginDAOImpl]
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.v1forum.dao.CoreDao com.v1forum.service.CoreServiceImpl.coreDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.v1forum.dao.CoreDao] is defined: expected single matching bean but found 2: [anCoreDAOImpl, anLoginDAOImpl]
我的控制员:
@Controller
public class LoginController {
@Autowired
public CoreService coreService;
@Autowired
public LoginService loginService;
@RequestMapping(value = "loginForm.htm", method = RequestMethod.GET)
public String loginForm(HttpServletRequest request, ModelMap map) {
map.put("memBean", new User());
return "loginform";
}
@RequestMapping(value = "memberRegistration.htm", method = RequestMethod.POST)
public String memberRegistration(HttpServletRequest req,
@ModelAttribute("memBean") User userBean, ModelMap map) {
if (userBean.getEmailId() == null) {
return "forward:loginForm.htm";
}
Integer userId = (Integer) coreService.save(userBean);
if (userId != null) {
userBean.setUserId(userId);
map.put("userbean", coreService.save(userBean));
}
return "member-profile";
}
@RequestMapping(value = "memberAuthentication.htm", method = RequestMethod.GET)
public String memberAuthentication(HttpServletRequest req,
@ModelAttribute("memBean") User userBean, ModelMap map) {
if (userBean.getEmailId() == null || userBean.getPassword() == null)
return "forward:loginForm.htm";
map.put("userbean", loginService.authenticateUser(userBean));
return "member-profile";
}
public interface CoreService {
public Object findById(Integer id, Object obj);
}
@Service("anCoreService")
public class CoreServiceImpl implements CoreService {
@Autowired
public CoreDao coreDao;
@Override
public java.lang.Object findById(Integer id, Object obj) {
return coreDao.findById(id, obj);
}
}
public interface CoreDao {
public Object findById(Integer id, Object obj);
}
package com.v1forum.dao;
@Repository("anCoreDAOImpl")
public class CoreDaoImpl extends CustomHibDaoSupport implements CoreDao {
public CoreDaoImpl() {
}
@Override
@Transactional
public Object findById(Integer id, java.lang.Object obj) {
return getHibernateTemplate().get(obj.getClass(), id);
}
}
public interface LoginService {
public User authenticateUser(User user);
}
@Service("anLoginService")
public class LoginServiceImpl extends CoreServiceImpl implements LoginService {
@Autowired
private LoginDao loginDao;
@Override
public User authenticateUser(User user) {
return loginDao.authenticateUser(user);
}
}
public interface LoginDao extends CoreDao {
public User authenticateUser(User user);
}
@Repository("anLoginDAOImpl")
public class LoginDaoImpl extends CoreDaoImpl implements LoginDao {
@Override
@Transactional
public User authenticateUser(User user) {
DetachedCriteria dc = DetachedCriteria.forClass(User.class);
if (user.getEmailId() != null)
dc.add(Restrictions.eq("emailId", user.getEmailId()));
List<User> userList =dc.getExecutableCriteria(getHibernateTemplate().getSessionFactory().getCurrentSession()).list();
return userList != null ? userList.get(0) : null;
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<context:property-placeholder location="classpath:database-config.properties"/>
<context:component-scan base-package="com.v1forum"/>
<context:annotation-config/>
<bean id="jspview" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix"><value>/WEB-INF/jsp/</value></property>
<property name="suffix"><value>.jsp</value></property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driverClassName}"/>
<property name="url" value="${url}"/>
<property name="username" value="root"/>
<property name="password" value="${password}"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.v1forum.vo"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hbm2ddl.auto">update</prop>
<prop key="show_sql">true</prop>
</props>
</property>
</bean>
</beans>
答案 0 :(得分:0)
您必须使用@Qualifier
注释和@Autowired
来指定要注入的bean ID。
您使用CoreDaoImpl
对@Repository("anCoreDAOImpl")
进行了注释。因此将创建一个标识为anCoreDAOImpl
的bean。类似地,对于LoginDaoImpl
,将创建一个标识为anLoginDAOImpl
的bean。
为了自动装配实现类,请使用下面的代码。
IN CoreServiceImpl
@Autowired
@Qualifier("anCoreDAOImpl") //TO inject `CoreDaoImpl`
public CoreDao coreDao;
在LoginServiceImpl
,
@Autowired
@Qualifier("anLoginDAOImpl")//To inject anLoginDAOImpl
private LoginDao loginDao;
添加@Qualifier
可以解决问题。