[ 
https://issues.apache.org/jira/browse/WW-5524?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18012696#comment-18012696
 ] 

Martin commented on WW-5524:
----------------------------

I have this same problem. I am working around it with the following code: 


{code:java}
package com.pkb.util.struts;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.ObjectFactory;
import org.apache.struts2.conversion.TypeConverter;
import org.apache.struts2.factory.ConverterFactory;
import org.apache.struts2.inject.Inject;

import java.util.Map;

/**
 * Patched version of org.apache.struts2.factory.StrutsConverterFactory
 * 
 * Works around https://issues.apache.org/jira/projects/WW/issues/WW-5524
 * 
 * Patched to call back into objectFactory instead of directly calling into 
Container to build the bean
 * similar to other ConverterFactory instances. Otherwise we end up skipping 
Spring! And some of our 
 * TypeConverters rely on other beans injected from Spring, e.g. 
com.pkb.util.struts.DatePickerStringToInstantConverter
 */
public class StrutsConverterFactory implements ConverterFactory {
    private static final Logger LOG = 
LogManager.getLogger(StrutsConverterFactory.class);

    private ObjectFactory objectFactory;

    @Inject
    public void setObjectFactory(ObjectFactory objectFactory) {
        this.objectFactory = objectFactory;
    }

    @Override
    public TypeConverter buildConverter(Class<? extends TypeConverter> 
converterClass, Map<String, Object> extraContext) throws Exception {
        LOG.debug("Creating converter of type [{}]", 
converterClass.getCanonicalName());
        return (TypeConverter)objectFactory.buildBean(converterClass, 
extraContext);
    }
}
{code}

And registering this StrutsTypeConverter as the default in struts.xml like this:

{code:java}
<bean type="org.apache.struts2.factory.ConverterFactory" 
class="com.pkb.util.struts.StrutsConverterFactory"/>
{code}

I'll shortly raise a PR which changes the existing struts implementation to the 
same as above. At the moment it calls directly into Container#inject which 
bypasses SpringObjectFactory. I don't know why this worked prior to struts 6.0.0

> Dependency injection not working in custom StrutsTypeConverter(s)
> -----------------------------------------------------------------
>
>                 Key: WW-5524
>                 URL: https://issues.apache.org/jira/browse/WW-5524
>             Project: Struts 2
>          Issue Type: Bug
>          Components: Core
>    Affects Versions: 6.0.0, 6.0.3, 6.1.1, 6.2.0, 6.1.2.1, 6.3.0, 6.4.0, 
> 6.6.0, 6.6.1, 6.7.0
>            Reporter: John Wilson
>            Priority: Major
>             Fix For: 6.8.0, 7.1.0
>
>
> Dependency injection via annotations (such as @PersistenceContext, 
> @Autowired) are no longer working within custom StrutsTypeConverter(s) since 
> version 6.0.0.
> This relates to the changes done in WW-4996 



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to