You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Bogoljub Pavičić <27...@proton.me.INVALID> on 2023/02/25 11:45:54 UTC

Factory method for dependency injection

Hi,

is it possible to produce beans using a factory method with struts2 default ioc container?
I want to inject an ObjectMapper but I want to configure the bean before inserting it in the container (similar to spring's @Bean method inside a @Configuration class)

@Bean
public ObjectMapper objectMapper() {

return new ObjectMapper(new JavaTimeModule());

}

the <bean> tag of struts.xml invokes only the empty constructor without giving any arguments
Thanks for your help

Re: Factory method for dependency injection

Posted by Bogoljub Pavičić <27...@proton.me.INVALID>.
Hi,

I followed your instructions and it works!

For anyone interested my implementation of the config provider is:

@Override
    public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {

        builder.factory(Bean.class, new Factory<Bean>() {
            @Override
            public ContentXmlModel create(Context context) throws Exception {
                return new Bean();
            }

            @Override
            public Class<? extends Bean> type() {
                return Bean.class;
            }
        }, Scope.SINGLETON);

    }


So just register a new factory without invoking the method create on the builder. You can ignore other overridden methods (loadPackages,destroy etc etc)

Thank you

Sent with Proton Mail secure email.

------- Original Message -------
On Wednesday, March 1st, 2023 at 8:27 AM, Lukasz Lenart <lu...@apache.org> wrote:


> sob., 25 lut 2023 o 12:47 Bogoljub Pavičić
> 276ee31fe0@proton.me.invalid napisał(a):
> 
> > Hi,
> > 
> > is it possible to produce beans using a factory method with struts2 default ioc container?
> > I want to inject an ObjectMapper but I want to configure the bean before inserting it in the container (similar to spring's @Bean method inside a @Configuration class)
> > 
> > @Bean
> > public ObjectMapper objectMapper() {
> > 
> > return new ObjectMapper(new JavaTimeModule());
> > 
> > }
> 
> 
> It's a bit more complicated in Struts, you must implement
> "ContainerProvider" or "ConfigurationProvider" interface, than
> register the provider:
> https://struts.apache.org/core-developers/configuration-provider-and-configuration
> 
> then use register() to add your beans
> https://github.com/apache/struts/blob/master/core/src/main/java/com/opensymphony/xwork2/config/ContainerProvider.java#L58
> 
> > the <bean> tag of struts.xml invokes only the empty constructor without giving any arguments
> 
> 
> Not really, you can use @Inject to inject required dependencies
> 
> 
> Regards
> --
> Łukasz
> + 48 606 323 122 http://www.lenart.org.pl/
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Factory method for dependency injection

Posted by Lukasz Lenart <lu...@apache.org>.
sob., 25 lut 2023 o 12:47 Bogoljub Pavičić
<27...@proton.me.invalid> napisał(a):
>
> Hi,
>
> is it possible to produce beans using a factory method with struts2 default ioc container?
> I want to inject an ObjectMapper but I want to configure the bean before inserting it in the container (similar to spring's @Bean method inside a @Configuration class)
>
> @Bean
> public ObjectMapper objectMapper() {
>
> return new ObjectMapper(new JavaTimeModule());
>
> }

It's a bit more complicated in Struts, you must implement
"ContainerProvider" or "ConfigurationProvider" interface, than
register the provider:
https://struts.apache.org/core-developers/configuration-provider-and-configuration

then use register() to add your beans
https://github.com/apache/struts/blob/master/core/src/main/java/com/opensymphony/xwork2/config/ContainerProvider.java#L58

> the <bean> tag of struts.xml invokes only the empty constructor without giving any arguments

Not really, you can use @Inject to inject required dependencies


Regards
-- 
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org