gpt4 book ai didi

java - Spring Data JPA 返回空存储库

转载 作者:行者123 更新时间:2023-12-02 04:02:19 26 4
gpt4 key购买 nike

使用 Spring Boot 和 Spring Data JPA 时,当 @Autowiring 时我得到空存储库.

我可能错过了一些简单的配置,但经过几个小时的查看和审查文档后,我发现没有任何缺陷。我需要另一双眼睛来重新审视。

相关配置如下:

@SpringBootApplication
@EnableJpaRepositories
@EnableTransactionManagement
public class ServerApp {
public static void main(String[] args) {
SpringApplication.run(ServerApp.class, args);
}
}

使用存储库的服务。执行doSomething()repository一片空白。

@Service
@Slf4j
public class MyService{
@Autowired
private MyRepository repository;

@Override
public AnEntity doSomething(String index) {
return repository.findOneByIndex(index);
}
}

存储库

public interface MyRepository extends   JpaRepository<AnEntity, String> {
AnEntity findOneByIndex(String index);
}

实体

@Entity
@Table(name = "entities")
@Getter
@Setter
@ToString
public class AnEntity implements Serializable{
private static final long serialVersionUID = -6256451671609986911L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String index;
private String data;
}

persistence.xml 文件

<persistence
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="default">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.myapp.AnEntity</class>
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="javax.persistence.transactionType" value="RESOURCE_LOCAL" />
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/authServer" />
<property name="javax.persistence.jdbc.user" value="user" />
<property name="javax.persistence.jdbc.password" value="password" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
</properties>
</persistence-unit>

POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
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.myapp</groupId>
<artifactId>myapp</artifactId>
<version>1.0-SNAPSHOT</version>

<packaging>pom</packaging>

<properties>
<java.version>1.8</java.version>
</properties>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.7.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.plugin</groupId>
<artifactId>spring-plugin-core</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1201-jdbc41</version>
</dependency>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>br.com.six2six</groupId>
<artifactId>fixture-factory</artifactId>
<version>3.0.0</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>

最佳答案

最终,我在使用 new 显式实例化服务的代码中遇到了错误,而不是依赖 Spring 的 CDI。因此,Service 中的 Repository 中的 @Autowire 没有被 Spring 处理。

错误

//explicit instantiation. Spring not in charge so no repo injected
setService(new MyService());

//Spring CDI takes charge and autowires the Service and it's dependencies
@Autowired
private MyService myService;

public void someMethod(){
setService(myService);
}

关于java - Spring Data JPA 返回空存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34764370/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com