我已经看过了关于这个话题的每一篇文章,但我还是找不出有什么问题。当我尝试运行服务器时,我会遇到以下异常:
Org.SpringFramework.Beans.Factory.UnsatistifiedDependencyException:创建名为“Administrator DAO”的bean时出错:通过方法“set DAO”表示的依赖项不满足,参数0:没有找到依赖项[Com.Project.DataAccessLayer.DataAccessObject]类型的合格bean:需要至少1个bean可以作为此依赖项的自动候选项。依赖项注释:{};嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:未找到依赖项[com.project.dataAccessLayer.dataAccessObject]类型的合格bean[com.project.dataAccessLayer.dataAccessObject]:应至少有1个bean可以作为此依赖项的自动候选项。依赖项批注:{}
它说我没有DataAccessObject的bean,即使它是用@repository注释的或者也是在servlet-context.xml中定义的
我有一个CustomerDAO和一个扩展CustomerDAO的AdministratorDAO。我将只发布客户部分,因为我认为,如果我可以使其工作,管理员部分将跟随。
DataAccessObject class
package com.project.dataAccessLayer;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import com.project.model.Account;
import com.project.model.ModelEntity;
import com.project.model.Product;
@Repository("dao")
public class DataAccessObject {
private SessionFactory sessionFactory;
private Session session;
@Autowired
@Qualifier("sessionFactory")
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public ModelEntity save(ModelEntity modelEntity) {
Integer id = null;
try {
session = sessionFactory.openSession();
session.beginTransaction();
id = (Integer) session.save(modelEntity);
if (id != null) {
modelEntity.setId(id);
}
session.getTransaction().commit();
session.close();
} catch (Exception e) {
this.update(modelEntity);
}
return modelEntity;
}
public void update(ModelEntity modelEntity) {
try {
session = sessionFactory.openSession();
session.beginTransaction();
session.update(modelEntity);
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
}
@SuppressWarnings("unchecked")
public List<Product> getProducts() {
List<Product> list = null;
try {
session = sessionFactory.openSession();
session.beginTransaction();
list = session.createCriteria(Product.class).list();
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
return list;
}
@SuppressWarnings("unchecked")
public List<Account> getAccounts() {
List<Account> list = null;
try {
session = sessionFactory.openSession();
session.beginTransaction();
list = session.createCriteria(Account.class).list();
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
return list;
}
@SuppressWarnings("unchecked")
public List<Product> getProductByName(String brand, String model) {
List<Product> list = null;
try {
session = sessionFactory.openSession();
session.beginTransaction();
list = session.createCriteria(Product.class).add(Restrictions.eq("brand", brand))
.add(Restrictions.eq("model", model)).list();
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
return list;
}
@SuppressWarnings("unchecked")
public List<Account> getAccountByUsername(String username) {
List<Account> list = null;
try {
session = sessionFactory.openSession();
session.beginTransaction();
list = session.createCriteria(Account.class).add(Restrictions.eq("username", username)).list();
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
return list;
}
public void delete(ModelEntity modelEntity) {
try {
session = sessionFactory.openSession();
session.beginTransaction();
session.delete(modelEntity);
session.getTransaction().commit();
session.clear();
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
}
CustomerDAO类
package com.project.dataAccessLayer;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import com.project.model.Account;
import com.project.model.Permission;
import com.project.model.Product;
@Repository("customerDAO")
public class CustomerDAO implements ICustomerDAO {
DataAccessObject dao;
@Autowired
@Qualifier("custDAO")
public void setDao(DataAccessObject dao) {
this.dao = dao;
}
public DataAccessObject getDao() {
return dao;
}
public Account signUp(String fullName, String username, String password, String email, String address,
Permission permission) throws Exception {
List<Account> allAccounts = dao.getAccounts();
for (Account it : allAccounts) {
if (it.getUsername().equals(username)) {
throw new Exception("The username already exists");
}
}
Account newAccount = new Account(fullName, username, password, email, address, permission);
Account savedAccount = (Account) dao.save(newAccount);
return savedAccount;
}
public int logIn(String username, String password) throws Exception {
List<Account> allAccounts = dao.getAccounts();
for (Account it : allAccounts) {
if (it.getUsername().equals(username)) {
{
if (it.getPassword().equals(password)) {
if (it.isOnline() == false) {
it.setOnline(true);
dao.update(it);
return 200;
} else {
throw new Exception("You are already logged in");
}
} else {
throw new Exception("Invalid password");
}
}
}
}
// The username was not found in the database
return 404;
}
public int logOut(int accountId) throws Exception {
List<Account> allAccounts = dao.getAccounts();
for (Account it : allAccounts) {
if (it.getId() == accountId) {
{
if (it.isOnline()) {
it.setOnline(false);
dao.update(it);
return 200;
} else {
throw new Exception("You are not logged in");
}
}
}
}
return 400;
}
public Account changePassword(String username, String oldPassword, String newPassword) throws Exception {
List<Account> allAccounts = dao.getAccounts();
for (Account it : allAccounts) {
if (it.getUsername().equals(username)) {
{
if (it.getPassword().equals(oldPassword)) {
if (it.isOnline()) {
it.setPassword(newPassword);
dao.update(it);
return it;
} else {
throw new Exception("You must be logged in in order to change password");
}
} else {
throw new Exception("Invalid password");
}
}
}
}
return null;
}
public List<Product> showAllProducts() {
return dao.getProducts();
}
public List<Product> getProductByName(String brand, String model) {
return dao.getProductByName(brand, model);
}
}
CustomerService类
package com.project.services;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.project.dataAccessLayer.CustomerDAO;
import com.project.model.Account;
import com.project.model.Permission;
import com.project.model.Product;
@Service("customerService")
@Transactional
public class CustomerService implements ICustomerService {
private CustomerDAO cDAO;
public CustomerDAO getcDAO() {
return cDAO;
}
@Autowired
public void setcDAO(CustomerDAO cDAO) {
this.cDAO = cDAO;
}
@Transactional
public Account signUp(String fullName, String username, String password, String email, String address,
Permission permission) throws Exception {
if (username.equals("") || password.length() < 3 || permission == null) {
throw new Exception("You must provide a username and a minimum 3 character long password");
} else {
return cDAO.signUp(fullName, username, password, email, address, permission);
}
}
@Transactional
public boolean logIn(String username, String password) throws Exception {
if (cDAO.logIn(username, password) == 200)
return true;
return false;
}
@Transactional
public boolean logOut(int accountId) throws Exception {
if (cDAO.logOut(accountId) == 200)
return true;
return false;
}
@Transactional
public Account changePassword(String username, String oldPassword, String newPassword) throws Exception {
if (newPassword.length() < 3) {
throw new Exception("Password must have minimum 3 characters");
}
return cDAO.changePassword(username, oldPassword, newPassword);
}
@Transactional
public List<Product> showAllProducts() {
return cDAO.showAllProducts();
}
@Transactional
public Product getProductByName(String brand, String model) throws Exception {
List<Product> pList = cDAO.getProductByName(brand, model);
if (pList.isEmpty()) {
throw new Exception(brand + " " + model + " does not exist");
} else {
return pList.get(0);
}
}
}
package com.project.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.project.model.Account;
import com.project.model.Product;
import com.project.services.CustomerService;
import com.project.services.ICustomerService;
@Controller("customerCOntroller")
@RequestMapping("/")
public class CustomerController {
ICustomerService customerService;
@Autowired(required = true)
@Qualifier(value="customerService")
public void setCustomerService(CustomerService cs){
this.customerService = cs;
}
@RequestMapping(value = "/account", method = RequestMethod.GET)
public ModelAndView showForm() {
return new ModelAndView("account", "account", new Account());
}
@RequestMapping(value = "/signUp", method = RequestMethod.POST)
public String signUp(@ModelAttribute("account") Account acc) throws Exception {
this.customerService.signUp(acc.getFullName(), acc.getUsername(), acc.getPassword(), acc.geteMail(),
acc.getAddress(), acc.getPermission());
return "redirect:/logIn";
}
@RequestMapping(value = "/logIn", method = RequestMethod.POST)
public String logIn(@ModelAttribute("account") Account acc) throws Exception {
this.customerService.logIn(acc.getUsername(), acc.getPassword());
return "redirect:/products";
}
@RequestMapping(value = "/logOut", method = RequestMethod.POST)
public String logOut(@ModelAttribute("account") Account acc) throws Exception {
this.customerService.logOut(acc.getId());
return "redirect:/logIn";
}
@RequestMapping(value="/changePassword/{newPassword}", method = RequestMethod.POST)
public String changePassword(@ModelAttribute("account") Account acc,
@PathVariable("newPassword") String newPassword) throws Exception {
this.customerService.changePassword(acc.getUsername(), acc.getPassword(), newPassword);
return "redirect:/logIn";
}
@RequestMapping(value = "/products", method = RequestMethod.GET)
public ModelAndView showProducts() {
List<Product> productList = this.customerService.showAllProducts();
return new ModelAndView("products", "productList", productList);
}
}
<....>``
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/root-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="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">
</beans>
<?xml ....">
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/jsp/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.project" />
<!-- 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/" />
<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
<beans:property name="url"
value="jdbc:mysql://localhost:3306/guitarshop" />
<beans:property name="username" value="root" />
<beans:property name="password" value="" />
</beans:bean>
<beans:bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<beans:property name="annotatedClasses">
<beans:list>
<beans:value>com.project.model.Account</beans:value>
<beans:value>com.project.model.Product</beans:value>
</beans:list>
</beans:property>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
</beans:prop>
<beans:prop key="hibernate.show_sql">true</beans:prop>
<beans:prop key="hibernate.enable_lazy_load_no_trans">true</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<!-- <beans:bean id="dao" -->
<!-- class="com.project.dataAccessLayer.DataAccessObject"> -->
<!-- <beans:property name="sessionFactory" ref="sessionFactory" /> -->
<!-- </beans:bean> -->
<!-- <beans:bean id="customerDAO" class="com.project.dataAccessLayer.CustomerDAO"> -->
<!-- <beans:property name="dao" ref="dao" /> -->
<!-- </beans:bean> -->
<!-- <beans:bean id="administratorDAO" -->
<!-- class="com.project.dataAccessLayer.AdministratorDAO"> -->
<!-- <beans:property name="dao" ref="dao" /> -->
<!-- </beans:bean> -->
<!-- <beans:bean id="customerService" class="com.project.services.CustomerService"> -->
<!-- <beans:property name="customerDAO" ref="customerDAO"></beans:property> -->
<!-- </beans:bean> -->
<!-- <beans:bean id="administratorService" class="com.project.services.AdministratorService"> -->
<!-- <beans:property name="administratorDAO" ref="administratorDAO"></beans:property> -->
<!-- </beans:bean> -->
<tx:annotation-driven transaction-manager="transactionManager" />
<beans:bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<beans:property name="sessionFactory"
ref="sessionFactory" />
</beans:bean>
</beans:beans>
我评论了DAO和服务bean,因为我知道@repository、@component和@Service注释将创建这些bean。
我必须在CustomerDAO和AdministratorDAO中都使用DataAccessObject。
您希望在CustomerDAO
中有一个带有@qualifier(“Custdao”)
的依赖关系DataAccessObject
bean,但没有带有@qualifier(“Custdao”)
的DataAccessObject
bean
换句话说,问题是由于@qualifier("CustDAO“)
(即Spring
容器无法将DataAccessObject
bean注入到CustomerDAO
中(因为没有预期的@qualifier
的bean可用),所以您需要更改DataAccessObject
,如下所示:
@Repository
@Qualifier("custDAO")
public class DataAccessObject {
//current code
}
您可以在此查看有关@qualifier
的更多信息
“我是Spring新手,我刚开始一个Spring MVC CRUD项目,但在尝试了很多方法之后,我一次又一次地面临同样的错误。 这是打印HTTP状态500的第一个异常-内部服务器错误。 HTTP状态500–内部服务器错误 javax。servlet。ServletException:Servlet。servlet[dispatcher]的init()引发异常组织。阿帕奇。卡塔琳娜。验证者。Auth
我在尝试启动应用程序时遇到此错误。我看过许多类似的问题和话题,但似乎没有一个对我有帮助。 创建名为“databaseManager”的bean时出错:通过字段“articleRepo”表示的未满足的依赖关系;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“pl.dzejkobdevelopment.da
我是spring MVC的新手。我正面临。我添加了,但仍然面临同样的问题。 上下文初始化过程中遇到异常-取消刷新尝试:org.springframework.beans.factory.unsatisfieddependencyexception:创建名为“user controller”的bean时出错:通过字段“user service”表示的不满足的依赖关系;嵌套异常为org.springf
堆栈跟踪
aliyun-oss bean创建失败 报错信息 代码片段 POM依赖如下 是OSSClient类中没有@Bean注解吗? 还是因为依赖版本冲突
正在尝试使用jpa/hibernate创建基本web服务。但豆子并没有被初始化。有人能帮我吗? 以下是我的Customer Controller.java: 以下是我的ervice.java: 下面是我的客户地址。爪哇: 以下是我的odel.java: 组织。springframework。豆。工厂UnsatisfiedPendencyException:创建名为“customerControll