【问题标题】:Problems with beans or xml filesbean 或 xml 文件的问题
【发布时间】:2017-08-08 18:11:11
【问题描述】:

我正在尝试创建自己的 eshop。首先,我在连接数据库时遇到问题。 我有以下文件:

Class HomeController.java (with the @Autowired) package controllers
Interface ProductDao.java package dao
Class ProductDaoImpl implements ProductDao (with @Repository, @Transactional and a @Autowired SessionFactory) package dao.impl
Class Product (with the @Entity) package model.

我还有一个 jdbc.properties 文件,其中包含属性文件夹中的用户名、密码、驱动程序、url 信息。 我认为所有这些文件都是正确的。

所以我可能会对以下文件有问题

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"     
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">

        <modelVersion>4.0.0</modelVersion>
        <groupId>com.mywebsite</groupId>
        <artifactId>emusicstore</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>war</packaging>
        <dependencies>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>4.1.4.RELEASE</version>
            </dependency>


            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>4.1.1.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-orm</artifactId>
                <version>4.1.4.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>4.0.1.Final</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate.javax.persistence</groupId>
                <artifactId>hibernate-jpa-2.0-api</artifactId>
                <version>1.0.1.Final</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>6.0.5</version>
            </dependency>



            <dependency>
                <groupId>jstl</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>

            <dependency>
                <groupId>taglibs</groupId>
                <artifactId>standard</artifactId>
                <version>1.1.2</version>
            </dependency>

        </dependencies>

        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.5.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">


                <listener>
                    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
                </listener>
                <context-param>
                    <param-name>contextConfigLocation</param-name>
                    <param-value>
                    WEB-INF/dispatcher-servlet.xml,
                    WEB-INF/applicationContext.xml      
                </param-value>
                </context-param>
                <!-- end of adding listener -->
                <servlet>
                    <servlet-name>dispatcher</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>/</url-pattern>
                </servlet-mapping>

                <!-- add listener -->

                <description>eShop Database</description>
                <resource-ref>
                    <description>DB Connection</description>
                    <res-ref-name>jdbc/myeshop</res-ref-name>
                    <res-type>javax.sql.DataSource</res-type>
                    <res-auth>Container</res-auth>
                </resource-ref>
            </web-app>

调度程序-servlet.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:context="http://www.springframework.org/schema/context"
            xmlns:mvc="http://www.springframework.org/schema/mvc" 
            xmlns:tx="http://www.springframework.org/schema/tx"
            xsi:schemaLocation="http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
                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
                http://www.springframework.org/schema/tx 
                http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">

            <context:component-scan base-package="controllers" />

            <mvc:annotation-driven />

            <bean id="viewResolver"
                class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="prefix" value="/WEB-INF/views/" />
                <property name="suffix" value=".jsp" />
            </bean>

            <mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />

            <tx:annotation-driven />
        </beans>

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:tx="http://www.springframework.org/schema/tx"
                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-4.1.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">

                <beans profile="dev">
                    <context:property-placeholder location="properties/jdbc.properties" />
                    <bean id="dataSource"
                        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
                        <property name="driverClassName" value="${jdbc.driver}"></property>
                        <property name="url" value="${jdbc.url}"></property>
                        <property name="password" value="${jdbc.password}"></property>
                        <property name="username" value="${jdbc.username}"></property>
                    </bean>

                    <bean id="sessionFactory"
                        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
                        <property name="dataSource" ref="dataSource"></property>
                        <property name="hibernateProperties">
                            <props>
                                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                                <prop key="hibernate.hbm2ddl.auto">update</prop>
                                <prop key="hibernate.show_sql">true</prop>
                                <prop key="hibernate.format_sql">true</prop>

                            </props>
                        </property>
                        <property name="packagesToScan">
                            <list>
                                <value>dao.**.*</value>
                            </list>
                        </property>
                    </bean>

                    <bean id="transactionManager"
                        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
                        <property name="sessionFactory" ref="sessionFactory"></property>
                    </bean>

                    <!-- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
                        <property name="dataSource" ref="dataSource"></property> 
                    </bean> -->

                    <tx:annotation-driven />
                </beans>

            </beans>

我昨天问了同样的问题。我试图用给定的答案进行调试,但没有。 我的问题是: 1)在调度程序servlet: 我可以把它设为 base-package="controllers, dao" 吗?还是我给定的代码是正确的?

2) 正如您在 applicationContext 中看到的,我有 2 种 id="transactionManager" 的 bean 方式。第二个是评论。我必须使用哪一个?为什么?

3) 我正在运行我的项目,错误和昨天一样org.springframework.beans.factory.BeanCreationException

4) 所有课程都在Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:

我真的很抱歉在 2 天内提出同样的问题,但我从本周开始尝试连接数据库,但我不能。任何建议都会对我有很大帮助。

谢谢大家,迈克

【问题讨论】:

    标签: java xml spring maven servlets


    【解决方案1】:
    1. 您可以将包名称细化为com.demo.controllercom.demo.daocom.demo.service。然后在 dispatcher-servlet.xml 中替换这个:&lt;context:component-scan base-package="com.demo" /&gt;

    2. HibernateTransactionManager 用于休眠,DataSourceTransactionManager 用于普通 JDBC。

    3. 我不知道你的代码有什么问题,但是我注意到你的方言是错误的,正确的是org.hibernate.dialect.MySQLDialect

    【讨论】:

    • 同样的问题...还有其他想法吗?
    【解决方案2】:

    尝试将此添加到上下文并测试

     <bean id="productDao" class="dao.impl.ProductDaoImpl">
    

    【讨论】:

    • 你的意思是在哪里添加这个上下文?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多