On Dec 22, 2007, at 4:35 AM, wesley wrote:

> I finally figured it out. After I put a web-beans.xml in META-INF  
> folder,
> everything worked fine.
>
> Below is my Struts2 ObjectFactory for Resin WebBeans (simple but run
> flawlessly ^_^):

Very nice.  I've added a new method to WebBeansContainer, which will  
give your ObjectFactory more capabilities:

   ComponentFactory createTransient(Class type);

createTransient lets you create objects like servlets, i.e. they can  
use the Resin injection capabilities like @In, @Named, and even the  
interceptors, but don't require a <bean> configuration or even a  
@Component annotation.

So, you'd change the code to look like:

   ComponentFactory factory;
   factory = webbeans.resolveByType(clazz);

   if (factory == null)
     factory = webbeans.createTransient(clazz);

   return factory.get();

In the case of struts2, that would mean Actions could use injection  
without any extra configuration.  If you do have a <bean> or  
<component> or @Component, the code will use the configured bean,  
otherwise it will create a new component and use the normal injection  
on it.

Also, I've added an ObjectFactory implementation at  
com.caucho.xwork2.ResinObjectFactory.  I haven't had a chance to test  
it with struts2 yet, but the implementation is simple enough that it  
should just work.

-- Scott

> ====================================================================== 
> ===
> /**
>  * Resin WebBeans ObjectFactory
>  */
> public class ResinObjectFactory extends StrutsObjectFactory {
>
>  static final Log logger =
>    LogFactory.getLog(ResinObjectFactory.class.getName());
>
>  @SuppressWarnings(value = "unchecked")
>  public Object buildBean(Class clazz, Map extraContext) throws
> java.lang.Exception {
>   Container container = WebBeansContainer.getCurrent();
>   ComponentFactory factory = container.resolveByType(clazz);
>   logger.debug("Building bean: " + clazz.getName());
>   if (factory != null) {
>    logger.debug("Got instance from webbeans container: " +  
> factory.get());
>    return factory.get();
>   } else {
>    logger.debug("Not a managed webbean. Create a new instance.");
>    return clazz.newInstance();
>   }
>  }
> }
> ====================================================================== 
> ===
> Thanks for your help, Scott.
>
> ----- Original Message -----
> From: "wesley" <[EMAIL PROTECTED]>
> To: "General Discussion for the Resin application server"
> <resin-interest@caucho.com>
> Sent: Saturday, December 22, 2007 10:11 AM
> Subject: Re: [Resin-interest] How Resin/Ioc used for struts2/ 
> xwork2,need an
> ObjectFactory implementation
>
>
>> Thanks Scott,
>> I tried already, both in Servlet and JSP or in an ObjectFactory
>> implemetation class, but got null ComponentFactory instance.
>>
>> My test JSP:
>> --------------------------------------------------------------------- 
>> ---------------
>> <%@ page contentType="text/html;charset=UTF-8" language="java" %>
>> <%@ page import="javax.webbeans.Container" %>
>> <%@ page import="com.caucho.webbeans.manager.WebBeansContainer" %>
>> <%@ page import="javax.webbeans.ComponentFactory" %>
>> <html>
>>  <head><title>Simple jsp page</title></head>
>>  <body>
>>  <%
>>   Container container = WebBeansContainer.create();
>>   ComponentFactory factory =
>> container.resolveByType(com.foo.bar.MyClass.class);
>>   out.print("my factory is " + factory);
>>  %>
>>
>>  </body>
>> </html>
>> --------------------------------------------------------------------- 
>> ---------------
>>
>> and com.foo.bar.MyClass was annotationed with @Component (or
>> unneccesary?).
>>
>> The html output is:
>> --------------------------------------------------------------------- 
>> ---------------
>> my factory is null
>> --------------------------------------------------------------------- 
>> ---------------
>>
>> Did I miss something or there should be some extra config work to  
>> do in
>> resin-web.xml like ELSolver?
>>
>>
>>
>> ----- Original Message -----
>> From: "Scott Ferguson" <[EMAIL PROTECTED]>
>> To: "General Discussion for the Resin application server"
>> <resin-interest@caucho.com>
>> Sent: Saturday, December 22, 2007 12:24 AM
>> Subject: Re: [Resin-interest] How Resin/Ioc used for struts2/ 
>> xwork2,need
>> an
>> ObjectFactory implementation
>>
>>
>>>
>>> On Dec 21, 2007, at 7:15 AM, wesley wrote:
>>>
>>>> To implement a com.opensymphony.xwork2.ObjectFactory, we should
>>>> override
>>>> method
>>>>     public Object buildBean(Class clazz, Map extraContext);
>>>> and create/obtain a bean instance of specified bean class (provided
>>>> by param
>>>> "clazz").
>>>>
>>>> I think I may get a WebBeansContainer instance as follow
>>>>     WebBeansContainer container = WebBeansContainer.create();
>>>> and then use it to create/obtain bean instance.
>>>
>>> You can treat it as:
>>>
>>> javax.webbeans.Container container = WebBeansContainer.create();
>>>
>>> Then use
>>>
>>> javax.webbeans.ComponentFactory component = container.resolveByType
>>> (clazz);
>>>
>>> And then
>>>
>>>   return component.get();
>>>
>>> (or component.create(); if you want to force a creation)
>>>
>>> The API piece of the webbeans draft spec/implementation is  
>>> sketchy by
>>> the way.  So it's very likely the specifics will change in the next
>>> version of Resin.  In other words, it's fine as a temporary bridge
>>> for things like structs2/xwork2.
>>>
>>> For example, the spec seems to define javax.webbeans.Component
>>> instead of ComponentFactory, but that conflicts with the
>>> @javax.webbeans.Component annotation.   In other words, the spec
>>> can't possibly be correct.
>>>
>>> The annotations and <bean>/<component> are solid.  It's just the API
>>> that's a bit of a mess.
>>>
>>> -- Scott
>>>>
>>>> How could I do this?
>>>>
>>>> ----- Original Message -----
>>>> From: "wesley" <[EMAIL PROTECTED]>
>>>> To: "General Discussion for the Resin application server"
>>>> <resin-interest@caucho.com>
>>>> Sent: Friday, December 21, 2007 8:06 PM
>>>> Subject: [Resin-interest] How Resin/Ioc used for struts2/
>>>> xwork2,need an
>>>> ObjectFactory implementation
>>>>
>>>>
>>>>> I'm using Struts2 in nearly every new projects these days, with
>>>>> Guice as
>>>>> the
>>>>> IoC injector.
>>>>> I want to switch to Resin's WebBeans implementation and Resin/IoC.
>>>>> One of the main concerns is that I could not figure out how to
>>>>> write a
>>>>> ObjectFactory implementation using
>>>>> Resin/IoC/WebBeans infrastucture, like Guice/Spring did.
>>>>>
>>>>> Could anyone provide me an example or some instructions/tips for
>>>>> me to
>>>>> follow?
>>>>> Thanks very much.
>>>>>
>>>>> Wesley
>>>>
>>>>
>>>>
>>>> _______________________________________________
>>>> resin-interest mailing list
>>>> resin-interest@caucho.com
>>>> http://maillist.caucho.com/mailman/listinfo/resin-interest
>>>
>>>
>>>
>>> _______________________________________________
>>> resin-interest mailing list
>>> resin-interest@caucho.com
>>> http://maillist.caucho.com/mailman/listinfo/resin-interest
>>>
>>
>>
>>
>> _______________________________________________
>> resin-interest mailing list
>> resin-interest@caucho.com
>> http://maillist.caucho.com/mailman/listinfo/resin-interest
>>
>
>
>
> _______________________________________________
> resin-interest mailing list
> resin-interest@caucho.com
> http://maillist.caucho.com/mailman/listinfo/resin-interest



_______________________________________________
resin-interest mailing list
resin-interest@caucho.com
http://maillist.caucho.com/mailman/listinfo/resin-interest

Reply via email to