I have two gwt RemoteServiceServlet in my project which has use GWT framework as below
#RemoteServiceRelativePath("AuthService")
public interface AuthService extends RemoteService {
}
public class AuthServiceImpl extends RemoteServiceServlet implements AuthService
{
#Autowired
private StoredService storeService;
}
#RemoteServiceRelativePath("webService")
public interface WebService extends RemoteService
{
}
public class WebServiceImpl extends RemoteServiceServlet implements WebService
{
#Autowired
private LogService logService;
}
Both the #Autowired of StoreService and LogService are work. But when I try to autowired WebService in AuthService, it will be null at runtime.
The interface and implements are in different package, which is com.test.client.service and com.test.server.service respectively.
I have confirm my SpringApplication-context.xml has below declare:
<context:component-scan
base-package="com.test.client.service, com.test.server.service
>
Also try to add #Service, #Component or #Configurable on WebServiceImpl, but still not works.
Override the init() of WebServiceImpl is also not work either.
#Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
WebApplicationContextUtils.getWebApplicationContext(config.getServletContext())
.getAutowireCapableBeanFactory().autowireBean(this);
}
Is that because the RemoteService cause the autowired not work correctly?
Any help is appreciated.
Thanks
I use the following to integrate GWT-RPC into Spring. I wrote it 6 years ago and never saw the need to change it. It is old-school and it works. It models GWT-RPC services as Spring MVC controllers, which they are. You can autowire anything into those controllers.
I mentioned it in a different answer, so I'll just post a link: https://stackoverflow.com/a/30838630/4982193
Related
I need to have an interface and then two implementations in different maven modules. Both impl services see api modul with interface but they don't see each other.
There is a default implementation and then transactional implementation. I want transactional impl service just load and call default impl service. Like this:
package my.app.core.api;
public interface MyService {
boolean process();
}
package my.app.core.impl
#Service
public class MyServiceImpl implements MyService {
#Override
public boolean process() {
// do something cool...
}
}
package my.app.somewhere.else.impl
#Service
#Transactional
public class TransactionalMyServiceImpl implements MyService {
#Autowire
private MyService myService;
#Override
public boolean process() {
myService.process();
}
}
Is it possible or do I need to #Autowire explicitly MyServiceImpl instead of interface? Which means to add maven dependancy to my.app.somewhere.else.impl.pom.
You can give different names to your services like so:
#Service
#Qualifier("transactionalMyService")
And then when you autowire you can use the name:
#Autowired
#Qualifier("transactionalMyService")
private MyService myService;
I already looked at a lot of posts and nothing seems to work quite as i liked it to.
I want to inject an object into ContainerRequestContext properties from a filter and retrieve it later in other classes.
here is my filter:
#Priority(Priorities.AUTHENTICATION)
public class AuthorizationFilter implements ContainerRequestFilter {
#Override
public void filter(ContainerRequestContext containerRequestContext) throws IOException {
containerRequestContext.setProperty("myObject", new Object());
}
}
here is the class I want access to ContainerRequestContext:
#Provider
public class SessionContextProvider implements ISessionContextProvider {
#Context
private ContainerRequestContext request;
#Override
public Object getSessionContext() {
return request.getProperty("mySessionContext");
}
}
and my spring config:
#Bean(name="sessionContextProvider")
#Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public ISessionContextProvider sessionContextProvider() {
return new SessionContextProvider();
}
Everything works as expected if I inject ContainerRequestContext into my web resource. However if call my provider class ContainerRequestContext is always null.
I don't seem why this would not work.
Reagrds
Jonas
The problem is that with the Jersey/Spring integration, it allows us to successfully inject Spring beans into Jersey components, but this is not always true the other way around.
Jersey has it's own DI framework, HK21, and it is responsible for handle the injections with Jersey components. With the Jersey Spring integration, Jersey will lookup the Spring Bean, and take it as is, it won't inject it with any dependencies, I guess assuming Spring should take care of it's own injections.
That being said, if you don't require the ISessionContextProvider to be a Spring bean, then you can just make it an HK2 service. It's pretty simple. If you don't require any special initialization, you can just let HK2 create it. Here a simple configuration
public JerseyConfig extends ResourceConfig {
public JerseyConfig() {
register(new AbstractBinder() {
bind(SessionContextProvider.class)
.to(ISessionContextProvider.class)
.in(RequestScoped.class);
});
}
}
And that's it. You have an injectable ISessionContextProvider2.
If you require the ISessionContextProvider provider to be a Spring bean, then another option is to grab the bean from the Spring ApplicatinContext, and explicitly inject it yourself, using HK2's analogue of the ApplicationContext, its ServiceLocator. To do that we would need to use a Factory to do all the work transparently, so you can still inject the bean without doing any extra work on the outside
import javax.inject.Inject;
import org.glassfish.hk2.api.Factory;
import org.glassfish.hk2.api.ServiceLocator;
import org.springframework.context.ApplicationContext;
public class SessionContextProviderFactory
implements Factory<SessionContextProvider> {
private final ISessionContextProvider provider;
#Inject
public SessionContextProviderFactory(ApplicationContext ctx,
ServiceLocator locator) {
provider = ctx.getBean(ISessionContextProvider.class);
locator.inject(provider);
}
#Override
public ISessionContextProvider provide() {
return provider;
}
#Override
public void dispost(ISessionContextProvider provider) { /* noop */ }
}
Then just register the factory
public JerseyConfig extends ResourceConfig {
public JerseyConfig() {
register(new AbstractBinder() {
bindFactory(SessionContextProviderFactory.class)
.to(ISessionContextProvider.class)
.in(RequestScoped.class);
});
}
}
1 - hk2
2 - See also Dependency injection with Jersey 2.0
I found a workaround. I could inject the Spring HttpServletRequest into my AuthorizationFilter and set the SessionContext on this one.
#Autowired
private HttpServletRequest request;
....
request.setAttribute("mySessionContext", sessionContext);
And then because HttpServletRequest is known to spring and besically represents the same thing in my SessionContextProvider I do the same:
#Autowired
private HttpServletRequest request;
#Override
public SessionContext getSessionContext() {
return (SessionContext) request.getAttribute("mySessionContext");
}
I dont think this is the best solution possible. But it works. Ill wait for peeskillet for any other input if there is a better solution.
Regards
Jonas
I have a .ear that imports a .jar and a .war modules. I'm trying to inject a EJB on my war that exists in jar, and I'm getting a null pointer exception when I call it. Here's the code.
WAR PACKAGE
Controller
This property "private AutenticacaoBean authBean;" is getting a null pointer exception. I don't really know if I need to use #remote in any place, because I'm using different packages, but in the same ear.
public class LoginController extends AbstractController {
#EJB
private AutenticacaoBean authBean;
public void execute() {
//code
}
}
JAR PACKAGE
Bean
#Stateless
#LocalBean
public class AutenticacaoBean {
#EJB
private UsuarioDAO usuarioDao;
//code
}
Implementation
#Stateless
public class UsuarioDAOImpl implements UsuarioDAO {
public UsuarioDAOImpl() {
//code
}
//code
}
Interface
#Local
public interface UsuarioDAO {
//code
}
Any help would be great. Thank you for your time!
EJB 3.1 dropped the requirement for local interfaces and you are using AutenticacaoBean without the interface, but probably your Application Server is not compliant about EJB 3.1 and you cannot perform the injection in this way:
#EJB
private AutenticacaoBean authBean;
You could try to create an interface (AutenticacaoLocal for example) for your AutenticacaoBean with the needed methods and then inject this one in your LoginController:
#EJB
private AutenticacaoLocal authBean;
Exactly as you did for UsuarioDAOImpl.
I'm developing a web app using the Spring 3 framework.
So, I have the next hierachy level in my controllers package:
#Controller
public class BaseController {
#Autowired
#Qualifier("problemService")
protected ProblemService problemservice;
#Autowired
#Qualifier("codeService")
protected CodeService codeservice;
#Autowired
#Qualifier("userService")
protected UserService userservice;
#Autowired
#Qualifier("sessionRegistry")
protected SessionRegistry session;
.....
}
with children
#Controller
public class CodeController extends BaseController{
...
}
#Controller
public class ProblemController extends BaseController{
...
}
What i want is all these service of BaseController available to the children, but more like static resources, that is, all children share an unique instance of the father's services, and not a new instance of each as inheritance does.
In that way, I tried this for each resource in BaseController:
#Autowired
#Qualifier("problemService")
protected static ProblemService problemservice;
but Spring throws a NullPointerException.
So, is my idea at least correct? I mean, i do want to do what i said, but i dont know if it is a good approach at least.
I am newbie with EJB and Injections...
I am currently using Vaadin framework with CDI
I have been trying to using injection but i have not could do it...
In my Vaadin UI class MyVaadinUI i have tried...
CDIUI("")
#SuppressWarnings("serial")
public class MyVaadinUI extends UI {
#EJB
UserController userController;
#Override
protected void init(VaadinRequest request) {
System.err.println("desde controller "+userController.getAll().size());
}
}
UserController
#Stateless
public class UserController {
#EJB
IUserDAO userDao;
public List<User> getAll() {
return userDao.findAll();
}
}
and it works!!
but when I do not inject UserController, it does not work... In other words when I instance the class UserController the injection in this class does not work...
Code does not work
CDIUI("")
#SuppressWarnings("serial")
public class MyVaadinUI extends UI {
#Override
protected void init(VaadinRequest request) {
UserController userController = new UserController();
System.err.println("desde controller "+userController.getAll().size());
}
}
Somebody can explain me why?
Thanks
Nicolas
Only in injected objects will have its dependencies injected. If you create an object with new all field having #inject, #ejb or #resource will not be injected.
In your case you create UserController like this:
UserController userController = new UserController();
and so this field will not be injected:
#EJB
IUserDAO userDao;
And therefore getAll() will throw a NullPointerException.
I use vaadin and cdi for projects. I'd recommend to use injection for almost everything or not at all. I inject my uis, views, own components... (and do not create them with new) so it is possible to inject ejb beans or other things into them. If you are using it only sometimes you are ending up with am mixture of injection and normal object creation and will have to pass around injected objects to other object you instantiated yourself. In another project of mine this happened and got really problematic for future changed in the code.