繁体   English   中英

没有EJB3(javax)且没有Springframework JBOSS AS7的Spring-Data(JPA)

[英]Spring-Data (JPA) with EJB3(javax) without Springframework JBOSS AS7

以下情况。 我想使用spring-data,因为我喜欢Domain JpaRepository(interface)系统。

但是这里来了,我不想使用弹簧框架本身。 我想坚持使用javax。* ejb3规范

我正在尝试设置项目配置。

我有一个带有spring-data-jpa的项目,该项目代表持久性层。 我使用@Stateless而不是springs @Service和@Inject而不是@autowired(因为我听说过同义词。persistancelayer内的accountservice仅用于测试目的。

PersistanceModule(Eclipseproject)
|
|-com.ns.persistance
|   |-domain
|       |-Account
|   |-repository
|       |-AccountRepository (JpaRepositry interface)
|   |-test
|       |-AccountService (which contains a method do save a user in the database)

帐户存储库

public interface AccountRepository extends JpaRepository<Account, Long> { 

    Account findByUsername(String username);

}

客户服务用于测试目的。

@Stateless
public class AccountService {

    @Inject
    private AccountRepository ar;

    public void createTestAccount() {
        Account account = new Account();
        account.setUsername("testAccount");
        account.setPassword("sicheres passwort");
        account.setEmail("kks.cs@web.de");
        account.setCreationTime(new Date());
        account.setLastModificationDate(new Date());

        account = ar.save(account);

    }
}

工作作业以测试弹簧数据

@Singleton
public class SSService {

    @EJB
    AccountService as;

    @Schedule(minute = "*/5", hour = "*", persistent = false)
    public void doWork() {

        System.out.println("WorkJob!!!!!!!");
        //as.createTestAccount();

    }

}

我有一个spring-data-config.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:jpa="http://www.springframework.org/schema/data/jpa"
   xsi:schemaLocation="
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
      http://www.springframework.org/schema/data/jpa 
      http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

    <jpa:repositories base-package="com.ns.persistence.repository" />

</beans>

JBoss不断告诉我AccountService为NULL(空指针)。 我相信我忘了设置一些配置,但是我无法掌握它,并且使用谷歌搜索不断获取简单的Springframework解决方案。 我的问题:我想念什么? spring数据是否可以与EJB(javax)一起使用,还是我需要使用springframework?

只是为了确保您做对了。 要使用Spring Data JPA,您将需要使用Spring框架。 您不需要一定使用容器(XML配置,容器设置),但是在应用程序类路径上将需要Spring JAR。

从1.1.0.RC1版本(已经发布)开始,Spring Data JPA项目支持在CDI容器中运行,JBoss AS7是其中的一个实例。 因此,使用刚才提到的juste版本的Spring Data JPA可以正常工作。 然后,不需要Spring配置文件,只需CDI指定的空beans.xml

如果使用的是较早版本,则必须编写一些代码以自己实例化存储库实例:

 @Stateless
 class YourService {

   @PersistenceContext
   private EntityManager em;

   private AccountRepository repository

   @PostConstruct
   public void init() {
      RepositoryFactorySupport factory = new JpaRepositoryFactory(em);
      this.repository = factory.getRepository(AccountRepository.class);
   }
}

将代码放入一个公共组件中,然后将该实例注入到服务中,然后避免在每个服务实现中重写此设置代码,这可能是有道理的。 正如我上面提到的那样:从即将发布的1.1.0版本开始,这将立即可用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM