我知道这个问题被问了很多次,但我真的很难克服它。我通过看到这些问题尝试了很多组合,但似乎没有人适合我的情况。
完整的日志错误如下:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'professorController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.personal.online.dao.ProfessorDao com.personal.online.controller.ProfessorController.professorDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.personal.online.dao.ProfessorDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:643)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:606)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:657)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:525)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:466)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
at javax.servlet.GenericServlet.init(GenericServlet.java:160)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:865)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:136)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.personal.online.dao.ProfessorDao com.personal.online.controller.ProfessorController.professorDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.personal.online.dao.ProfessorDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
... 34 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.personal.online.dao.ProfessorDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1100)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:960)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
... 36 more
jan 22, 2015 9:51:21 PM org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Allocate exception for servlet appServlet
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.personal.online.dao.ProfessorDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1100)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:960)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:643)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:606)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:657)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:525)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:466)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
at javax.servlet.GenericServlet.init(GenericServlet.java:160)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:865)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:136)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
这是我目前的实施:
控制器:
package com.personal.online.controller;
// imports
@Transactional
@Controller
public class ProfessorController {
@Autowired
private ProfessorDao professorDao;
@RequestMapping("cadastrarProfessor")
public String cadastrar() {
professorDao.persist(new Professor("some name"));
return "professor/formulario";
}
}
接口:
package com.personal.online.dao;
public interface ProfessorDao {
void persist(Professor professor);
}
存储库:
package com.personal.online.dao;
@Repository
public class JpaProfessorDao implements ProfessorDao {
@PersistenceContext
EntityManager em;
@Transactional
public void persist(Professor professor) {
em.persist(professor);
}
}
型号:
package com.personal.online.model;
@Entity
@Table(name = "Professor")
public class Professor implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long id;
private String nome;
public Professor() {
}
public Professor(String nome) {
this.nome = nome;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
}
Spring配置:
<annotation-driven />
<resources mapping="/resources/**" location="/resources/" />
<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>
<beans:bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<beans:ref bean="jsonMessageConverter" />
</beans:list>
</beans:property>
</beans:bean>
<beans:bean id="jsonMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</beans:bean>
<beans:bean id="mysqlDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
<beans:property name="url"
value="jdbc:mysql://localhost:3306/PersonalOnline" />
<beans:property name="username" value="root" />
<beans:property name="password" value="" />
</beans:bean>
<beans:bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<beans:property name="dataSource" ref="mysqlDataSource" />
<beans:property name="jpaVendorAdapter">
<beans:bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</beans:property>
</beans:bean>
<beans:bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<beans:property name="entityManagerFactory" ref="entityManagerFactory" />
</beans:bean>
<tx:annotation-driven />
<context:component-scan base-package="com.personal.online.controller" />
的persistence.xml:
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.personal.online.model.Professor</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/personal_online" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
答案 0 :(得分:9)
将JpaProfessorDao
的包添加到要扫描的包列表中
<context:component-scan base-package="com.personal.online.controller, com.personal.online.dao" />
答案 1 :(得分:2)
在您的sprint配置中,您有<context:component-scan base-package="com.personal.online.controller" />
,这意味着它只会在该包下查找带注释的bean定义,因此,它找不到您的存储库。要么像@ ha9u63ar建议的那样将它添加到.xml配置中,要么将必要的包添加到component-scan元素中。您可以添加多个包,用逗号分隔,或者只是将“基数”提高一级:<context:component-scan base-package="com.personal.online" />
答案 2 :(得分:0)
当我创建3级Spring MVC项目时,有一个这样的例外。 我的错误是: 在服务层我创建了目录&#34; 资源&#34;其中找到了service-context.xml。但是这个目录应该被称为&#34; 资源&#34; (根据Maven规范)。
答案 3 :(得分:0)
在 Springframework 中,使用 var chart = new Highcharts.Chart({
chart: {
renderTo: "container-main-bar",
type: "line"
},
title: {
text: null
},
series: [{
"name": "Department A",
"data": [{
"name": "Month1",
"y": 27,
"drilldown": "levelA2"
},
{
"name": "Month2",
"y": 24,
"drilldown": "levelA2"
},
{
"name": "Month3",
"y": 22,
"drilldown": "levelA2"
},
{
"name": "Month4",
"y": 26,
"drilldown": "levelA2"
},
{
"name": "Month5",
"y": 21,
"drilldown": "levelA2"
},
{
"name": "Month6",
"y": 22,
"drilldown": "levelA2"
},
{
"name": "Month7",
"y": 23,
"drilldown": "levelA2"
},
{
"name": "Month8",
"y": 24,
"drilldown": "levelA2"
},
{
"name": "Month9",
"y": 21,
"drilldown": "levelA2"
},
{
"name": "Month10",
"y": 20,
"drilldown": "levelA2"
},
{
"name": "Month11",
"y": 22,
"drilldown": "levelA2"
},
{
"name": "Month12",
"y": 25,
"drilldown": "levelA2"
},
{
"name": "Month13",
"y": 23,
"drilldown": "levelA2"
},
{
"name": "Month14",
"y": 24,
"drilldown": "levelA2"
},
{
"name": "Month15",
"y": 25,
"drilldown": "levelA2"
},
{
"name": "Month16",
"y": 26,
"drilldown": "levelA2"
},
{
"name": "Month17",
"y": 24,
"drilldown": "levelA2"
},
{
"name": "Month18",
"y": 23,
"drilldown": "levelA2"
},
{
"name": "Month19",
"y": 23,
"drilldown": "levelA2"
},
{
"name": "Month20",
"y": 25,
"drilldown": "levelA2"
},
{
"name": "Month21",
"y": 25,
"drilldown": "levelA2"
},
{
"name": "Month22",
"y": 25,
"drilldown": "levelA2"
},
{
"name": "Month23",
"y": 25,
"drilldown": "levelA2"
},
{
"name": "Month24",
"y": 25,
"drilldown": "levelA2"
}
],
"drilldown": {
"series": [{
"name": 'Headcount',
"id": 'levelA2',
"data": [{
"name": "Month1",
"y": 10,
},
{
"name": "Month2",
"y": 12,
},
{
"name": "Month3",
"y": 11,
},
{
"name": "Month4",
"y": 10,
},
{
"name": "Month5",
"y": 9,
},
{
"name": "Month6",
"y": 8,
},
{
"name": "Month7",
"y": 10,
},
{
"name": "Month8",
"y": 12,
},
{
"name": "Month9",
"y": 11,
},
{
"name": "Month10",
"y": 13,
},
{
"name": "Month11",
"y": 14,
},
{
"name": "Month12",
"y": 10,
},
{
"name": "Month13",
"y": 9,
},
{
"name": "Month14",
"y": 8,
},
{
"name": "Month15",
"y": 11,
},
{
"name": "Month16",
"y": 10,
},
{
"name": "Month17",
"y": 9,
},
{
"name": "Month18",
"y": 10,
},
{
"name": "Month19",
"y": 11,
},
{
"name": "Month20",
"y": 12,
},
{
"name": "Month21",
"y": 13,
},
{
"name": "Month22",
"y": 10,
},
{
"name": "Month23",
"y": 11,
},
{
"name": "Month24",
"y": 12,
}
]
}]
}
}, ],
xAxis: {
categories: [{
"name": "2013",
"categories": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
},
{
"name": "2014",
"categories": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
},
{
"name": "2015",
"categories": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
},
{
"name": "2016",
"categories": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
},
{
"name": "2017",
"categories": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
},
{
"name": "2018",
"categories": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
}
]
},
yAxis: [{ // Primary yAxis
title: {
text: 'No. of Employees'
},
}],
});
批注将对存储库软件包的扫描转发到配置类。
示例:
@EnableJpaRepositories
答案 4 :(得分:-1)
您的applicationContext.xml文件中没有以下bean定义:
@Autowired
private ProfessorDao professorDao;
您需要为此创建部分。它应该工作!
<bean id="professorDao" class="com.personal.online.dao.ProfessorDao"/>
如有必要,请确保设置构造函数参数。