Announcement Announcement Module
Collapse
No announcement yet.
Entity manager has not been injected Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Entity manager has not been injected

    Hi,
    I'm trying to implement a simple application. I have a simple class (I took it from http://blog.springsource.com/2009/05/27/roo-part-2/)

    Code:
    @Entity
    @RooJavaBean
    @RooToString
    @RooEntity
    public class Rsvp {
    
        @NotNull
        @Size(min = 1, max = 30)
        private String code;
    
        @Size(max = 30)
        private String email;
     }
    and then implemented some other class that uses it

    Code:
    public class MyApplication {
    
    	public static void main(String params[]) {
    		MyApplication myApp = new MyApplication();
    		myApp.startMyApp();
    	}    
    
    	public void startMyApp() {
    		Rsvp rsvp = new Rsvp();
    		rsvp.setEmail("Email");
    		System.out.println("Test Rsvp email: " + rsvp.getEmail());
    		rsvp.persist();
    	}
    }
    When I run it the line "rsvp.persist();" throws an exception "Exception in thread "main" java.lang.IllegalStateException: Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)" In a debug mode I see that entityManager is not injected.

    I use SpringSource Tool Suite 2.1.0.SR01 with Eclipse AspectJ Development Tools 2.0.1.e35x-20091001-1600 and included the following libraries in my project

    aspectjweaver-1.6.3.jar
    org.springframework.beans-3.0.0.RC1.jar
    com.springsource.org.aspectj.tools-1.6.5.RELEASE.jar
    org.springframework.jdbc-3.0.0.RC1.jar
    com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar
    com.springsource.javax.transaction-1.1.0.jar
    com.springsource.javax.validation-1.0.0.CR3.jar
    com.springsource.org.hibernate.validator-4.0.0.Beta2.jar
    com.springsource.slf4j.api-1.5.6.jar
    com.springsource.javax.persistence-1.0.0.jar
    com.springsource.slf4j.log4j-1.5.6.jar
    com.springsource.edu.emory.mathcs.backport-3.1.0.jar
    ehcache-1.3.0.jar
    com.springsource.org.hibernate.ejb-3.3.2.GA.jar
    hibernate-annotations-3.3.1.ga.jar
    com.springsource.com.mysql.jdbc-5.1.6.jar
    org.springframework.transaction-3.0.0.RC1.jar
    org.springframework.aop-3.0.0.RC1.jar
    org.springframework.context-3.0.0.RC1.jar
    org.springframework.roo.annotations-1.0.0.RC2.jar
    com.springsource.org.aspectj.runtime-1.6.5.RELEASE.jar
    com.springsource.org.hibernate-3.2.6.ga.jar
    org.springframework.orm-3.0.0.RC1.jar
    spring-jpa-2.0.6.jar
    aspectjrt-1.6.5.jar
    hibernate-entitymanager.jar
    dbpopulator-0.1.0-SNAPSHOT.jar
    org.springframework.core-3.0.0.RC1.jar
    org.springframework.aspects-3.0.0.RC1.jar
    com.springsource.org.apache.commons.logging-1.1.1.jar
    org.springframework.beans-3.0.0.RC1.jar
    org.springframework.core-3.0.0.RC1.jar
    org.springframework.aspects-3.0.0.RC1.jar
    aspectjweaver-1.6.3.jar
    org.springframework.transaction-3.0.0.RC1.jar
    com.springsource.org.apache.commons.logging-1.1.1.jar
    commons-lang-2.4.jar

    I read that the reason could be the absence of an aspects library. I also tried to add org.springframework.aspects-3.0.0.RC1.jar to the "AspectJ Build->Aspect path". Nothing helps

  • #2
    Roo uses Spring Framework's @Configurable support. For @Configurable to work, the Spring Framework needs to have been initialized. So try adding a new ClasspathXmlApplicationContext("classpath*:META-INF/spring/applicationContext*.xml) to the start of your main method.

    A good test is to run the generated integration tests. These prove that AJDT/AspectJ is working OK.

    I'm actually surprised you're using a main method rather than running up a webapp. Is that for experimentation, or were you building a console or standalone-style app?

    Comment


    • #3
      Hi, Ben,
      Many thanks for answering. You're 100% right! I did forget to initialize a context. It was so stupid to forget that! I ran integration tests and they were ok. Now it's clear why and what happened.
      I used a main method just to experiment with Roo. However, I'm going to use Roo for my Swing application.

      Comment


      • #4
        Using Roo for Swing. Interesting. If you get a chance to post a blog or article on that, please point me to it as I am sure others would be interested in reading more....

        Comment


        • #5
          > Using Roo for Swing. Interesting.

          Indeed. How hard would it be to replace the web layer with a desktop app?

          Further, how hard would it be for Roo to generate RESTful services and a Swing-based JNLP application to invoke them? Doable I guess.

          Btw. how does the Swing threading model (Swing/AWT components should only be created/manipulated from within the EDT) work with the Spring DI container?

          --
          Jukka

          Comment


          • #6
            Most people startup the Spring DI container from within their main method before starting to paint any widgets. You might like to look at the Spring Rich Client Project for ideas, as they specialize in Swing in that project.

            Roo already creates REST endpoints so it wouldn't be hard to access them from a Swing client, especially when combined with Spring 3's client-side RestTemplate. You might also want to consider HttpInvoker if you just want plain old remoting.

            Comment


            • #7
              facing problem is the Spring Aspects JAR configured as an AJC/AJDT aspects library

              hi
              i am working on a web application .However i have folowed all the steps .But still i am facing the same issue.
              "Failed to invoke handler method [public java.lang.String com.neev.itrack.web.AgentController.createForm(org .springframework.ui.ModelMap)]; nested exception is java.lang.IllegalStateException: Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)"

              StackTrace
              "org.springframework.web.bind.annotation.support.H andlerMethodInvoker.invokeHandlerMethod(HandlerMet hodInvoker.java:171)
              org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter.invokeHandlerMethod(An notationMethodHandlerAdapter.java:414)
              org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter.handle(AnnotationMetho dHandlerAdapter.java:402)
              org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:771)
              org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:716)
              org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:647)
              org.springframework.web.servlet.FrameworkServlet.d oGet(FrameworkServlet.java:552)
              javax.servlet.http.HttpServlet.service(HttpServlet .java:617)
              javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
              org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:290)
              org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
              org.apache.catalina.core.ApplicationDispatcher.inv oke(ApplicationDispatcher.java:646)
              org.apache.catalina.core.ApplicationDispatcher.pro cessRequest(ApplicationDispatcher.java:436)
              org.apache.catalina.core.ApplicationDispatcher.doF orward(ApplicationDispatcher.java:374)
              org.apache.catalina.core.ApplicationDispatcher.for ward(ApplicationDispatcher.java:302)
              org.tuckey.web.filters.urlrewrite.NormalRewrittenU rl.doRewrite(NormalRewrittenUrl.java:195)
              org.tuckey.web.filters.urlrewrite.RuleChain.handle Rewrite(RuleChain.java:159)
              org.tuckey.web.filters.urlrewrite.RuleChain.doRule s(RuleChain.java:141)
              org.tuckey.web.filters.urlrewrite.UrlRewriter.proc essRequest(UrlRewriter.java:90)
              org.tuckey.web.filters.urlrewrite.UrlRewriteFilter .doFilter(UrlRewriteFilter.java:417)
              org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:235)
              org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
              org.springframework.web.filter.HiddenHttpMethodFil ter.doFilterInternal(HiddenHttpMethodFilter.java:7 1)
              org.springframework.web.filter.OncePerRequestFilte r.doFilter(OncePerRequestFilter.java:76)
              org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:235)
              org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
              org.springframework.web.filter.CharacterEncodingFi lter.doFilterInternal(CharacterEncodingFilter.java :88)
              org.springframework.web.filter.OncePerRequestFilte r.doFilter(OncePerRequestFilter.java:76)
              org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:235)
              org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
              org.springframework.web.filter.ShallowEtagHeaderFi lter.doFilterInternal(ShallowEtagHeaderFilter.java :57)
              org.springframework.web.filter.OncePerRequestFilte r.doFilter(OncePerRequestFilter.java:76)
              org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:235)
              org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
              org.springframework.orm.jpa.support.OpenEntityMana gerInViewFilter.doFilterInternal(OpenEntityManager InViewFilter.java:113)
              org.springframework.web.filter.OncePerRequestFilte r.doFilter(OncePerRequestFilter.java:76)
              org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:235)
              org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
              org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:233)
              org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:191)
              org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:127)
              org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:102)
              org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:109)
              org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:298)
              org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:852)
              org.apache.coyote.http11.Http11Protocol$Http11Conn ectionHandler.process(Http11Protocol.java:588)
              org.apache.tomcat.util.net.JIoEndpoint$Worker.run( JIoEndpoint.java:489)
              java.lang.Thread.run(Thread.java:619)
              "

              Thanks
              Palam Garg

              Comment


              • #8
                Hi,

                I know this is quite an old thread but I've got a similar problem:
                Exception ist the same "Entitymanager has not been injected" - happening also when running the spring roo generated tests.

                Now I've tried to initialize the context in my startup class of wicket. The result is that nothing has changed, the exception is still there. What I'm wondering about ist when I add the EntityManager to my startup class the entitymanager gets injected successfully.
                Code:
                package de.bens.releaseviewer.web;
                
                import org.apache.wicket.protocol.http.WebApplication;
                import org.apache.wicket.spring.injection.annot.SpringComponentInjector;
                import org.springframework.beans.factory.annotation.Autowired;
                import org.springframework.context.ApplicationContext;
                import org.springframework.stereotype.Component;
                
                import de.bens.releaseviewer.web.page.HomePage;
                
                @Component
                public class ReleaseViewerApplication extends WebApplication {
                	
                	@Autowired
                	private ApplicationContext context;
                	
                //	@PersistenceContext						// This is working!
                //	private EntityManager em;
                	
                	public ReleaseViewerApplication() {
                	}
                
                	private static final String DEFAULT_ENCODING = "UTF-8";
                
                	@Override
                	protected void init() {
                		super.init();
                		
                		getComponentInstantiationListeners().add(new SpringComponentInjector(this, context, true));
                
                		getMarkupSettings().setDefaultMarkupEncoding(DEFAULT_ENCODING);
                		getRequestCycleSettings().setResponseRequestEncoding(DEFAULT_ENCODING);
                
                	}
                
                	@Override
                	public Class<HomePage> getHomePage() {
                		return HomePage.class;
                	}
                
                	public static WebApplication get() {
                		return WebApplication.get();
                	}
                
                }
                I already checked that the applicationContext is included in the classpath and whether it's correctly spelled...

                This is my applicationContext:
                Code:
                <?xml version="1.0" encoding="UTF-8" standalone="no"?>
                <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
                
                    <context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>
                
                    <context:spring-configured/>
                
                    <context:component-scan base-package="de.bens.releaseviewer">
                        <context:exclude-filter expression=".*_Roo_.*" type="regex"/>
                        <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
                    </context:component-scan>
                    <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
                        <property name="driverClassName" value="${database.driverClassName}"/>
                        <property name="url" value="${database.url}"/>
                        <property name="username" value="${database.username}"/>
                        <property name="password" value="${database.password}"/>
                        <property name="testOnBorrow" value="true"/>
                        <property name="testOnReturn" value="true"/>
                        <property name="testWhileIdle" value="true"/>
                        <property name="timeBetweenEvictionRunsMillis" value="1800000"/>
                        <property name="numTestsPerEvictionRun" value="3"/>
                        <property name="minEvictableIdleTimeMillis" value="1800000"/>
                    </bean>
                    <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
                        <property name="entityManagerFactory" ref="entityManagerFactory"/>
                    </bean>
                    <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
                    <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
                        <property name="persistenceUnitName" value="persistenceUnit"/>
                        <property name="dataSource" ref="dataSource"/>
                    </bean>
                    
                </beans>
                Does anyone have an idea what do I do wrong?

                Thanks.
                Erik

                Comment


                • #10
                  Originally posted by [email protected] View Post
                  Hi All,
                  did anyone solved this problem?

                  This happens when I execute any database query on @PostConstruct from within integration test.
                  I am having the exact same issue...get this error when trying to persist in @PostConstruct while running integration tests.

                  Comment


                  • #11
                    Hi All,
                    I've faced the same problem again and again, I assume is a kind of bug of STS, It happens to me when I try to launch unit test from IDE, and when the problem arises what I do is a "maven clean install" from outside of the IDE, refresh the project folder (F5) in Eclipse, and relaunch the unit tests... and it works!
                    So don't get crazy searching a miss-configuration in your application context xml file, I'm pretty sure this bug has to do with eclipse maven plugin + spring plugin... who knows..

                    Comment

                    Working...
                    X