bean或xml文件的问题

时间:2017-03-17 09:05:44

标签: java xml spring maven servlets

我正在尝试创建自己的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)在dispatcher-servlet: 我可以把它变成base-package =&#34; controller,dao&#34; ?或者我给出的代码是对的?

2)正如你在applicationContext中看到的,我有两种方法的bean,id =&#34; transactionManager&#34;。第二个是评论。我必须使用哪一个?为什么?

3)我正在运行我的项目,错误与昨天的 org.springframework.beans.factory.BeanCreationException 相同。

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

我真的很抱歉在2天内问同样的事情,但我试图从本周开始就连接数据库而我不能。任何建议都会对我有所帮助。

谢谢大家,迈克

2 个答案:

答案 0 :(得分:0)

  1. 您可以将包名称优化为com.demo.controllercom.demo.daocom.demo.service。然后在dispatcher-servlet.xml中替换它:<context:component-scan base-package="com.demo" />

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

  3. 我不知道您的代码有什么问题,但我注意到您的方言有误,右边是org.hibernate.dialect.MySQLDialect

答案 1 :(得分:0)

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

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