我正在做一个GWTP项目,并使用Spring Data JPA与oracle数据库建立连接。我已经阅读了几个教程,其中直接使用了存储库接口而没有使用实现。在需要的地方@Autowired它工作得很好。我试图使用相同的策略,但似乎@Autowired注释根本不起作用。
这是我的存储库:
@Repository
public interface BugRepository extends JpaRepository<Bug, Long> {
List<Bug> findAll();
.....
}
我尝试在服务实现中使用@Autowired注入它(我使用RESTful服务):
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/bugs")
@Component
public class BugServiceImpl{
@Autowired
private BugRepository bugRepository;
@GET
@Path("/findAll")
public List<Bug> findAll() {
return bugRepository.findAll();
}
}
这是我的实体:
@Entity
@Table(name = "BUGS")
@SequenceGenerator(name = "BUG_SEQUENCE", sequenceName = "BUG_SEQUENCE")
public class Bug implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "BUG_SEQUENCE")
@Column(name="BUG_ID")
private Long bugId;
@Column(name="BUG_NAME")
private String bugName;
@OneToOne
@PrimaryKeyJoinColumn
@Column(name="CREATED_BY")
private User createdBy;
@OneToOne
@PrimaryKeyJoinColumn
@Column(name="ASSIGNED_TO")
private User assignedTo;
@Column(name="CREATION_DATE")
private Date creationDate;
@Column(name="LAST_UPDATE_DATE")
private Date lastUpdateDate;
@Column(name="BUG_COMMENT")
private String bugComment;
@OneToOne(cascade = CascadeType.ALL, optional = false, fetch = FetchType.EAGER, orphanRemoval = true)
@PrimaryKeyJoinColumn
@Column(name="PRIORITY_ID")
private Priority priority;
@OneToOne
@PrimaryKeyJoinColumn
private Status status;
public Bug() {
}
}
我在main / resources / META-INF中也有applicationContext.xml和persistence.xml。这是我的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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
<context:component-scan base-package="com.edu" />
<jpa:repositories base-package="com.edu.server.repositories" />
<context:annotation-config />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<property name="url" value="***"/>
<property name="username" value="***"/>
<property name="password" value="***"/>
</bean>
<!-- EntityManagerFactory -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:packagesToScan="com.edu.shared.entity"
p:dataSource-ref="dataSource"
>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="true" />
<property name="showSql" value="false" />
</bean>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
我的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">
<!-- oracle -->
<persistence-unit name="oracle">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.edu.server.service.BugServiceImpl</class>
<class>com.edu.server.repositories.BugRepository</class>
<class>com.edu.shared.entity.Bug</class>
<properties>
<property name="hibernate.archive.autodetection" value="class" />
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver" />
<property name="hibernate.connection.url" value="***" />
<property name="hibernate.connection.username" value="***" />
<property name="hibernate.connection.password" value="***" />
<property name="hibernate.flushMode" value="FLUSH_AUTO" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
最后我得到的例外是:
java.lang.NullPointerException
com.edu.server.service.BugServiceImpl.findAll(BugServiceImpl.java:39)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
...
当我调试代码并将断点放到自动装配的存储库时,它看起来是空的,所以我认为它没有正确注入,这就是为什么调用方法findAll会触发NullPointerException。那么,为什么你认为@Autowired注释不起作用?
答案 0 :(得分:0)
我认为您正在混合两种Spring / JPA配置方式。上次当我使用XML配置Spring / JPA项目时,我只使用DataSource
bean而没有persistence.xml
配置来连接数据库。我建议你阅读Spring Data的官方文档。社区拥有最好的文档之一。
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/orm.html
答案 1 :(得分:0)
我看到的第一个问题是您已使用@Repository
你不应该那样做,但如下:
//no annotation here
public interface BugRepository extends JpaRepository<Bug, Long> {
List<Bug> findAll();
.....
}
其次,请确保您的BugRepository
界面位于以下包中,否则无法使用:
<jpa:repositories base-package="com.edu.server.repositories" />
第三次我注意到的是persistance.xml
,您不仅注意到@Entity
个bean,还注明了@Service
和@Repository
。您应该只有@Entity
个bean(要管理的)
最后,您似乎在混合Spring
和Jersey
,因此请确保正确设置了Spring容器(应用程序/ Web上下文),因此它可以管理(注入)你的bean / repos / services。
答案 2 :(得分:0)
我要感谢大家的帮助。我解决了我的问题。我的配置文件存在问题。
首先,我真的不需要任何persistence.xml,因为我在applicationContext.xml中创建了一个dataSource bean,其中包含了有关的所有必需信息。与我的数据库连接。我想,你不应该混合这两件事。
其次,您应该正确配置Spring和Jersey之间的链接。我不得不在我的pom.xml中添加一些新的依赖项,这些依赖项是链接Spring和Jersey所必需的(有一个我不知道存在的jersey-spring3依赖项)。所以,现在我使用的所有依赖关系都是关于Spring和Jersey的:
pipe(first())
此外,我必须配置我的web.xml,以便Jersey可以读取applicationContext.xml。如果不配置我的web.xml,applicationContext.xml就没用了,这就是为什么注释和与数据库的连接不起作用的原因。这是我的web.xml:
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>${javax.rs.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.fusesource.restygwt</groupId>
<artifactId>restygwt</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>1.1.2.RELEASE</version>
<type>pom</type>
<!--<scope>import</scope>-->
<scope>compile</scope>
</dependency>
<!-- DataSource (HikariCP) -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.2.5</version>
</dependency>
<!-- JPA Provider (Hibernate) -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.8.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.10.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>12.1.0.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.8.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>${jersey.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
</exclusions>
</dependency>
据我所知, <?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_2_5.xsd"
version="2.5">
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:META-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>
org.glassfish.jersey.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.edu</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
确保了网络配置&#34;倾听&#34;对于其他配置xml文件,以及现在如何读取和使用applicationContext.xml。并使用这些设置
ContextLoadListener
我确保我的软件包将被扫描以查找@Provider和@Path注释,如果没有这些代码,我的服务将无法激活。 有关此提供程序包设置的更多信息,请参阅此处:
我希望我的问题和这个答案对所有具有类似配置问题的人都有用。