Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.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
Java @自动连线储存库空_Java_Spring_Spring Boot_Spring Data Jpa - Fatal编程技术网

Java @自动连线储存库空

Java @自动连线储存库空,java,spring,spring-boot,spring-data-jpa,Java,Spring,Spring Boot,Spring Data Jpa,@Autowired注释注入测试类的ExampleRepository效果良好,但注入服务类的注释为空。为什么会发生这种情况?我如何修复它 存储库类 public interface ExampleRepository extends JpaRepository<Example, Long> { // ... } @Service public class Feed { @Autowired private ExampleRepository examp

@Autowired
注释注入测试类的
ExampleRepository
效果良好,但注入服务类的注释为空。为什么会发生这种情况?我如何修复它

存储库类

public interface ExampleRepository extends JpaRepository<Example, Long> {

    // ...
}
@Service
public class Feed {

    @Autowired
    private ExampleRepository exampleRepository;

    private File file;

    public Feed(String file) {
        this.file = new File(file);
    }

    // ...
}
@ExtendWith(SpringExtension.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@SpringBootTest
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Rollback(false)
public abstract class ExampleTests {

    @Autowired
    private ExampleRepository exampleRepository;

    @BeforeEach
    public void setUp() {
        restoreInitialData();
    }

    protected void restoreInitialData() {
        this.exampleRepository.deleteAll();
    }

    @Test
    public void test() {
        Feed feed = new Feed("example.json");
        Optional<Example> example = feed.ingest();
        assertTrue(example.isPresent());
    }

    // ...
}
测试类

public interface ExampleRepository extends JpaRepository<Example, Long> {

    // ...
}
@Service
public class Feed {

    @Autowired
    private ExampleRepository exampleRepository;

    private File file;

    public Feed(String file) {
        this.file = new File(file);
    }

    // ...
}
@ExtendWith(SpringExtension.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@SpringBootTest
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Rollback(false)
public abstract class ExampleTests {

    @Autowired
    private ExampleRepository exampleRepository;

    @BeforeEach
    public void setUp() {
        restoreInitialData();
    }

    protected void restoreInitialData() {
        this.exampleRepository.deleteAll();
    }

    @Test
    public void test() {
        Feed feed = new Feed("example.json");
        Optional<Example> example = feed.ingest();
        assertTrue(example.isPresent());
    }

    // ...
}
@ExtendWith(SpringExtension.class)
@TestInstance(TestInstance.Lifecycle.PER_类)
@春靴测试
@DataJpaTest
@AutoConfigureTestDatabase(replace=AutoConfigureTestDatabase.replace.NONE)
@回滚(false)
公共抽象类示例测试{
@自动连线
私有示例库示例库;
@之前
公共作废设置(){
恢复初始数据();
}
受保护的无效恢复初始数据(){
这个.exampleRepository.deleteAll();
}
@试验
公开无效测试(){
Feed=newfeed(“example.json”);
可选示例=feed.inset();
assertTrue(例如.isPresent());
}
// ...
}


@Repository
注释添加到接口
示例Repository
@Repository
注释添加到接口
示例Repository

删除
最终
@M.denum问题仍然存在,因为您自己正在创建类的新实例,而不是让Spring管理实例创建。请参见谈论未在
Feed
中注入的变量,您还需要向我们展示如何获得
Feed
实例,以及如何使用它。因此,简而言之,提供@M.Prokhorov好的,我更新了代码。Remove
final
@M.Deinum问题仍然存在,因为您自己正在创建类的新实例,而不是让Spring管理实例创建。请参见谈论未在
Feed
中注入的变量,您还需要向我们展示如何获得
Feed
实例,以及如何使用它。因此,简而言之,提供一个@M.Prokhorov好的,我更新了代码。不,它不是,因为它是Spring数据JPA存储库。JpaRepository不是Spring数据JPA的一部分吗?是的,但你不需要
@repository
来检测它们。Spring数据自动检测扩展Spring数据存储库接口的存储库(这是
JpaRepository
的超级接口之一。不,它不是,因为它是Spring数据JPA存储库。JpaRepository不是Spring数据JPA的一部分吗?是的,但您不需要
@repository
来检测它们。Spring数据自动检测扩展Spring数据
存储库
接口的存储库e(这是
JpaRepository
的超级接口之一)。