lukaszlenart commented on PR #528:
URL: https://github.com/apache/struts/pull/528#issuecomment-1087197928

   Yeah... I'm planning to write a guideline how to use Struts DI mechanism, it 
can be hard to understand on the first glimpse ;-)
   
   You can try to follow my changes in this 
[PR](https://github.com/apache/struts/pull/529/files) - I introduced an 
injectable date formatter with two default implementations, but users can 
provide they own if needed.
   
   So the whole trick is that you must define an extension point: 
`struts.ognl.expressionCacheFactory` (in my PR it was `struts.date.formatter`) 
and supporting interface/class that will be used together `OgnlCacheFactory` 
(`DateFormatter` in my PR). Now in `StrutsBeanSelectionProvider` you must 
register an alias which ties the extension point with the backing 
interface/class - in my case it was 
   
   ```
   alias(DateFormatter.class, StrutsConstants.STRUTS_DATE_FORMATTER, builder, 
props, Scope.SINGLETON);
   ```
   
   (your PR is missing this step, sorry that I didn't spot it ealy :( )
   
   Having that set, either you can make this a required dependency and provide 
at least one implementation of the interface/class or make it optional and 
handle nulls in your code.
   
   In my case I made it required and provided two implementations: 
`SimpleDateFormatAdapter` named `simpleDateFormatAdapter` (as a bean name) and 
`DateTimeFormatterAdapter` named `dateTimeFormatterAdapter` - the names can be 
of your choice, they to not play any role yet.
   
   Now you must register the beans in Struts DI:
   
   ```
   <bean type="org.apache.struts2.components.date.DateFormatter" 
name="simpleDateFormatter" 
class="org.apache.struts2.components.date.SimpleDateFormatAdapter" 
scope="singleton"/>
   <bean type="org.apache.struts2.components.date.DateFormatter" 
name="dateTimeFormatter" 
class="org.apache.struts2.components.date.DateTimeFormatterAdapter" 
scope="singleton"/>
   ```
   
   ^^ this ties implementation with the name.
   
   And now it's time to tell Struts DI which implementation to use for the 
extension point:
   
   ```
   struts.date.formatter=dateTimeFormatter
   ```
   
   and done! :)
   
   With such approach users can define they own implementations and used them 
with the extension point.
   
   If something is still unclear please ask, I will try to prepare the 
guideline.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to