applicationContext.xml、设置 Hibernate、Spring MVC 和 MySql 的问题

发布于 2024-12-25 00:10:54 字数 4322 浏览 4 评论 0原文

我正在尝试使用 Maven 设置 SpringMVC 3.0 + Hibernate + MySql 。 applicationContext.xml 遇到令人沮丧的问题。 DAO 服务的控制器可以正常工作,只是我在将 Hibernate 设置为 MySql 时遇到问题。

在网上搜索了很多东西,但不同的教程有不同的说明,所以我现在一团糟。

applicationContext.xml

<?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/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<context:component-scan base-package="com.ray.service.blog" />
<mvc:annotation-driven />

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

<bean id="blogDao" class="com.ray.service.blog.dao.BlogDao">
    <!-- <property name="sessionFactory" ref="sessionFactory"></property> -->
    <constructor-arg ref="sessionFactory" />
</bean>

<bean id="blogService" class="com.ray.service.blog.services.BlogService" />

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
    <property name="configurationClass">
        <value>org.hibernate.cfg.AnnotationConfiguration</value>
    </property>
    <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="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/ray" />
    <property name="username" value="root" />
    <property name="password" value="root" />
</bean>

这是堆栈跟踪的前几行:

[2012-01-04 10:17:27,265] ERROR [org.springframework.web.context.ContextLoader] Context initialization failed 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'blogController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.ray.service.blog.controllers.BlogController.setBlogService(com.ray.service.blog.services.BlogService); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'blogService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.ray.service.blog.services.BlogService.setBlogDao(com.ray.service.blog.dao.BlogDao); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'blogDao' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'sessionFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)

如果您需要更多信息,请告诉我...谢谢!

I am trying to setup SpringMVC 3.0 + Hibernate + MySql with Maven.
Having frustrating issues with the applicationContext.xml.
The controller to the service to the DAO works, just that I am having problems setting up Hibernate to MySql.

Googled lots of stuff online but different tutorials have different instructions so I'm all messed up now.

applicationContext.xml

<?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/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<context:component-scan base-package="com.ray.service.blog" />
<mvc:annotation-driven />

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

<bean id="blogDao" class="com.ray.service.blog.dao.BlogDao">
    <!-- <property name="sessionFactory" ref="sessionFactory"></property> -->
    <constructor-arg ref="sessionFactory" />
</bean>

<bean id="blogService" class="com.ray.service.blog.services.BlogService" />

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
    <property name="configurationClass">
        <value>org.hibernate.cfg.AnnotationConfiguration</value>
    </property>
    <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="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/ray" />
    <property name="username" value="root" />
    <property name="password" value="root" />
</bean>

Here's the first few lines of the stack trace:

[2012-01-04 10:17:27,265] ERROR [org.springframework.web.context.ContextLoader] Context initialization failed 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'blogController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.ray.service.blog.controllers.BlogController.setBlogService(com.ray.service.blog.services.BlogService); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'blogService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.ray.service.blog.services.BlogService.setBlogDao(com.ray.service.blog.dao.BlogDao); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'blogDao' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'sessionFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)

Let me know if you need more information... thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

隔岸观火 2025-01-01 00:10:54

要解决该错误,请将其添加到您的 pom.xml

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${slf4j.version}</version>
    </dependency>

要改善您的连接,请尝试使用类似的内容

<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:3306/ray" />
    <property name="username" value="root" />
    <property name="password" value="root" />
</bean>

,并在 pom.xml 中您需要添加

<dependency>
    <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
    <version>1.4</version>
</dependency>

在我们的项目中,我们通常使用此定义或 c3p0

Future:
考虑添加以下属性以拥有池连接

<property name="initialSize" value="${hibernate.initialSize}" />
<property name="minIdle" value="${hibernate.minIdle}" />
<property name="maxActive" value="${hibernate.maxActive}" />

并查看此 bean 以将配置属性导出到专用文件

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="locations">
            <list>
                <value>classpath:hibernate_jdbc.properties</value>  
            </list>
        </property>
    </bean>

To solve the error add this to your pom.xml

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${slf4j.version}</version>
    </dependency>

To improve your connection try using something like this

<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:3306/ray" />
    <property name="username" value="root" />
    <property name="password" value="root" />
</bean>

and in the pom.xml you need to add

<dependency>
    <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
    <version>1.4</version>
</dependency>

In our projects we usually use this definition or the one from c3p0

Future:
Consider adding the following properties to have pooled connection

<property name="initialSize" value="${hibernate.initialSize}" />
<property name="minIdle" value="${hibernate.minIdle}" />
<property name="maxActive" value="${hibernate.maxActive}" />

And have look at this bean to export your configuration properties to dedicated files

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="locations">
            <list>
                <value>classpath:hibernate_jdbc.properties</value>  
            </list>
        </property>
    </bean>
屋檐 2025-01-01 00:10:54

您可能缺少 slf4j 的绑定。

Hibernate 在 3.3.x 中切换到 slf4j。

此处列出的绑定之一添加到项目的 classpath/pom.xml 中,它应该可以工作。

You are probably missing a binding for slf4j.

Hibernate switched to slf4j in 3.3.x.

Add one of the bindings listed here to your project's classpath/pom.xml and it should work.

绝不放开 2025-01-01 00:10:54

看起来罪魁祸首是NoClassDefFoundError。 org/slf4j/impl/StaticLoggerBinder.
您需要在类路径中包含适当的 jar。您拥有的 hibernate 提取物应该有一个 slf4j jar。

Looks like the culprit is NoClassDefFoundError. org/slf4j/impl/StaticLoggerBinder.
You need to include the appropriate jar in the classpath. The hibernate extract you have should have an slf4j jar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文