- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“homeController”的bean时出错:通过字段“dao”表达的依赖关系不满足;嵌套异常是org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“alienDao”的bean时出错:通过字段“sessionFactory”表达的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.BeanCreationException:
HomeController.java
package com.telusko.springmvc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import com.telusko.springmvc.dao.AlienDao;
import com.telusko.springmvc.model.Alien;
@Controller
public class HomeController
{
@Autowired
AlienDao dao;
@ModelAttribute
public void modelData(Model m)
{
m.addAttribute("name","Aliens");
}
@RequestMapping("/")
public String home()
{
return "index";
}
@GetMapping("getAliens")
public String getAliens(Model m)
{
m.addAttribute("result", dao.getAliens());
return "showAliens";
}
@RequestMapping("addAlien")
public String addAlien(@ModelAttribute Alien a)
{
return "result";
}
}
AlienDao.java
package com.telusko.springmvc.dao;
import java.util.List;
import javax.transaction.Transactional;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import com.telusko.springmvc.model.Alien;
@Service
@Repository
public class AlienDao
{
@Autowired
private SessionFactory sessionFactory;
@Transactional
public List<Alien> getAliens()
{
Session session = sessionFactory.getCurrentSession();
List <Alien> aliens = session.createQuery("from Alien",Alien.class).list();
return aliens;
}
}
Alien.java
package com.telusko.springmvc.model;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Alien
{
@Id
private int aid;
private String aname;
public int getAid() {
return aid;
}
public void setAid(int aid) {
this.aid = aid;
}
public String getAname() {
return aname;
}
public void setAname(String aname) {
this.aname = aname;
}
@Override
public String toString() {
return "Alien [aid=" + aid + ", aname=" + aname + "]";
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Welcome to Telusko
<form action="addAlien" method="POST">
Enter your id : <input type="text" name="aid"><br>
Enter your name : <input type="text" name="aname"><br>
<input type="submit">
</form>
</body>
</html>
结果.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isELIgnored="false"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Result is : ${alien}
Welcome Back ${name}
</body>
</html>
显示外星人
<%@ page language="java" contentType="text/html;
charset=ISO-8859-1"pageEncoding="ISO-8859-1" isELIgnored="false"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
${result}
</body>
</html>
telusko-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:ctx="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<ctx:component-scan base-package="com.telusko"></ctx:component-scan>
<ctx:annotation-config></ctx:annotation-config>
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/telusko"></property>
<property name="user" value="root"></property>
<property name="password" value="12345678"></property>
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="10" />
<property name="maxIdleTime" value="30000" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="packageToScan" value="com.telusko.springmvc.model" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="myTransactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="myTransactionManager" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<servlet>
<servlet-name>telusko</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>telusko</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
错误显示
HTTP Status 500 – Internal Server Error
Type Exception Report
Message Servlet.init() for servlet [telusko] threw exception
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
javax.servlet.ServletException: Servlet.init() for servlet [telusko] threw exception
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:688)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1594)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Unknown Source)
Root Cause
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'homeController': Unsatisfied dependency expressed through field 'dao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'alienDao': Unsatisfied dependency expressed through field 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/telusko-servlet.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'packageToScan' of bean class [org.springframework.orm.hibernate5.LocalSessionFactoryBean]: Bean property 'packageToScan' is not writable or has an invalid setter method. Did you mean 'packagesToScan'?
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643)
org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130)
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422)
最佳答案
在堆栈跟踪之后,您必须将属性 packageToScan
重命名为 packagesToScan
关于java - 不满意的依赖异常 : Error creating bean with name 'homeController' : Unsatisfied dependency expressed through field 'dao' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61188611/
在 Spring Boot 应用程序(v. 1.3.2)中,我尝试使用 jOOQ 生成所有映射数据库表的 POJO,以便它们也被注释为与 JPA 一起使用。数据库是 PostgreSQL。 问题在于
我有一些非常标准的代码,它从一个流中获取一个序列化对象,它基本上看起来像这样: Object getObjectFromStream(InputStream is) { ObjectInpu
在我的 Node.js 代码上运行 JSLint 时,它显示 “'Uint8Array' was used before it was defined 。” Mozilla reference将其置于
我在 Xcode6、beta 6 左右创建了一个项目(今天升级到 b7)。使用 Swift,一直在为 iOS 8 SDK 等开发。 到目前为止,Storyboard 有两个 View Controll
我是一名优秀的程序员,十分优秀!