- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图在我的 Spring web 应用程序中使用 Hibernate 的注释和数据源,但我有一个错误“NoSuchBeanDefinitionException”,我不知道为什么。从我的角度来看,一切都是正确的......如果有人可以帮助我解决它,那对我来说非常好:)
这是我的 root-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.2.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns="http://www.springframework.org/schema/beans">
<context:component-scan base-package="fot.manager.service, fot.manager.dao" />
<context:annotation-config />
<!-- To use transactions -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/f1_tycoon" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<!-- Hibernate -->
<bean id="sessionFactory" name="connectionDB"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="annotatedClasses">
<list>
<value>fot.manager.model.Bannissement</value>
<value>fot.manager.model.CategorieForum</value>
<value>fot.manager.model.CategorieNews</value>
<value>fot.manager.model.Commentaire</value>
<value>fot.manager.model.MessageForum</value>
<value>fot.manager.model.News</value>
<value>fot.manager.model.Pilote</value>
<value>fot.manager.model.Rang</value>
<value>fot.manager.model.SectionForum</value>
<value>fot.manager.model.Signalement</value>
<value>fot.manager.model.SujetForum</value>
<value>fot.manager.model.Utilisateur</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.format_sql=true
</value>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
网络.xml :
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>fot.manager</display-name>
<welcome-file-list>
<welcome-file>connexion.html</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/root-context.xml</param-value>
</context-param>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Ajax servlet -->
<servlet>
<servlet-name>ajax</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>*.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ajax</servlet-name>
<url-pattern>/ajax/*</url-pattern>
</servlet-mapping>
</web-app>
和堆栈跟踪:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [fot.manager.service.NewsService] 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:986)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:856)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:768)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1122)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:599)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:518)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:459)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5210)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5493)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3988)
at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:425)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1345)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1530)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1519)
at java.lang.Thread.run(Unknown Source)
我还有一个 Java 文件的其他错误堆栈跟踪:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jqGridController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private fot.manager.service.NewsService fot.manager.ajax.jqGridController.newsService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [fot.manager.service.NewsService] 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:288)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1122)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:599)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:518)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:459)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5210)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5493)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3988)
at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:425)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1345)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1530)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1519)
at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private fot.manager.service.NewsService fot.manager.ajax.jqGridController.newsService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [fot.manager.service.NewsService] 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:514)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
... 31 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [fot.manager.service.NewsService] 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:986)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:856)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:768)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
... 33 more
Java 文件:
package fot.manager.ajax;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import fot.manager.model.News;
import fot.manager.service.NewsService;
@Controller
@RequestMapping("/ajax")
public class jqGridController {
@Autowired
private NewsService newsService;
// Contrôleur pour afficher les news
@RequestMapping("/getNews")
@ResponseBody
public Collection<News> getNews(){
Collection<News> listNews = newsService.getAllNews();
for (News news : listNews){
System.out.println(news.getAuteur().getPseudo());
}
return listNews;
}
}
我的 NewsService 类实现:
package fot.manager.service.impl;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import fot.manager.dao.NewsDao;
import fot.manager.model.News;
import fot.manager.service.NewsService;
@Service("newsService")
@Transactional
public class NewsServiceImpl implements NewsService {
@Autowired
private NewsDao newsDao;
@Override
public Collection<News> getAllNews() {
return newsDao.getAllNews();
}
@Override
public News getNewsById(int id) {
return newsDao.getNewsById(id);
}
@Override
public News editNews(News news) {
return newsDao.editNews(news);
}
@Override
public News deleteNews(int id) {
return newsDao.deleteNews(id);
}
}
界面:
package fot.manager.service;
import java.util.Collection;
import fot.manager.model.News;
public interface NewsService {
Collection<News> getAllNews();
News getNewsById(int id);
News editNews(News news);
News deleteNews(int id);
}
调度程序 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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="bgc.isis.generator.controller"/>
<context:annotation-config/>
<!-- Resources -->
<mvc:resources location="/resources/images/, /resources/css/, /resources/js/" mapping="/resources/**"/>
<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />
<!--
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass">
<value>
org.springframework.web.servlet.view.tiles2.TilesView
</value>
</property>
</bean>
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
</beans>
<!-- ViewResolver class : org.springframework.web.servlet.view.UrlBasedViewResolver -->
非常感谢解决我的问题:)
最佳答案
我不确定这个语法是否有效(至少我在文档中找不到它):
<context:component-scan base-package="fot.manager.service, fot.manager.dao" />
我认为您正在寻找:
<context:component-scan base-package="fot.manager">
<context:include-filter type="regex" expression=".*\.service\..*"/>
<context:include-filter type="regex" expression=".*\.dao\..*"/>
</contextn:component-scan>
关于spring - 在 Spring 项目中使用 DataSource en sessionFactory 的 Hibernate 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24083427/
我使用 hibernate 作为持久层来与数据库通信。我使用 maven 插件 hbm2java 来生成 dao、java、*hbm.xml 和 hibernate.cfg.xml。直到那个时候插件工
我想知道上面提到的 hibernates sessionfactory 类的两种方法之间的概念差异,如果可以通过简单的例子。 最佳答案 前者打开一个正常(有状态) session ,而后者(可能不足为
我正在关注 spring doc entry关于在独立应用程序中以编程方式设置我的应用程序上下文。 主要内容: public class Reporter { public static vo
如果我在结束时用 Session.getCurrentTransaction().commit() 关闭 session 或用 session.close() 关闭 session 会发生什么方法?
我正在尝试在我的 Spring 应用程序中使用 Hibernate 进行 Db 访问。但是现在,我收到以下错误: Could not locate SessionFactory in JNDI jav
我在基于 Java 的配置中使用 Hibernate 和 Spring。我的配置文件是这个: @Bean @Autowired public HibernateTransactionManager t
下面的代码不起作用。问题是事务没有在数据库中提交 案例1: @Transactional public void save(Person p) {
我对这两者有点困惑。据我所知,两者都返回 hibernate session ,SessionFactory.getCurrentSession()返回基于属性 的上下文 session 这是在 h
Image 错误: 1) AdminModel.java - 模型类。 2) HibernateUtil.java 促进 Hibernate DB 连接。 3) AdminDAO.java - 你们知
我遇到此错误,我找不到解决方案 我在Grails中使用postgres 这是不断发生的错误 org.springframework.beans.factory.BeanCreationExceptio
我需要 Hibernate 的一个特定功能,即 StatelessSession,为此我需要 Hibernate 的 SessionFactory。问题是我只有entityManagerFactory
我正在开发 jsf 应用程序并使用 hibernate 作为后端。我想创建 session 工厂并在整个应用程序中关闭它一次。我正在使用 util 类创建 session 工厂。 import org
我正在通过读取项目外部的属性文件来创建 session 工厂。我的属性文件如下, hibernate.connection.driver_class=com.mysql.jdbc.Driver hib
我们有一种情况,我们有多个具有相同模式的数据库,但每个数据库中的数据不同。我们正在创建一个 session 工厂来处理这个问题。 问题是我们不知道我们将连接到哪个数据库,直到运行时我们可以提供它。但是
在我的应用程序中,当我需要在这些方法中访问我的数据库时,我一直在我的方法参数中传递我的 SessionFatory。它在我的 Controller 中实例化,使用: @Autowired privat
我对 DataSource 和 SessionFactory 之间的区别感到困惑。 我认为SessionFactory是一个用于检索 session 的管理器(我猜这实际上是与数据库的连接)。 Dat
我正在尝试学习 spring 3 和 DAO 和 BO 类以及如何使用它进行 Autowiring ,我想知道这是连接 sessionFactory 的正确方法,因为我已经读到它更好地使用 publi
我正在将旧应用程序移植到 Hibernate 5,但在登录阶段遇到了问题。这是它的工作原理(我无法更改): 用户最初使用通用登录名/密码(所有用户相同)连接到 oracle DB 然后用户运行“登录”
根据文档,SessionFactory 应该是线程安全的: An ISessionFactory is threadsafe, many threads can access it concurren
如何使用 java 配置创建 SessionFactory? @Bean public SessionFactory sessionFactory(){ AnnotationSessionFa
我是一名优秀的程序员,十分优秀!