Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法自动连接Spring数据存储库_Spring_Spring Mvc_Repository_Autowired - Fatal编程技术网

无法自动连接Spring数据存储库

无法自动连接Spring数据存储库,spring,spring-mvc,repository,autowired,Spring,Spring Mvc,Repository,Autowired,我正在用Spring、Hibernate和MySQL数据库创建一个简单的应用程序。我做了一些研究,但没有一个答案是准确的。我有一个存储库接口,它是BaseCrudRepository的扩展,它扩展了ReadOnlyRepository,它是org.springframework.data.repository.repository的扩展。 代码如下: package dziecko.repositories; import dziecko.models.AreaDate; import dzi

我正在用Spring、Hibernate和MySQL数据库创建一个简单的应用程序。我做了一些研究,但没有一个答案是准确的。我有一个存储库接口,它是BaseCrudRepository的扩展,它扩展了ReadOnlyRepository,它是org.springframework.data.repository.repository的扩展。 代码如下:

package dziecko.repositories;

import dziecko.models.AreaDate;
import dziecko.utils.BaseCrudRepository;

public interface AreaDateRepository extends BaseCrudRepository<AreaDate, Integer> {}
java.lang.AssertionError:预期对象不为null

所有实体都是在数据库中创建的。我就是不知道如何正确配置这些Spring存储库。每件事似乎都是这样做的。有人知道如何在这里修复自动布线吗?

所以我在WEB-INF文件夹中有app-context.xml文件,我没有将此上下文附加到测试中。工作版本:

@ContextConfiguration("/META-INF/app-context.xml")
public class SpringConfigurationTest extends AbstractTestNGSpringContextTests {

    @Autowired
    private AreaDateRepository adRepo;

    @Test
    public void shouldWireComponents() {
        Assert.assertNotNull(adRepo);
    }

}

发生这种情况是因为您的测试类没有Spring意识。您需要:

  • 通过Maven依赖项或其他方式将jar包含在(测试)类路径中
  • 从您的测试中,我可以看到您正在使用TestNG,因此上面的jar将为您提供TestNG Spring测试运行程序:
  • 用(假设您没有测试上下文。顺便说一句,这将是一个更好的实践)注释您的类:
  • @ContextConfiguration(位置={“classpath:app context.xml”})
    公共类SpringConfigurationTest扩展了AbstractTestNGSpringContextTests
    {
    //...
    }
    
    您从未在单元测试中创建任何Spring上下文,也没有使用Spring测试运行程序运行测试。Spring如何在测试中自动连接bean?多谢阅读。我不知道我期望的是什么,但我已经花了很多时间在其他地方寻找原因……非常感谢。我将为测试创建上下文。
    <?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"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
    
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/app-context.xml</param-value>
        </context-param>
    
        <listener>
            <listener-class>
                org.springframework.web.context.ContextLoaderListener
            </listener-class>
        </listener>
    </web-app>
    
    <?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:beans="http://www.springframework.org/schema/beans" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
        xmlns:context="http://www.springframework.org/schema/context" 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
    
        <!-- Enable @Controller annotation support -->
        <mvc:annotation-driven />
    
        <jpa:repositories base-package="dziecko.repositories" />
    
        <context:annotation-config />
        <!-- Scan classpath for annotations (eg: @Service, @Repository etc) -->
        <context:component-scan base-package="dziecko.rests, dziecko.services">
            <context:exclude-filter expression="org.springframework.web.bind.annotation.RestController" type="annotation" />
        </context:component-scan>
    
        <!-- JDBC Data Source. -->
        <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://127.0.0.1:3306/dziecko" />
            <property name="username" value="root" />
            <property name="password" value="" />
            <property name="validationQuery" value="SELECT 1" />
        </bean>
    
        <!-- Hibernate Session Factory -->
        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="jpaVendorAdapter">
             <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
            </property>
            <property name="packagesToScan">
                <array>
                    <value>dziecko.models</value>
                </array>
            </property>
            <property name="jpaProperties">
                <props>
                    <prop key="hibernate.hbm2ddl.auto">create</prop>
                    <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
                    <prop key="hibernate.connection.charSet">UTF-8</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.format_sql">true</prop>
                    <prop key="jadira.usertype.autoRegisterUserTypes">true</prop>
                    <prop key="jadira.usertype.databaseZone">jvm</prop>
                    <prop key="hibernate.id.new_generator_mappings">true</prop>
                </props>
            </property>
        </bean>
    
        <!-- Hibernate Transaction Manager -->
        <bean id="transactionManager"
            class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory" />
        </bean>
    
        <!-- Activates annotation based transaction management -->
        <tx:annotation-driven transaction-manager="transactionManager" />
    </beans>
    
    package dziecko.tests;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.testng.Assert;
    import org.testng.annotations.Test;
    
    import dziecko.repositories.AreaDateRepository;
    
    public class SpringConfigurationTest {
    
        @Autowired
        private AreaDateRepository adRepo;
    
        @Test
        public void shouldWireComponents() {
            Assert.assertNotNull(adRepo);
        }
    }
    
    @ContextConfiguration("/META-INF/app-context.xml")
    public class SpringConfigurationTest extends AbstractTestNGSpringContextTests {
    
        @Autowired
        private AreaDateRepository adRepo;
    
        @Test
        public void shouldWireComponents() {
            Assert.assertNotNull(adRepo);
        }
    
    }