Using the idea that we found in:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/ejb.html#ejb-implementation-ejb3
we wanted to use interceptor in order to access spring boot beans from an EJBs. But the problem is, the example of the documentation uses a new context.
How can the EJBs access the spring boot context?
We tried this:
public class MySpringActuatorMetricsCoreTestInterceptor extends SpringBeanAutowiringInterceptor {
//Spring boot application context
@Autowired
ApplicationContext applicationContext;
@SuppressWarnings("resource")
@Override
protected BeanFactory getBeanFactory(Object target) {
return applicationContext.getAutowireCapableBeanFactory();
}
}
And the EBJ looks like this:
// ejb
@Stateless
// spring
@Interceptors(MySpringActuatorMetricsCoreTestInterceptor.class)
public class FirstBean {
[...]
The problem is: application context is not already initialized because EJBs initialization happens before and as a consequence -> null pointer exception.
We think there are two options: - We get the application context somehow from the spring boot. - We can give the context that we could create by MySpringActuatorMetricsCoreTestInterceptor to the spring boot context.
Is there any solution? another option?
We are using Glassfish 3.1
Thanks!
Ok I found a way as it seems:
I just added a beanRefContext.xml to my classpath:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans_2_0.dtd">
<beans>
<bean class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg value="classpath*:simpleContext.xml" />
</bean>
</beans>
Which references a new applicationContext file named simpleContext.xml also in my classpath:
...
<!-- Enable annotation support within our beans -->
<context:annotation-config/>
<context:spring-configured/>
<context:component-scan base-package="your.package.path" />
<context:property-placeholder location="classpath*:*.properties" />
...
Now I was able to inject spring boot services into my EJB:
@Stateless(name = "RightsServiceEJB")
@Remote
@Interceptors(SpringBeanAutowiringInterceptor.class)
public class RightsServiceEJB implements IRightsServiceEJB {
@Autowired
ExampleService exampleService;
@Override
public String sayHello() {
return exampleService.sayHello();
}
}
This however is for now a small hello world Example, I am not sure if the Spring service still can reference resources initialized by Spring boot. This will need further testing on my side.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With