I can confirm that both methods work :-) Thanks!
I did add a GuiceEndpointModule which looks like this:
public class GuiceEndpointModule implements Module {
@Override
public void configure(Binder binder) {
binder.bind(TestEndpoint.class);
}
}
And that does the trick :-) But also adding a default contstructor with the
@Inject annotation on it does work!
I found this issue because I was writing intregration unit tests. Also for
those unit tests (JUnit, but I manually create an injector on setup of the
test) it works to add a default constructor with the @Inject on it...
Thank a lot for the help!!
Op vrijdag 18 oktober 2013 11:46:57 UTC+2 schreef Thomas Broyer:
>
> When Guice there's no explicit binding in your Guice config, and the
> resource has no @Inject-annotated constructor, then jersey-guice will only
> inject its members, so AOP won't work.
>
> https://github.com/jersey/jersey-1.x/blob/master/jersey/contribs/jersey-guice/src/main/java/com/sun/jersey/guice/spi/container/GuiceComponentProviderFactory.java
>
> Workaround: add a constructor and annotate it with @Inject, or add a
> bind(TestEndpoint.class) to your Guice module.
>
> On Friday, October 18, 2013 11:34:39 AM UTC+2, Dirk Vranckaert wrote:
>>
>> Guice instantiates it, in the GuiceContextListener you can see the 'new
>> ServletModule()'
>>
>> The ServletModule looks like this:
>> public class ServletModule extends
>> com.google.inject.servlet.ServletModule {
>> private static final String JERSEY_SCAN_PACKAGE =
>> "eu.vranckaert.worktime.json.endpoint.impl";
>> private static final String JERSEY_POJO_MAPPING_FEATURE = "true";
>>
>> @Override
>> protected void configureServlets() {
>> /*
>> * The following line will scan our package for Jersey Resources
>> */
>>
>> final Map<String, String> params = new HashMap<String, String>();
>> params.put("com.sun.jersey.config.property.packages",
>> JERSEY_SCAN_PACKAGE);
>> params.put("com.sun.jersey.api.json.POJOMappingFeature",
>> JERSEY_POJO_MAPPING_FEATURE);
>>
>> filter("/*").through(PersistFilter.class);
>> serve("/rest/*").with(GuiceContainer.class, params);
>> }
>> }
>>
>> In here I have the PersistFilter also for the session-per-http-request as
>> explained in the docs (at the bottom):
>> https://code.google.com/p/google-guice/wiki/JPA
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <persistence version="1.0"
>> xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="
>> http://www.w3.org/2001/XMLSchema-instance"
>> xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
>> http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
>>
>> <persistence-unit name="myDB" transaction-type="RESOURCE_LOCAL">
>> <provider>org.hibernate.ejb.HibernatePersistence</provider>
>> <exclude-unlisted-classes>false</exclude-unlisted-classes>
>> <properties>
>> <property name="hibernate.connection.driver_class"
>> value="com.mysql.jdbc.Driver"/>
>> <property name="hibernate.connection.url"
>> value="jdbc:mysql://localhost:3306/WorkTime"/>
>> <property name="hibernate.connection.username" value="root"/>
>> <property name="hibernate.connection.password" value=""/>
>> <property name="hibernate.dialect"
>> value="org.hibernate.dialect.MySQLDialect"/>
>> <property name="hibernate.show_sql" value="false" />
>> <property name="hibernate.archive.autodetection"
>> value="class"/>
>> <property name="hibernate.id.new_generator_mappings"
>> value="true"/>
>> </properties>
>> </persistence-unit>
>> </persistence>
>>
>> Remark the the transaction type that is set to RESOURCE_LOCAL. I also
>> tried removing the transaction type. According to the docs I understand
>> that if I set the transaction type to RESOURCE_LOCAL that I would have a
>> session per transaction...
>>
>> Kr
>>
>> Dirk
>>
>> Op vrijdag 18 oktober 2013 11:27:40 UTC+2 schreef Thomas Broyer:
>>>
>>> Who's responsible for instantiating the TestEndpoint class? Is it really
>>> instantiated by Guice or is Guice only injecting its members (in which case
>>> AOP won't work) ?
>>>
>>> On Friday, October 18, 2013 11:06:53 AM UTC+2, Dirk Vranckaert wrote:
>>>>
>>>> I'm building a backend application for one of my Android applications.
>>>> My setup is Hibernate as ORM, MySQL as DB, Guice as DI and
>>>> Guice-Persist for the link between ORM and DI.
>>>>
>>>> My webservice-endpoint class looks like this:
>>>>
>>>> @Path("test")
>>>> public class TestEndpoint {
>>>> @Inject
>>>> private UserDao userDao;
>>>>
>>>> @GET
>>>> @Path("persistUser")
>>>> @Produces(MediaType.TEXT_PLAIN)
>>>> @Transactional
>>>> public String persistUser() {
>>>> User user = new User();
>>>> user.setEmail("[email protected]");
>>>> user.setFirstName("FirstName");
>>>> user.setLastName("LastName");
>>>> user = userDao.persist(user);
>>>> return "ok";
>>>> }
>>>> }
>>>>
>>>> When I manually manage the transaction it works and my entity is
>>>> persisted:
>>>>
>>>> @Path("test")
>>>> public class TestEndpoint {
>>>> @Inject
>>>> private UserDao userDao;
>>>>
>>>> @Inject
>>>> private Provider<EntityManager> em;
>>>>
>>>> @GET
>>>> @Path("persistUser")
>>>> @Produces(MediaType.TEXT_PLAIN)
>>>> public String persistUser() {
>>>>
>>>> ((HibernateEntityManager)em.get()).getSession().getTransaction().begin();
>>>>
>>>> User user = new User();
>>>> user.setEmail("[email protected]");
>>>> user.setFirstName("FirstName");
>>>> user.setLastName("LastName");
>>>> user = userDao.persist(user);
>>>>
>>>>
>>>> ((HibernateEntityManager)em.get()).getSession().getTransaction().commit();
>>>>
>>>> return "ok";
>>>> }
>>>> }
>>>>
>>>> My UseDaoImpl that is injected via UserDao looks like this:
>>>>
>>>> public abstract class UserDaoImpl {
>>>> @Inject
>>>> private Provider<EntityManager> em;
>>>>
>>>> public EntityManager getEntityManager() {
>>>> return em.get();
>>>> }
>>>> public User persist(User instance) {
>>>> getEntityManager().persist(instance);
>>>> return instance;
>>>> }
>>>>
>>>> public User findById(String id) {
>>>> return getEntityManager().find(User.class, id);
>>>> }
>>>> }
>>>>
>>>> My GuiceContextListener looks like this:
>>>> public class GuiceContextListener extends GuiceServletContextListener {
>>>> @Override
>>>> protected Injector getInjector() {
>>>> Injector injector = Guice.createInjector(
>>>> new DatabaseModule(),
>>>> new GuiceDaoModule(),
>>>> new GuiceServiceModule(),
>>>> new ServletModule()
>>>> );
>>>> return injector;
>>>> }
>>>> }
>>>>
>>>> The DatabaseModule:
>>>> public class DatabaseModule extends AbstractModule {
>>>> private static final String JPA_UNIT = "myDB";
>>>>
>>>> @Override
>>>> protected void configure() {
>>>> install(new JpaPersistModule(JPA_UNIT));
>>>> }
>>>> }
>>>>
>>>> The GuiceDaoModule:
>>>> public class GuiceDaoModule implements Module {
>>>> @Override
>>>> public void configure(Binder binder) {
>>>> binder.bind(UserDao.class).to(UserDaoImpl.class);
>>>> }
>>>> }
>>>>
>>>> And this is my web.xml configuration:
>>>> <?xml version="1.0" encoding="ISO-8859-1"?>
>>>> <web-app xmlns="http://java.sun.com/xml/ns/javaee"
>>>> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>>>> xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
>>>> http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
>>>> version="2.5">
>>>> <display-name>MyApp</display-name>
>>>>
>>>> <listener>
>>>>
>>>> <listener-class>eu.vranckaert.test.guice.GuiceContextListener</listener-class>
>>>> </listener>
>>>>
>>>> <!-- GUICE -->
>>>> <filter>
>>>> <filter-name>guiceFilter</filter-name>
>>>>
>>>> <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
>>>> </filter>
>>>>
>>>> <filter-mapping>
>>>> <filter-name>guiceFilter</filter-name>
>>>> <url-pattern>/*</url-pattern>
>>>> </filter-mapping>
>>>> </web-app>
>>>>
>>>>
>>>> When I do findById(..) I can also retrieve my record, so my DB
>>>> connection is working, I'm just not able to use the @Transactional
>>>> annotation...
>>>> So I found something about wrong scope of my EntityManager, but I have
>>>> no clue how I can change the entity manager's scope...
>>>>
>>>> You guys have any idea what is wrong with my setup that the annotation
>>>> does not work?
>>>>
>>>> Dirk
>>>>
>>>
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice.
For more options, visit https://groups.google.com/groups/opt_out.