2012-04-30 23 views
0

以下情況。我想使用spring-data,因爲我喜歡Domain JpaRepository(interface)系統。彈簧數據(JPA)與EJB3(javax)無Springframework JBOSS AS7

這裏是但是,我不想使用Spring框架本身。我想堅持javax。* ejb3規範

我正在嘗試設置項目配置。

我有一個項目用spring-data-jpa代表pesristance層。我使用@Stateless代替彈簧@Service和@Inject替代@Autowired(因爲我聽說有同義詞。在persistancelayer內的AccountService只存在測試purporses。

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

AccountRepository

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("[email protected]"); 
     account.setCreationTime(new Date()); 
     account.setLastModificationDate(new Date()); 

     account = ar.save(account); 

    } 
} 

Workjob測試彈簧數據

@Singleton 
public class SSService { 

    @EJB 
    AccountService as; 

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

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

    } 

} 

我有一個彈簧數據-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。

回答

0

只是爲了確保你得到這個權利。要使用Spring Data JPA,你需要使用Spring框架。您不需要使用容器(XML配置,容器設置),但您需要在應用程序類路徑上使用Spring JAR。

從版本1.1.0.RC1(已發佈)開始,Spring Data JPA項目支持在CDI容器內運行,而JBoss AS7是其中的一個實例。因此,Spring Data 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版本將會開箱即用。