You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Shannon, Andrew" <an...@pqa.com> on 2008/01/11 21:33:23 UTC

S2 Spring Bean DI to an i18n ListResourceBundle

I'd like to use a ListResourceBundle that pulls message information from
the database.  The pieces include (see below for code):
 
1.  a service that gets messages from the database.
2.  a ListResourceBundle
3.  applicationContext-struts.xml configuration to inject my service
into the resource bundle
4.  struts.xml configuration defining my i18n resource.
 
My new resource works fine if I hard code the key/value pairs, however,
the spring injected service is NULL.  It seems to me like the i18n
configuration is not Spring Bean aware.  Can anyone please help me
understand why the resource loader is unaware of spring or what I can do
to circumvent this?  'twould be very nice if it the DI could work in
this case as that's what makes life easy.
 
public class DbMessageResources extends ListResourceBundle implements
Serializable {
    private static final Log LOG =
LogFactory.getLog(DbMessageResources.class);
    private MessageService messageService;
 
    //spring injected service
    public void setMessageService(final MessageService messageService) {
        this.messageService = messageService;
    }
 
    protected Object[][] getContents() {
        LOG.debug("Loading DB Message Resources...");
        
        //the message is null
        List<Message> messages = messageService.getAllMessages();
        String[][] bundleContents = new
String[messages.size()][messages.size()];
 
        if (messages.size() > 0) {
            for (int i = 0; i < messages.size(); i++) {
                bundleContents[i][0] = messages.get(i).getId();
                bundleContents[i][1] = messages.get(i).getMessage();
 
                LOG.debug("Added DB Message Resources:{" +
bundleContents[i][0] + "," + bundleContents[i][1]);
            }
        }
 
//        Works fine but I don't want to hard code these.
//        Object[][] bundleContents = {
//                {"login.userLoggedOutMsg", "Logged out"},
//                {"login.authenticationErrorMsg", "Bad Login"}};
 
        return bundleContents;
    }
}
 
 
applicationContext-struts.xml entry:
 
    <bean id="dbMessageResources"
class="x.y.z.web.util.DbMessageResources" scope="singleton">
        <property name="messageService" ref="messageService"/>
    </bean>
 
struts.xml - ideally, but does not understand:
 
<constant name="struts.custom.i18n.resources"
value="dbMessageResources"/>
 
 
struts.xml with package declaration - works, but is not Spring injected.
I believe S2 is trying to create its own instance and skipping Spring,
hence it doesn't resolve the service:
 
<constant name="struts.custom.i18n.resources"
value="x.y.z.web.util.DbMessageResources"/>
 
Thanks,
Andrew
 

Re: S2 Spring Bean DI to an i18n ListResourceBundle

Posted by ARCS <an...@pqa.com>.
Simple solution.  Also noted that the bundle only gets loaded once and
everything seems to work fine.  Spring rocks.

String[] config=  new String[]{"applicationContext-resources.xml",
"applicationContext-dao.xml",
"applicationContext-service.xml"};

ApplicationContext context = new ClassPathXmlApplicationContext(config);
MessageService messageService =
(MessageService)context.getBean("messageService");

-- 
View this message in context: http://www.nabble.com/S2-Spring-Bean-DI-to-an-i18n-ListResourceBundle-tp14764821p14812183.html
Sent from the Struts - User mailing list archive at Nabble.com.


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