You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by tom <mv...@googlemail.com> on 2012/05/30 16:54:09 UTC

@PostConstruct method called before injection occurred

Hi,

I am using apache-tomee-1.0.0-plus(clean, no additional jars) and while
playing around with some dependency injection I stumbled upon a strange
behavior:

The method annotated with @PostConstruct gets called before the variables
are injected. According to the @PostConstruct JavaDoc it should be the other
way around.

So the Dependency Injection and the PostConstruct work, but I can't think of
a way to change the order.

At this moment I don't have a way to upload my war file, so I´ll just list
what my project contains so far:

It is a maven-archetype-webapp project

The web.xml looks like this:

<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"
	metadata-complete="false" version="2.5">

	<display-name>OpenEJB REST Example</display-name>

	<filter>
		<filter-name>TestFilter</filter-name>
		<filter-class>a.b.c.TestFilter</filter-class>
		<init-param>
			<param-name>testfilter</param-name>
			<param-value>testfilter</param-value>
		</init-param>
	</filter>

	<filter-mapping>
		<filter-name>TestFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

</web-app>

Whereas the filter actually does nothing.

The class containing @PostConstruct:

package a.b.c;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("hello")
public class Hello {

    @Inject
    private Greeting mGreeting;
    
    @PostConstruct
    public void init(){
        String tmp = "test";
    }
    
    @GET
    public String hello(){
        if(mGreeting == null){
            return "Well, it still doesn't work...";
        }else{
            return mGreeting.greet();
        }
    }
}

The String tmp = "test" is just there so I can set a breakpoint.


And the injected Greeting class:

package a.b.c;

public class Greeting {
    
    public String greet(){
        return "Greetings";
    }
}

So maybe I am doing something completely wrong here. ^^

Thanks in advance :)

--
View this message in context: http://openejb.979440.n4.nabble.com/PostConstruct-method-called-before-injection-occurred-tp4655229.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: @PostConstruct method called before injection occurred

Posted by tom <mv...@googlemail.com>.
Indeed, that way it works.

Thanks :)

--
View this message in context: http://openejb.979440.n4.nabble.com/PostConstruct-method-called-before-injection-occurred-tp4655229p4655296.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: @PostConstruct method called before injection occurred

Posted by Romain Manni-Bucau <rm...@gmail.com>.
@EJB or @Inject shoul dbe pretty close currently

well for a pojo it was buggy (cf the jira) but add @javax.ejb.Stateless to
your pojo at top level class and it should work even in 1.0.0

- Romain


2012/5/30 tom <mv...@googlemail.com>

> Thanks for your very fast reply :)
>
> I fear you left me somewhat behind there. So I did some googling and
> quessed
> that I should use the @EJB annotation instead. So I followed this tutorial
> here:
>
> http://openejb.apache.org/examples-trunk/cdi-basic/README.html
>
> It's the results in the same behavior for me. But now I really quess I did
> something wrong xD
>
> Maybe you could give me some more hints with EJBs?
>
> Thanks :)
>
> --
> View this message in context:
> http://openejb.979440.n4.nabble.com/PostConstruct-method-called-before-injection-occurred-tp4655229p4655232.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>

Re: @PostConstruct method called before injection occurred

Posted by tom <mv...@googlemail.com>.
Thanks for your very fast reply :)

I fear you left me somewhat behind there. So I did some googling and quessed
that I should use the @EJB annotation instead. So I followed this tutorial
here: 

http://openejb.apache.org/examples-trunk/cdi-basic/README.html

It's the results in the same behavior for me. But now I really quess I did
something wrong xD

Maybe you could give me some more hints with EJBs?

Thanks :)

--
View this message in context: http://openejb.979440.n4.nabble.com/PostConstruct-method-called-before-injection-occurred-tp4655229p4655232.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: @PostConstruct method called before injection occurred

Posted by Romain Manni-Bucau <rm...@gmail.com>.
https://issues.apache.org/jira/browse/TOMEE-207

- Romain


2012/5/30 Romain Manni-Bucau <rm...@gmail.com>

> Hi,
>
> you speak about rest services right?
>
> you are right but if you use an ejb instead it should be fine.
>
> I'll have a look soon on this topic
>
> - Romain
>
>
>
> 2012/5/30 tom <mv...@googlemail.com>
>
>> Hi,
>>
>> I am using apache-tomee-1.0.0-plus(clean, no additional jars) and while
>> playing around with some dependency injection I stumbled upon a strange
>> behavior:
>>
>> The method annotated with @PostConstruct gets called before the variables
>> are injected. According to the @PostConstruct JavaDoc it should be the
>> other
>> way around.
>>
>> So the Dependency Injection and the PostConstruct work, but I can't think
>> of
>> a way to change the order.
>>
>> At this moment I don't have a way to upload my war file, so I´ll just list
>> what my project contains so far:
>>
>> It is a maven-archetype-webapp project
>>
>> The web.xml looks like this:
>>
>> <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"
>>        metadata-complete="false" version="2.5">
>>
>>        <display-name>OpenEJB REST Example</display-name>
>>
>>        <filter>
>>                <filter-name>TestFilter</filter-name>
>>                <filter-class>a.b.c.TestFilter</filter-class>
>>                <init-param>
>>                        <param-name>testfilter</param-name>
>>                        <param-value>testfilter</param-value>
>>                </init-param>
>>        </filter>
>>
>>        <filter-mapping>
>>                <filter-name>TestFilter</filter-name>
>>                <url-pattern>/*</url-pattern>
>>        </filter-mapping>
>>
>> </web-app>
>>
>> Whereas the filter actually does nothing.
>>
>> The class containing @PostConstruct:
>>
>> package a.b.c;
>>
>> import javax.annotation.PostConstruct;
>> import javax.inject.Inject;
>> import javax.ws.rs.GET;
>> import javax.ws.rs.Path;
>>
>> @Path("hello")
>> public class Hello {
>>
>>    @Inject
>>    private Greeting mGreeting;
>>
>>    @PostConstruct
>>    public void init(){
>>        String tmp = "test";
>>    }
>>
>>    @GET
>>    public String hello(){
>>        if(mGreeting == null){
>>            return "Well, it still doesn't work...";
>>        }else{
>>            return mGreeting.greet();
>>        }
>>    }
>> }
>>
>> The String tmp = "test" is just there so I can set a breakpoint.
>>
>>
>> And the injected Greeting class:
>>
>> package a.b.c;
>>
>> public class Greeting {
>>
>>    public String greet(){
>>        return "Greetings";
>>    }
>> }
>>
>> So maybe I am doing something completely wrong here. ^^
>>
>> Thanks in advance :)
>>
>> --
>> View this message in context:
>> http://openejb.979440.n4.nabble.com/PostConstruct-method-called-before-injection-occurred-tp4655229.html
>> Sent from the OpenEJB User mailing list archive at Nabble.com.
>>
>
>

Re: @PostConstruct method called before injection occurred

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Hi,

you speak about rest services right?

you are right but if you use an ejb instead it should be fine.

I'll have a look soon on this topic

- Romain


2012/5/30 tom <mv...@googlemail.com>

> Hi,
>
> I am using apache-tomee-1.0.0-plus(clean, no additional jars) and while
> playing around with some dependency injection I stumbled upon a strange
> behavior:
>
> The method annotated with @PostConstruct gets called before the variables
> are injected. According to the @PostConstruct JavaDoc it should be the
> other
> way around.
>
> So the Dependency Injection and the PostConstruct work, but I can't think
> of
> a way to change the order.
>
> At this moment I don't have a way to upload my war file, so I´ll just list
> what my project contains so far:
>
> It is a maven-archetype-webapp project
>
> The web.xml looks like this:
>
> <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"
>        metadata-complete="false" version="2.5">
>
>        <display-name>OpenEJB REST Example</display-name>
>
>        <filter>
>                <filter-name>TestFilter</filter-name>
>                <filter-class>a.b.c.TestFilter</filter-class>
>                <init-param>
>                        <param-name>testfilter</param-name>
>                        <param-value>testfilter</param-value>
>                </init-param>
>        </filter>
>
>        <filter-mapping>
>                <filter-name>TestFilter</filter-name>
>                <url-pattern>/*</url-pattern>
>        </filter-mapping>
>
> </web-app>
>
> Whereas the filter actually does nothing.
>
> The class containing @PostConstruct:
>
> package a.b.c;
>
> import javax.annotation.PostConstruct;
> import javax.inject.Inject;
> import javax.ws.rs.GET;
> import javax.ws.rs.Path;
>
> @Path("hello")
> public class Hello {
>
>    @Inject
>    private Greeting mGreeting;
>
>    @PostConstruct
>    public void init(){
>        String tmp = "test";
>    }
>
>    @GET
>    public String hello(){
>        if(mGreeting == null){
>            return "Well, it still doesn't work...";
>        }else{
>            return mGreeting.greet();
>        }
>    }
> }
>
> The String tmp = "test" is just there so I can set a breakpoint.
>
>
> And the injected Greeting class:
>
> package a.b.c;
>
> public class Greeting {
>
>    public String greet(){
>        return "Greetings";
>    }
> }
>
> So maybe I am doing something completely wrong here. ^^
>
> Thanks in advance :)
>
> --
> View this message in context:
> http://openejb.979440.n4.nabble.com/PostConstruct-method-called-before-injection-occurred-tp4655229.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>