[jboss-user] [JBoss/Spring Integration] - Re: How to inject EJB into POJO

alen_ribic do-not-reply at jboss.com
Tue Jul 7 07:32:14 EDT 2009


"marius.bogoevici" wrote : Alen,
  | 
  | You can try using one of the standard Spring mechanisms:
  | 
  | - org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean or the jee namespace
  | - the @EJB annotation
  | 
  | There is a sample showing the usage of the EJB annotation in https://anonsvn.jboss.org/repos/jbossas/projects/spring-int/trunk/documentation/samples/horoscope/horoscope-war/src/main/java/org/jboss/tutorial/web/AppController.java - the root of the sample is
  | 
  | https://anonsvn.jboss.org/repos/jbossas/projects/spring-int/trunk/documentation/samples/horoscope/
  | 
  | The difference between this and your scenario is that yours is injecting EJBs in a Spring-Deployer bootstrapped application context, whereas the sample is injecting them in an ApplicationContext bootstrapped by the ContextLoaderListener, but this should work pretty much the same.
  | 
  | Give it a try and let me know if you encounter any issues.
  | 
  | Marius

Thx for your reply.

I tried adding the @EJB to my POJO and still got a NPE.

I will try explain the exact scenario on my end:

jboss-spring.xml


  | <?xml version="1.0" encoding="UTF-8"?>
  | <beans xmlns="http://www.springframework.org/schema/beans"
  |        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  |        xmlns:context="http://www.springframework.org/schema/context"
  |        xmlns:jee="http://www.springframework.org/schema/jee"
  |        xmlns:util="http://www.springframework.org/schema/util"
  |        xmlns:tx="http://www.springframework.org/schema/tx"
  |        xmlns:aop="http://www.springframework.org/schema/aop"
  |        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  |        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
  |        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
  |        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
  |        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
  |        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
  |     <context:annotation-config/>
  |     
  |     <jee:jndi-lookup id="userServiceLookup" jndi-name="InspecApplication/UserServiceBean/local"/>
  |     <jee:jndi-lookup id="projectServiceLookup" jndi-name="InspecApplication/ProjectServiceBean/local"/>
  |     <jee:jndi-lookup id="generalServiceLookup" jndi-name="InspecApplication/GeneralServiceBean/local"/>
  |     <jee:jndi-lookup id="milestoneServiceLookup" jndi-name="InspecApplication/MilestoneServiceBean/local"/>
  |     <jee:jndi-lookup id="taskServiceLookup" jndi-name="InspecApplication/TaskServiceBean/local"/>
  |     <jee:jndi-lookup id="eventServiceLookup" jndi-name="InspecApplication/EventServiceBean/local"/>
  |     
  |     <bean id="backendManager" name="/inspecbackend/BackendManager"
  |         class="com.eluminary.inspec.backend.BackendManager">
  |         <property name="userService" ref="userServiceLookup" />
  |         <property name="projectService" ref="projectServiceLookup" />
  |         <property name="generalService" ref="generalServiceLookup" />
  |         <property name="milestoneService" ref="milestoneServiceLookup" />
  |         <property name="taskService" ref="taskServiceLookup" />
  |         <property name="eventService" ref="eventServiceLookup" />
  |     </bean>
  | 
  |     <bean id="commandTaskStart" class="com.eluminary.inspec.backend.command.task.CommandTaskStart">
  |         <property name="backendManager" ref="backendManager" />
  |     </bean>
  |     ...
  | </beans>
  | 

JEE Interceptor that intercepts methods on my Session Bean.
StartTaskEvent.java


  | public class StartTaskEvent extends BaseEventInterceptor {
  |     private Log log = LogFactory.getLog(getClass());
  | 
  |     @EJB private TaskService taskService;
  | 
  |     @AroundInvoke
  |     public Object handleEvent(InvocationContext ctx) throws Exception {
  |         ActionEventHandler aeh = new TaskActionEventHandlerImpl();
  |         Long taskId = (Long)ctx.proceed();
  |         TaskEvent te = new TaskEvent();
  |         te.setTask(taskService.getTask(taskId));
  |         te.setType(TaskEvent.TaskEventType.START);
  |         aeh.actionPerformed(te);
  |         return taskId;
  |     }
  | }
  | 


TaskActionEventHandlerImpl.java (POJO)


  | public class TaskActionEventHandlerImpl implements TaskActionEventHandler {
  | 
  |    ...
  | 
  |     public void onStart() {
  |         Command cmd = new CommandTaskStart(taskEvent.getTask());
  |         cmd.execute();
  |     }
  | 
  |    ...
  | 
  | }
  | 

CommandTaskStart.java (POJO):


  | public class CommandTaskStart implements Command {
  |     private Task task;
  |     private BackendManager backendManager;
  | 
  |     public BackendManager getBackendManager() {
  |         return backendManager;
  |     }
  | 
  |     public void setBackendManager(BackendManager backendManager) {
  |         this.backendManager = backendManager;
  |     }
  |     
  |     public CommandTaskStart(Task task) {
  |         this.task = task;
  |     }
  | 
  |     public void execute() {
  |         task.setStatus(TaskStatus.GREEN);
  |         getBackendManager().getTaskService().updateTask(task);
  |     }
  | 

I basically want to springify the above code as much as possible.
For instance, the Interceptor StartTaskEvent exposes the implementation of the ActionEventHandler called TaskActionEventHandlerImpl().

So first problem here is how do I inject the TaskActionEventHandlerImpl into the StartTaskEvent Interceptor? @Spring I though might not work since its not an EJB. And it didn't work.

Thanks,
-Al

View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4242158#4242158

Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4242158



More information about the jboss-user mailing list