You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by "Zheng, Xiahong" <Xi...@FMR.COM> on 2008/01/17 22:11:39 UTC

Myfaces-1.2.1-snapshot: Spring injected bean not available in @PostContruct method

In my managed bean, I have a service bean configured and injected by
spring as in the following,

private DataService dataService;

@PostConstruct
public void retrieveData() {
        dataService.retrieveData();
}

Public void setDataService(DataService dataService) {
        this.dataService = dataService;
}

Using RI, dataService is already injected before retrieveData method is
called. However, this is not the case with myfaces. It seems that spring
injection happens much later resulting in NPE thrown in the method. Is
this expected with myfaces inplementation or a bug?




RE: Myfaces-1.2.1-snapshot: Spring injected bean not available in @PostContruct method

Posted by "Zheng, Xiahong" <Xi...@FMR.COM>.
Yes, that's exactly how I inject spring beans. I am injecting a spring managed bean to a JSF managed bean. What do you mean by defining all beans in spring?  Do you suggest I should have spring manage my JSF bean? Is it possible?

-----Original Message-----
From: Bernhard Huemer [mailto:bernhard.huemer@gmail.com] 
Sent: Friday, January 18, 2008 5:53 AM
To: MyFaces Discussion
Subject: Re: Myfaces-1.2.1-snapshot: Spring injected bean not available in @PostContruct method

Hello,

most probably he's injecting the Spring bean using managed-properties, 
i.e. I think his managed bean definition looks like the following:

<managed-bean>
   <managed-bean-name>...</managed-bean-name>
   ...

   <managed-property>
     <property-name>dataService</property-name>
     <value>#{dataService}</value>
   </managed-property>
</managed-bean>

However, there's a bug in MyFaces as it calls methods being annotated 
with @PostConstruct before all properties have been injected. I have 
already created a JIRA Issue, but I haven't got the time to fix this bug 
so far. Therefore I'd recommend you to define alle your beans in Spring 
directly, just as Gerhard has already mentioned.

regards,
Bernhard

On 01/18/2008 +0100,
"Gerald Müllan" <ge...@gmail.com> wrote:
> Hi,
> 
> Do you have the managed bean in faces-config.xml defined oder via
> spring configuration file?
> MyFaces should not be responsible for injecting a spring bean. This is
> done when using Spring beans only.
> 
> I would suggest to define it directly in Spring an use Spring`s
> init-method approach.
> 
> cheers,
> 
> Gerald
> 
> On Jan 17, 2008 10:11 PM, Zheng, Xiahong <Xi...@fmr.com> wrote:
>>
>>
>> In my managed bean, I have a service bean configured and injected by spring
>> as in the following,
>>
>> private DataService dataService;
>>
>> @PostConstruct
>> public void retrieveData() {
>>         dataService.retrieveData();
>> }
>>
>> Public void setDataService(DataService dataService) {
>>         this.dataService = dataService;
>> }
>>
>> Using RI, dataService is already injected before retrieveData method is
>> called. However, this is not the case with myfaces. It seems that spring
>> injection happens much later resulting in NPE thrown in the method. Is this
>> expected with myfaces inplementation or a bug?
>>


Re: Myfaces-1.2.1-snapshot: Spring injected bean not available in @PostContruct method

Posted by Gerald Müllan <ge...@gmail.com>.
When using Spring in combination with JSF it makes no sense to mix up
ManagedBean facility
controlled beans with Spring defined Service-/DAO-Beans.

I would recommend to define them also via Spring. In this case you can
take fully advantage of Spring features
and dont`t add unnecessary configuration overhead to your application.

cheers,

Gerald

On Jan 18, 2008 11:53 AM, Bernhard Huemer <be...@gmail.com> wrote:
> Hello,
>
> most probably he's injecting the Spring bean using managed-properties,
> i.e. I think his managed bean definition looks like the following:
>
> <managed-bean>
>    <managed-bean-name>...</managed-bean-name>
>    ...
>
>    <managed-property>
>      <property-name>dataService</property-name>
>      <value>#{dataService}</value>
>    </managed-property>
> </managed-bean>
>
> However, there's a bug in MyFaces as it calls methods being annotated
> with @PostConstruct before all properties have been injected. I have
> already created a JIRA Issue, but I haven't got the time to fix this bug
> so far. Therefore I'd recommend you to define alle your beans in Spring
> directly, just as Gerhard has already mentioned.
>
> regards,
> Bernhard
>
> On 01/18/2008 +0100,
>
> "Gerald Müllan" <ge...@gmail.com> wrote:
> > Hi,
> >
> > Do you have the managed bean in faces-config.xml defined oder via
> > spring configuration file?
> > MyFaces should not be responsible for injecting a spring bean. This is
> > done when using Spring beans only.
> >
> > I would suggest to define it directly in Spring an use Spring`s
> > init-method approach.
> >
> > cheers,
> >
> > Gerald
> >
> > On Jan 17, 2008 10:11 PM, Zheng, Xiahong <Xi...@fmr.com> wrote:
> >>
> >>
> >> In my managed bean, I have a service bean configured and injected by spring
> >> as in the following,
> >>
> >> private DataService dataService;
> >>
> >> @PostConstruct
> >> public void retrieveData() {
> >>         dataService.retrieveData();
> >> }
> >>
> >> Public void setDataService(DataService dataService) {
> >>         this.dataService = dataService;
> >> }
> >>
> >> Using RI, dataService is already injected before retrieveData method is
> >> called. However, this is not the case with myfaces. It seems that spring
> >> injection happens much later resulting in NPE thrown in the method. Is this
> >> expected with myfaces inplementation or a bug?
> >>
>
>



-- 
http://www.irian.at

Your JSF powerhouse -
JSF Consulting, Development and
Courses in English and German

Professional Support for Apache MyFaces

Re: Myfaces-1.2.1-snapshot: Spring injected bean not available in @PostContruct method

Posted by Bernhard Huemer <be...@gmail.com>.
Hello,

most probably he's injecting the Spring bean using managed-properties, 
i.e. I think his managed bean definition looks like the following:

<managed-bean>
   <managed-bean-name>...</managed-bean-name>
   ...

   <managed-property>
     <property-name>dataService</property-name>
     <value>#{dataService}</value>
   </managed-property>
</managed-bean>

However, there's a bug in MyFaces as it calls methods being annotated 
with @PostConstruct before all properties have been injected. I have 
already created a JIRA Issue, but I haven't got the time to fix this bug 
so far. Therefore I'd recommend you to define alle your beans in Spring 
directly, just as Gerhard has already mentioned.

regards,
Bernhard

On 01/18/2008 +0100,
"Gerald Müllan" <ge...@gmail.com> wrote:
> Hi,
> 
> Do you have the managed bean in faces-config.xml defined oder via
> spring configuration file?
> MyFaces should not be responsible for injecting a spring bean. This is
> done when using Spring beans only.
> 
> I would suggest to define it directly in Spring an use Spring`s
> init-method approach.
> 
> cheers,
> 
> Gerald
> 
> On Jan 17, 2008 10:11 PM, Zheng, Xiahong <Xi...@fmr.com> wrote:
>>
>>
>> In my managed bean, I have a service bean configured and injected by spring
>> as in the following,
>>
>> private DataService dataService;
>>
>> @PostConstruct
>> public void retrieveData() {
>>         dataService.retrieveData();
>> }
>>
>> Public void setDataService(DataService dataService) {
>>         this.dataService = dataService;
>> }
>>
>> Using RI, dataService is already injected before retrieveData method is
>> called. However, this is not the case with myfaces. It seems that spring
>> injection happens much later resulting in NPE thrown in the method. Is this
>> expected with myfaces inplementation or a bug?
>>


Re: Myfaces-1.2.1-snapshot: Spring injected bean not available in @PostContruct method

Posted by Gerald Müllan <ge...@gmail.com>.
Hi,

Do you have the managed bean in faces-config.xml defined oder via
spring configuration file?
MyFaces should not be responsible for injecting a spring bean. This is
done when using Spring beans only.

I would suggest to define it directly in Spring an use Spring`s
init-method approach.

cheers,

Gerald

On Jan 17, 2008 10:11 PM, Zheng, Xiahong <Xi...@fmr.com> wrote:
>
>
>
> In my managed bean, I have a service bean configured and injected by spring
> as in the following,
>
> private DataService dataService;
>
> @PostConstruct
> public void retrieveData() {
>         dataService.retrieveData();
> }
>
> Public void setDataService(DataService dataService) {
>         this.dataService = dataService;
> }
>
> Using RI, dataService is already injected before retrieveData method is
> called. However, this is not the case with myfaces. It seems that spring
> injection happens much later resulting in NPE thrown in the method. Is this
> expected with myfaces inplementation or a bug?
>
>



-- 
http://www.irian.at

Your JSF powerhouse -
JSF Consulting, Development and
Courses in English and German

Professional Support for Apache MyFaces