Jon
Jon

Reputation: 277

How to use Spring Roo domain model in a regular web app

I have built a little model using Roo. There are 6 objects in the model that have various relationships - it was neat, it took like 20 minutes! Now that I have it, I want to create Spring MVC project and use the domain model - but I want to keep the web project totally separate from the Roo project (I don't understand Spring MVC to use the scaffolding in Roo yet).

So I've created the dependency in the pom.xml of the web project and I can build and deploy the web project using 'mvn tomcat:run'. However, when the web tries to use the Spring Roo objects I get exceptions like this:

java.lang.IllegalStateException: Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)
at com.loquatic.clcmops.consignortool.entities.Consignor_Roo_Jpa_ActiveRecord.entityManager_aroundBody0(Consignor_Roo_Jpa_ActiveRecord.aj:19)
at com.loquatic.clcmops.consignortool.entities.Consignor_Roo_Jpa_ActiveRecord.ajc$interMethod$com_loquatic_clcmops_consignortool_entities_Consignor_Roo_Jpa_ActiveRecord$com_loquatic_clcmops_consignortool_entities_Consignor$entityManager(Consignor_Roo_Jpa_ActiveRecord.aj:1)
at com.loquatic.clcmops.consignortool.entities.Consignor.entityManager(Consignor.java:1)

I have done this in a standalone project for a project where I had a class that had a main() method and I had to add a line of code to get the context loaded correctly (this basically: How to use JUnit tests with Spring Roo? (Problems with EntityManager) ). This being Spring MVC I sort of assumed it was happening automagically - I am obviously mistaken.

So do I need to do something in the web project to get the context loaded for the Roo jar file? Or do I need to change something in the roo jar file project? Do I need annotate a controller with something to make this happy?

I've tried adding the following to the pom.xml in the web project:

       <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>   

and

   <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${org.aspectj-version}</version>
    </dependency>   
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>${org.aspectj-version}</version>
    </dependency>   

But that hasn't changed anything - and this is some serious guess work, too.

Anyone have any pointers? How are you supposed to use a Spring domain model in another project that isn't necessarily a Roo project?

Thanks!

Upvotes: 0

Views: 798

Answers (1)

CodeChimp
CodeChimp

Reputation: 8154

First of all, I don't see any logical reason to separate the domain code from the controller code. Keep in mind, I say this from the perspective of someone that has worked on a HUGE application comprising tens-of-thousands of classes, all sorted into about 15 different sub-projects in Eclipse. This is all build using Spring, mostly, but no Roo.

I think what you need to do is this. First off, you need to create a master Maven project. Not a JAR or WAR project, just a master "container". I forget the exact maven command, but there is one that creates an empty parent POM and all.

Once you have the "master" setup, you create two sub-POMs, one for the domain stuff, one for the WAR stuff. The master will dictate the order in which you need to compile things. Typically its low-level libs first, higher-level projects next, and ultimately some WAR project. Your WAR projects POM should be set to pull in the higher-level projects, and the higher-level project POMs should pull in your low-level libraries.

What we use as our basic hierarchy is this:

'master'
  - components
      - 'lib 1'
      - 'lib 2'
  - apps
      - 'app 1'
          - 'app 1'_jar
          - 'app 1'_war
      - 'app 2'
          - 'app 2'_jar
          - 'app 2'_war

This is the basic layout for out project at work, with the items in the '' to be substituted for whatever names you are using. Don't ask me why we laid things out this way, it was discussions made many years ago that may or may not be relavant any longer. Bottom line: Its excessive, but it works.

Once you get your project hierarchy setup, then you need to remove all the projects from Eclipse. Just delete them...they are setup with Roo and such in mind, and it wont play will doing non-Roo stuff with Roo stuff. Then run your:

mvn eclipse:clean eclipse:eclipse

Usually you can do this from the top-level project, but sometimes you have to do each sub-project separately.

I would also recommend you looking at how Spring Roo handles Spring MVC. It's really a nice implementation, uses Aspects quite well, and is SUPER easy to learn and use.

Upvotes: 0

Related Questions