【问题标题】:What is the correct procedure for including a Maven-Spring-Roo-Hibernate jar in a Non-Maven-Non-Spring project?在 Non-Maven-Non-Spring 项目中包含 Maven-Spring-Roo-Hibernate jar 的正确程序是什么?
【发布时间】:2013-08-29 13:39:17
【问题描述】:

我正在使用 Spring Roo 生成一堆 Hibernate 对象,在同一个项目的单元测试中,如果我这样做,我可以成功地读写数据库:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { "classpath*:/META-INF/spring/applicationContext.xml"})
public class SomeTest extends AbstractJUnit4SpringContextTests {

@Test
public void someTest() throws Exception {
    MyUser myUser = MyUsers.findByUserId(123);
    System.out.println(myUser.getFirstName());
}
....

现在,如果我执行 mvn clean install package 并将 jar 包含在外部项目中并执行相同的代码:

MyUser myUser = MyUsers.findByUserId(123);
System.out.println(myUser.getFirstName());

我收到“尚未注入实体管理器(Spring Aspects JAR 是否配置为 AJC/AJDT 方面库?)”

我尝试在 Spring-Roo-Hibernate 项目中像这样创建一个类,并在其上添加 ContextConfiguration:

@Service
@ContextConfiguration(locations = { "classpath*:/META-INF/spring/applicationContext.xml"})
public class SomeClassImpl {

    public MyUser doSomething(){

        MyUser myUser = MyUsers.findByUserId(123);
                return myUser;

    }

}

现在当我在外部项目中调用 doSomething() 时:

public class TestDatabase {
    public static void main(String[] args){

        SomeClassImpl k = new SomeClassImpl();
        k.doSomething();
    }   
}

...我得到同样的错误: “实体管理器尚未注入(Spring Aspects JAR 是否配置为 AJC/AJDT 方面库?)”

查看生成的 AspectJ 代码:

privileged aspect MyUser_Roo_Jpa_ActiveRecord {

    @PersistenceContext
    transient EntityManager MyUser.entityManager;

    public static final EntityManager MyUser.entityManager() {
        EntityManager em = new MyUser().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

...我可以看到 @PersistenceContext 应该初始化 MyUser.entityManager() 当项目被 jarred 并包含在外部项目中时它不是。如何手动初始化 entityManager ?或者当它使用它作为将初始化 entityManager 的包含库时,是否有另一种方法来初始化 spring 项目中的上下文?

【问题讨论】:

    标签: java spring hibernate maven spring-roo


    【解决方案1】:

    因为应用程序上下文不知道模型。您正在从 appcontext 中创建模型。如果您使用 getBean("MyUser"); 获取模型类似的事情会起作用。否则自动连接模型并将该模型用于您的 crud 操作。

    在MyUser中可以看到如下代码——@PersistenceContext rotected transient EntityManager entityManager;然后只有 entityManager 对象会在 Myuser 中初始化。

       @RunWith(SpringJUnit4ClassRunner.class) 
       @ContextConfiguration(locations = { "classpath*:/META-INF/spring/applicationContext.xml"})
       public class SomeTest extends AbstractJUnit4SpringContextTests {
    
           @Autowired
           MyUser myUser; // use this object for ur crud operations 
    
           @Test
           public void someTest() throws Exception {                
               MyUser otherObject= myUser.findByUserId(123);
               System.out.println(otherObject.getFirstName());
           }
        }
    

    【讨论】:

    • 我认为你误解了这个问题,ProjectA 是一个 Maven-SpringRoo-Hibernate 项目,单元测试在 ProjectA 中运行良好,ProjectB 是一个非 Maven、非 Spring、非休眠项目. ProjectA 被编译成 JAR 文件并作为依赖项包含在 ProjectB 中,然后 projectB 正在使用这些 Hibernate 对象,但是由于没有通过 projectB 在 projectA 中初始化上下文,所以它失败了。我想知道的是如何在 projectB 中包含 projectA.jar 并且仍然能够在 projectB 中使用来自 projectA 的 Hibernate 对象。
    猜你喜欢
    • 1970-01-01
    • 2013-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多