I'm trying to use Guice to inject properties of a JSF managed bean. This is all running on Google App Engine (which may or may not be important)
I've followed the instructions here:
http://code.google.com/docreader/#p=google-guice&s=google-guice&t=GoogleAppEngine
One problem is in the first step. I can't subclass the Servlet module and setup my servlet mappings there because Faces is handled by the javax.faces.webapp.FacesServlet which subclasses Servlet, not HttpServlet. So, I tried leaving my servlet configuration in the web.xml file and simply instantiating a new ServletModel() along with my business module when creating the injector in the context listener described in the second step.
Having done all that, along with the web.xml configuration, my managed bean isn't getting any properties injected. The method is as follows
#ManagedBean
#ViewScoped
public class ViewTables implements Serializable
{
private DataService<Table> service;
#Inject
public void setService( DataService<Table> service )
{
this.service = service;
}
public List<Table> getTables()
{
return service.getAll();
}
}
So, I'm wondering if there is a trick to get Guice injecting into a JSF managed bean? I obviously can't use the constructor injection because JSF needs a no-arg constructor to create the bean.
Check the following JSF-Guice integration framework/advice:
http://code.google.com/p/jsf-sugar/
http://notdennisbyrne.blogspot.com/2007/09/integrating-guice-and-jsf.html
http://cagataycivici.wordpress.com/2007/03/26/integrating_guice_and_jsf/
http://snippets.dzone.com/posts/show/7171
You can also create an HTTP servlet that then simple delegates the request on to a FacesServlet (like a wrapper). This should give you the same effect using Guice Servlet.
How about this approach, works well for us:
http://uudashr.blogspot.com/2008/12/guicing-jsf-with-guice.html
being the developer of jsf sugar I really would like to know the problem you had using it. We are already using it in production here so there shouldn't be any "show stoppers", maybe something is just not well documented? Just drop me a mail: murbanek(at)gmx_net (replace the _ with a .) .
check out http://code.google.com/p/guice2jsf/, and website starchu.blogspot.com, it has excellent library that provides Guice and JSF 2.0 integration
As information in this post are getting out of date but the question is still relevant, I'd like to share my findings about this topic. I wrote a little tutorial including a runnable sample project on how to setup a fully guice powered web stack. You can find it here: https://github.com/skuzzle/guice-jsf
Related
My Spring Boot (v2.3.4) based application uses my custom library containing core entities and business logic. To use entities and repositories from this library I had to use #EnableJpaRepositories and #EntityScan annotations with proper packages provided.
I also wanted to initialize database with some required data (let's say the configuration) during application startup. I found that Spring Boot allows to use data.sql or data-${platform}.sql files to achieve that.
Long story short when using #EnableJpaRepositories annotation the data.sql script is not executed.
I did some digging in the code and found that when #EnableJpaRepositories annotation is not used then entityManagerFactory bean is of org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean type. This bean uses org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher bean post processor, which fires org.springframework.boot.autoconfigure.jdbc.DataSourceSchemaCreatedEvent event indicating the schema has been created. Class, which listens for this event is org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker. This listener invokes initSchema() method from org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer class. This method is responsible for whole initialization using data.sql script.
It looks like setting #EnableJpaRepositories annotation creates instance of different class for entityManagerFactory bean, which does not support this simple initialization.
My basic question is then how to make it all work with #EnableJpaRepositories annotation. I can always use Hibernate's import.sql file (which works fine) but I'm also trying to understand what exactly is going on under the hood I how can I control it.
UPDATE 1 28.09.2021
I did further investigation and #EnableJpaRepositories annotation does not change the instance type of entityManagerFactory but it causes silent exception (?) when creating org.springframework.scheduling.annotation.ProxyAsyncConfiguration bean (during creation of org.springframework.context.annotation.internalAsyncAnnotationProcessor bean). It looks like everything is related to #EnableAsync annotation, which I'm also using but didn't know it might be related. But it is - removing it makes the initialization work even with #EnableJpaRepositories.
UPDATE 2 28.09.2021
I've found full explanation for my issue. There are 4 conditions, which must be met to reproduce the issue:
#EnableJpaRepositories annotation in application configuration
#EnableAsync annotation in application configuration
Configuration implements AsyncConfigurer interface
Autowired any JpaRepository repository or any other bean which injects repository
Enabling asynchronous execution and implementing AsyncConfigurer makes the whole configuration to be instantiated before regular beans. Because Spring has to inject repository, it needs to instantiate entityManagerFactory bean too. Spring prints thenINFO level logs like below:
Bean 'entityManagerFactoryBuilder' of type [org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
One of not eligible BeanPostProcessors is DataSourceInitializedPublisher responsible for firing DataSourceSchemaCreatedEvent event. Without that event, data-${platform}.sql script won't be processed at all.
I'm not sure what is the role of #EnableJpaRepositories in that process but without it the problem does not occur.
Example
Minimal code to reproduce the issue (data.sql located in src/main/resources):
#Entity
public FileStore {
...
}
public interface FileStoreRepository extends extends JpaRepository<FileStore, Long> {
}
#Configuration
#EnableAsync
#EnableJpaRepositories
public class Configuration implements AsyncConfigurer {
#Autowired
private FileStoreRepository fileStoreRepository;
...
}
Solutions
There are two solutions I'm aware of:
Move AsyncConfigurer along with its overrided methods and #EnableAsync annotation to separate configuration class
Use #Lazy annotation on autowired bean like below:
#Lazy
#Autowired
private FileStoreRepository fileStoreRepository;
Similar problem was pointed by #Allen D. Ball and can be checked there.
This behavior had changed.
Take a look at the
how-to guide.
Add:
spring.jpa.defer-datasource-initialization: true
I came to know GAE cloud endpoints yesterday. From that time I am trying to generate APIs for my current web application. I am using JPA2.0, I chose one of my entity classes right clicked on it and then "generate Google endpoint class" . So now I have another class for this entity with #API annotations, etc.
But the problem is after deploying the app when I go to : https://developers.google.com/apis-explorer/?base=https://myAppId.appspot.com/_ah/api#p/
the services tab is empty. Same thing when I check it locally(Image below)
You need to Generate Cloud Endpoints Library (in Eclipse, right click on the Project, it's under Google) as well.
I had similar issue and it was caused by missing public attribute in methods.
#Api
public class MyApi {
#ApiMethod
void myMethod() { }
}
caused that I saw no methods. While added
#Api
public class MyApi {
#ApiMethod
public void myMethod() { }
}
methods started to be visible.
1.Login appengine
https://appengine.google.com/
2.Click the [Version] link in a Main category
3.Select your version and [Make Default] button
4.You can access the api explorer
https://myAppId.appspot.com/_ah/api/explorer
Best Regard.
I actually managed to resolve the above issue. So I had a web application existing and I thought I could just add annotations to it and have the APIs represented for it after deployment. But I realized that I had to start from scratch by creating an android app and then generate the back-end for that app and add my classes there. It now works. Thank you.
Points to remember before working on endpoints :
Need to create an endpoint client library before running your project.(In Eclipse : Project -> Right click -> Google -> Generate cloud endpoints library)
Check whether you are using latest Google Plugin or not. Because files required by endpoints will be executed from the plugin. If you are not able to generate the endpoint library. problem is with the plugin .Try updating it.
Endpoints will work only on default versions. Make sure that you made your version default.
finally try loading http://myApp.appspot.com/_ah/api/explorer. Everything should be fine now.
I have a simple GAE application using JPA and eclipse plugin for GAE (sdk version 1.7.2).
I only have one simple entity mapped wit 2 properties : 1 Key key ; and one String name.
I have created only one class (taken from the "Datastore Callbacks" documentation of gae)
public class PostPutCallbacks {
static Logger logger = Logger.getLogger(PostPutCallbacks.class.getName());
#PostPut
public void collectSample(PutContext context) {
logger.fine("Finished putting " + context.getCurrentElement().getKey());
}
}
The database callback is not called.
I tried with several other annotations (#PrePut #PreGet) but they still didn't work .
I've also tried with the Datastore low leve API and the callback is still not called.
I searched the web to see if there is a configuration besides the annotation processing jar manualy added for eclipse but I didn't find anything..
Can anyone give me a hint?
I want to provide more information in this thread. If you are unable to see the datastore callbacks being triggered. Try this procedure as documented. You need to do some simple project set up at Eclipse in order to make it work. Hope this helps.
I jumped a little fast to stackoverflow..I had to restart eclipse and it worked
There are two similar questions asked here and here but no adequate answers are given.
I found that I can use Enunciate to create WADL for a RestEasy service. So I tried it.
In one of my services I have a method mapped to HTTP GET which I am using like below
...
import org.jboss.resteasy.annotations.Form;
...
#GET
#Produces({MediaType.APPLICATION_JSON})
#Transactional(readOnly = true)
public WebServicePageResponse<D> find(#Form WebServicePageRequest<E> wsPageRequest)
{
...
}
Enunciate performs a validation on the service methods before it generates the WADL, and throws this error and fails
"A resource method that is mapped to HTTP GET must not specify an entity parameter."
#Form is a RestEasy specific annotation, while Enunciate can only parse JSR-311 annotations.
Has anyone done something similar? Has anyone successfully used Enunciate to generate documentation for a RestEasy service? Are there any alternatives?
Looks like a great suggestion for a new feature. Tracking it here.
It might be an awkward workaround, but have you tried using the signature override?
The best solution I found to this was to remove #Form annotation and use the individual annotations instead (enter link description hereatleast till Enunciate start supporting this).
What I'm looking for is some sort of class or annotation I can add to Java classes that are dedicated to handling specific requests, and having URLs map to these based on their name. For example, have the URL ".../api/GetContactsRequest" map to a handler named GetContactsRequest (or 404 if no such handler exists). I know I could write servlets and map each URL to each servlet, but I figure the less boilerplate routing code/configuration, the better! These will mostly be application request handlers, communicating using JSON. I haven't figured out how I'll be handling static requests, but I'll most likely just be sending a big web application at the user that navigates itself or something.
For background, I'm using the Google App Engine so I have access to yaml configurations and their servlet APIs. So is there a standard way of doing this with either the Java servlet APIs or the Google App Engine-specific framework? I've only ever used specific Java servlet frameworks like Apache and stuff before, which were all already built by the time I started working on them, so I really don't know what there is to use with this environment. I'm also new to this all in general, and having trouble wading through what Servlets, Services, Filters, Listeners, and Handlers all are, and which is best for this simple routing behavior that I want/need. I'm worried I'll pick the wrong one, or don't even know of the one that would suit my needs.
This is what JAX-RS does - not exactly class name mapping, but mapping via annotations. See some of the features.
There are several implementations, personally I use RESTEasy - it works flawlessly on GAE. Additionally I use Jackson (comes with RESTEasy) to produce JSON.
If you need to produce html then look at htmleasy - it's a thin layer on top of RESTEasy enabling use of different html templating libraries. It'll help you separate logic from presentation.
EDIT:
If you really want to avoid using standard libs and writing something on your own, then write a servlet filter that inspects the request and forwards it to your servlet (or invoke custom code):
public class ForwardFilter implements Filter {
#Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) {
if(request.getRequestURI().equals("/some/path")){
request.getRequestDispatcher("/path/where/servlet/registered").forward(request, response);
return; // prevents normal request processing
}
// you need this for normal request path
filterChain.doFilter(request, response);
}
}