Results 1 to 5 of 5

Thread: Defining application color constants

  1. #1
    Join Date
    Mar 2005
    Posts
    135

    Default 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. #2
    Join Date
    Mar 2005
    Posts
    135

    Default

    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..

  3. #3
    Join Date
    Mar 2005
    Posts
    135

    Default

    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?

  4. #4
    Join Date
    Mar 2005
    Posts
    135

    Default

    Nobody an idea?

  5. #5
    Join Date
    Dec 2005
    Posts
    269

    Default

    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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •