使用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>
答案 0 :(得分:2)
最终,我在使用new
显式实例化服务的代码中出错,而不是依赖于Spring的CDI。因此,Spring中没有处理服务存储库中的@Autowire in。
WRONG
//explicit instantiation. Spring not in charge so no repo injected
setService(new MyService());
RIGHT
//Spring CDI takes charge and autowires the Service and it's dependencies
@Autowired
private MyService myService;
public void someMethod(){
setService(myService);
}
答案 1 :(得分:0)
按如下方式更改MyRepository类:
public interface MyRepository extends JpaRepository<AnEntity, Long> {
public AnEntity findOneByData(String data);
public AnEntity findOneByIndex(String index);
}