简体   繁体   English

在Non-Maven-Non-Spring项目中包含Maven-Spring-Roo-Hibernate jar的正确程序是什么?

[英]What is the correct procedure for including a Maven-Spring-Roo-Hibernate jar in a Non-Maven-Non-Spring project?

I'm using Spring Roo to generate a bunch of Hibernate objects, inside my unit tests in the same project I can successfully read-write to the database if I do: 我正在使用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());
}
....

Now if I do a mvn clean install package and include the jar in an external project and do the same code: 现在,如果我执行mvn clean安装软件包并将jar包含在外部项目中,并执行相同的代码:

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

I get "Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)" 我得到“尚未注入实体管理器(Spring Aspects JAR是否配置为AJC / AJDT Aspects库?)”

I've tried creating a class inside the Spring-Roo-Hibernate project like this and adding ContextConfiguration on top of that: 我试过像这样在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;

    }

}

Now when I call doSomething() in an external project: 现在,当我在外部项目中调用doSomething()时:

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

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

... I get the same error: "Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)" ...我收到相同的错误:“尚未注入实体管理器(Spring Aspects JAR是否配置为AJC / AJDT Aspects库?)”

Looking at the generated AspectJ code: 查看生成的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;
    }

... I can see that @PersistenceContext is suppose to initialize MyUser.entityManager() which it is not when the project is jarred and included in an external project. ...我可以看到@PersistenceContext是用来初始化MyUser.entityManager()的,当项目被震撼并包含在外部项目中时,它不是。 How would one go about manually initializing the entityManager ? 如何手动初始化entityManager? Or is there another way to initialize the context in the spring project when it's using it as an included library which will initialize the entityManager? 还是在将Spring项目用作包含的库时会初始化springManager中的上下文的另一种方法来初始化EntityManager?

Because the application context don't aware about the model. 因为应用程序上下文不了解模型。 You are creating the model out of the appcontext. 您是通过appcontext创建模型的。 if you get the Model using getBean("MyUser"); 如果使用getBean(“ MyUser”)获得模型; some thing like that will work. 这样的事情会起作用。 else auto-wire the model and use that model for your crud operations. 否则,将自动连接模型并将该模型用于原始操作。

You can see the following code in MyUser - @PersistenceContext rotected transient EntityManager entityManager; 您可以在MyUser中看到以下代码-@PersistenceContext旋转的瞬态EntityManager实体管理器; Then only the entityManager object will initialized inside the the Myuser. 然后,只有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());
       }
    }

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

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