Using container urls scanning in Tomee 7.0.0-M1

It's nice to have CDI scanning in global level aka container level. 

Here is a great new feature coming with Tomee 7.0.0-M1 release. Global scan property needs to be set in <tomee home>/conf/system.properties

 openejb.scan.webapp.container=true  

Here is my testing case:

Environment: mac os, jdk 1.8, tomee 7.0.0-M1

  • build a jar (maven) project : global-lib.jar
  • define bean Service in this jar project
 public interface Service {  
      public int doWork(int a, int b);  
 }  
 public class ServiceImpl implements Service {  
      @Override  
      public int doWork(int a, int b) {  
           return a + b;  
      }  
 }  

  • build a war (maven) project: global-web.war,
  • add dependency of above jar project as provided
  • add testServlet to try @inject from container level
 @WebServlet(name = "testServlet", urlPatterns = {"/testServlet"})  
 public class TestServlet extends HttpServlet {  
      private static final long serialVersionUID = 1L;  
      @Inject  
      private Service service;  
      protected void doGet(HttpServletRequest request,  
                HttpServletResponse response) throws ServletException, IOException {  
           int a = 2;  
           int b = 3;  
           PrintWriter out = response.getWriter();  
           out.println("Hello World: " + service.doWork(a, b));  
           out.close();  
      }  
 }  

  • copy global-lib.jar to <tomee home>/lib
  • start tomee by <tomee home>/bin/start.sh
  • you will see following on screen.
   
     

Note: Currently, javax.inject.Singleton doesn't work per the following exception in log. Instead, using javax.ejb.Singleton;
 Caused by: javax.enterprise.inject.spi.DeploymentException: couldn't start owb context  
 at org.apache.openejb.cdi.ThreadSingletonServiceImpl.initialize(ThreadSingletonServiceImpl.java:182)  
 at org.apache.openejb.cdi.CdiBuilder.build(CdiBuilder.java:41)  
 at org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:894)  
 at org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:694)  
 at org.apache.tomee.catalina.TomcatWebAppBuilder.startInternal(TomcatWebAppBuilder.java:1214)  
 ... 16 more  
 Caused by: java.lang.IllegalStateException: Only normal scoped beans can use @Startup - likely @ApplicationScoped  
 at org.apache.openejb.cdi.OpenEJBLifecycle.starts(OpenEJBLifecycle.java:272)  
 at org.apache.openejb.cdi.OpenEJBLifecycle.startApplication(OpenEJBLifecycle.java:237)  
 at org.apache.openejb.cdi.ThreadSingletonServiceImpl.initialize(ThreadSingletonServiceImpl.java:180)  
 ... 20 more  











No comments :

Post a Comment