我们一直在搜索互联网和平装课程,但似乎无法找到解决问题的方法:
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [services.GebruikerDAO] is defined: expected single matching bean but found 2: gebruikerDAO,GebruikerDAO
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:863)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:768)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
... 58 more
要绘制一些背景信息:我们必须使用java jsp页面创建一个小型网站,并使用弹簧安全性和maven使用数据库获取信息并检查信息。以下是我们所有的文件。
这是我们的GebruikerDAO课程:
package services;
import domein.Gebruiker;
public interface GebruikerDAO extends GenericDao<Gebruiker>{
public Gebruiker getGebruikerByStamnummer(String stamnummer);
}
这是我们的JpaGebruikerDao课程:
@Repository("gebruikerDAO")
public class JpaGebruikerDao extends GenericDaoJpa<Gebruiker> implements GebruikerDAO{
@PersistenceContext
private EntityManager em;
public JpaGebruikerDao()
{
super(Gebruiker.class);
}
/*@Override
@Transactional
public List<Gebruiker> findAll() {
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<Gebruiker> criteriaQuery = criteriaBuilder.createQuery(Gebruiker.class);
Root<Gebruiker> gebruiker = criteriaQuery.from(Gebruiker.class);
criteriaQuery.orderBy(criteriaBuilder.desc(gebruiker.get(Gebruiker_.familienaam)));
// JPQL select
criteriaQuery.select(gebruiker);
TypedQuery<Gebruiker> query = em.createQuery(criteriaQuery);
return query.getResultList();
}*/
// CRUD methods
@Override
@Transactional
public Gebruiker update(Gebruiker gebruiker) {
return em.merge(gebruiker);
}
@Override
@Transactional(readOnly = true)
public Gebruiker get(String id) {
return em.find(Gebruiker.class, id);
}
@Override
@Transactional
public void insert(Gebruiker gebruiker) {
em.persist(gebruiker);
}
@Override
@Transactional
public void delete(Gebruiker gebruiker) {
em.remove(em.merge(gebruiker));
}
@Override
public Gebruiker getGebruikerByStamnummer(String stamnummer) {
return get(stamnummer);
}
}
这是我们的Controller类:
@Controller
public class PlanningController {
@Autowired
@Qualifier("GebruikerDAO")
private GebruikerDAO gebruikerDao;
@RequestMapping(value = "/planning", method = RequestMethod.GET)
public String listGuest(Model model)
{
List<Gebruiker> gebruikerlijst = gebruikerDao.findAll();
List<Student> studentlijst = new ArrayList<>();
for (Gebruiker s : gebruikerlijst) {
if (s instanceof Student){
Student student = (Student)s;
if(student.getLokaal() != null)
{
studentlijst.add(student);
}
}
}
// there is more but it just checks for if a timeslot is available in our planning
这是我们的spring-security文件:
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<beans:import resource="applicationContext.xml"/>
<http auto-config="true">
<intercept-url pattern="/admin*" access="hasAnyRole('ROLE_Student', 'ROLE_Promotor', 'ROLE_Coordinator')"/>
<logout logout-success-url="/planning.htm"/>
<access-denied-handler ref="accessDeniedHandler"/>
<remember-me/>
</http>
<authentication-manager>
<authentication-provider>
<password-encoder hash="sha-256"/>
<jdbc-user-service data-source-ref="dataSource"
users-by-username-query="select StamNummer, WachtwoordEncrypted, 'true' as enabled from gebruiker where StamNummer=?"
authorities-by-username-query="select StamNummer, Type as authority from gebruiker where StamNummer=?"
role-prefix="ROLE_"
/>
</authentication-provider>
</authentication-manager>
<beans:bean class="org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler"/>
这是我们的applicationContext文件:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<bean name = "GebruikerDAO" class="services.JpaGebruikerDao"/>
</beans>
这是我们的dispatcher-servlet文件:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.1.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="controller"/>
<context:component-scan base-package="services"/>
<bean id="accessDeniedHandler" class="handler.MyAccessDeniedHandler">
<property name="accessDeniedUrl" value="403"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/bachelorproef"/> <!-- jdbc:mysql://localhost:3306/mysql?zeroDateTimeBehavior=convertToNull-->
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>domein</value>
</list>
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="database" value="MYSQL"/>
</bean>
</property>
</bean>
<bean id="transactionManager"
class=" org.springframework.orm.jpa.JpaTransactionManager ">
<constructor-arg ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
</beans>
这是我们的web.xml文件:
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web- app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml,
/WEB-INF/spring-security2.xml
</param-value>
</context-param>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
这是我们的Gebruiker课程:
@Entity
@Table(name = "gebruiker")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "Type")
public abstract class Gebruiker implements Serializable {
@Id
@Column(name = "StamNummer")
private String stamNummer;
@Column(name = "Familienaam")
private String familienaam;
@Column(name = "Voornaam")
private String voornaam;
@Column(name = "Email")
private String email;
@Column(name = "Wachtwoord")
private String wachtwoord;
@Column(name = "Campus")
private String campus;
@Transient
private String naam;
public Gebruiker() {
}
(+ some extra getters, setters and generatePassword methods)
这是我们的学生班:
@Entity
@DiscriminatorValue("Student")
public class Student extends Gebruiker{
@ManyToOne
private Promotor promotor;
@OneToOne
@JoinColumn(name = "HuidigDossier_DossierId")
private Dossier dossier;
@Temporal(TemporalType.TIMESTAMP)
private Date datumPresentatie;
private String lokaal;
(+ some getters, setters and toString)
这是我们的推广课程:
@Entity
@DiscriminatorValue("Promotor")
public class Promotor extends Gebruiker {
@OneToMany(mappedBy="promotor")
List<Student> studenten;
public Promotor(){
studenten = new ArrayList<>();
}
public List<Student> getStudenten() {
return studenten;
}
public void setStudenten(List<Student> studenten) {
this.studenten = studenten;
}
}
这是我们的BPCoordinator类:
@Entity
@DiscriminatorValue("Coordinator")
public class BPCoordinator extends Gebruiker{
public BPCoordinator(){
}
}
我知道如果NoUniqueBeanDefinitionException
错误发生,有很多信息可以找到,但这几乎总是指实体管理器,在这里他说gebruikerDAO和GebruikerDAO都找不到,我们不知道他在哪里从中获取它们或为什么会发生此错误。大部分代码几乎都是纯粹的其他学校项目和paperbackcode的复制粘贴(我们的课程由我们的课程给出)
想法在何处以及如何解决问题将是惊人的。
答案 0 :(得分:14)
从XML配置中删除<bean name = "GebruikerDAO" class="services.JpaGebruikerDao"/>
(如果配置中存在所有内容,我建议您删除整个文件)。
这是必需的,因为JpaGebruikerDao
已经通过组件扫描(名称为gebruikerDAO
)注册为(存储库)bean。
答案 1 :(得分:1)
从类JpaGebruikerDao中删除@Repository(&#34; gebruikerDAO&#34;)注释,因为它已经注册, 或删除
答案 2 :(得分:0)
此异常有两种可能的解决方案。 1-仅在XML文件中定义类并使用@Qualifier:
public class PlanningController {
@Autowired
@Qualifier("GebruikerDAO")
private GebruikerDAO gebruikerDao;
在XML中仅为PlanningController定义一个bean
2-或者删除Qualifier注释,然后在PlanningController中添加getter和setter或构造函数,然后在XML中使用属性定义上下文:
<bean class="[path of PlanningController]">
<property name="gebruikerDao" ref="[GebruikerDAO implementation bean name]"/>
答案 3 :(得分:0)
在这种情况下,我们可以使用
@Inject
@Named("GebruikerDAO")
private GebruikerDAO gebruikerDao;
这将消除对使用什么实现的混淆。