Announcement Announcement Module
Collapse
No announcement yet.
Defining application color constants Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Defining application color constants

    I'm implementing an application with Struts/Spring and one tile in my view contains a legend which mainly defines the colors used by the application.

    Previously, this was implemented by a ServletContextListener which loads all necessary data from the database and made it available for the user requests to show. These color definitions come from the database.

    How do I best implement this in Spring, so that the types (with their colors) will be read from the database when the application starts and will also be available to the application via the applicationContext?

  • #2
    I implemented this functionality now through an ApplicationListener, but don't know if this is the best choice.
    Code:
    public class ApplicationInitListener implements ApplicationListener, ServletContextAware {
      private static final Logger log = Logger.getLogger(ApplicationInitListener.class);
    
      private TypeService service;
      private ServletContext ctx;
    
      public void setTypeService(TypeService service) {
        this.service = service;
      }
    
      public void setServletContext(ServletContext ctx) {
        this.ctx = ctx;
      }
    
      public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ContextRefreshedEvent) {
          createTypeCache();
        } else if (event instanceof ContextClosedEvent) {
          log.info("[Context] Application terminated.");
        }
      }
    
      private void createTypeCache() {
        ctx.setAttribute(Constants.AKEY_TYPES, service.getTypes());
        log.info("[Context] Type cache initialized.");
      }
    
    }
    And in my applicationContext configuration:
    Code:
    <bean id="typeService" class="com.service.TypeServiceImpl" autowire="byName"/>
    
    <bean id="applicationInitListener" class="com.portal.listeners.ApplicationInitListener">
    	<property name="typeService">
    		<ref bean="typeService"/>
    	</property>
    </bean>
    But I can't imagine that this is the only viable solution for this problem. Is this the best approach afterall????

    It seems that my context get refreshed twice in application startup, b/c my log displays the following:
    INFO [main] (ApplicationInitListener.java:49) - [Context] Type cache initialized.
    INFO [main] (ApplicationInitListener.java:49) - [Context] Type cache initialized.

    The same with the "application terminated" log message..

    Comment


    • #3
      I can't believe that nobody could help me out with this little issue???

      This problem is kinda trivial for most applications, there must be better solutions to fix the problem, no?

      Comment


      • #4
        Nobody an idea?

        Comment


        • #5
          why don't you define your color service simply as one of beans inside of a ctx, I mean without the need to implement some Spring-specific interfaces?

          your service will be initialized upon the start-up anyway (unless lazy-init is specified), so why pull it explicitly?

          Comment

          Working...
          X