Announcement Announcement Module
Collapse
No announcement yet.
Is it possible to test prototype scoped beans at startup Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Is it possible to test prototype scoped beans at startup

    I am trying to check dependencies are set during application startup. I have tried using @required and custom init methods but get an error message related to unable to verify .... exists at initialization.

    When I change the scope to singleton they work.

    The reason I have them scoped to prototype is because this is a struts2 project and these beans are statefull.

    Ultimately, what I would like to do is somehow ensure these beans have their dependencies set/ are not null during startup.

    I have tried many searches and cannot find a solution. Any ideas would be helpful.

  • #2
    Can you please post the error message you get?

    Also, prototype scoped beans will be created only when requested, and cannot be created at startup.

    If your requirement is to verify if the prototype bean dependencies, and custom init methods are set propertly, have a singleton bean which implements BeanFactoryAware interface. Using the context set in this singleton bean, get the all the bean definitions that are prototype (use getBeansOfType method) and call getBean() on the context for each returned prototype bean.

    Let me know if this helps.

    Comment


    • #3
      here is the error message I was recieving:

      INFO XmlConfigurationProvider:380 - Unable to verify action class [xxx] exists at initialization

      xxx was the name of the action I was putting in @Required or the init method in.

      I think your idea with the singleton bean should work I am going to try that this morning.

      Comment


      • #4
        So I am running into some issues - I am not seeing my new bean being run.

        do I want to create an init-method in this new bean where I do the checks or do I want to call another function from the setter method (setBeanFactory).

        I didn't see getBeansOfType in the interface for the basic beanFactory.

        When I looked at getBeansOfType in the ListableBeanFactory it looks like you need to provide a class ...so what I was attempting to do was provide the base action which all my other actions extend and then iterate through the map returned by getBeansOfType - is this correct?

        When I read your initial response sometimes it seems like I might be able to make a call to get all prototype scoped beans - if this is true can you provide some further guidance?


        Here is a copy of my class currently hooked up with an init method. I am just trying to get the keys for right now.

        public class StartupDependencyCheck implements BeanFactoryAware {

        private ListableBeanFactory beanFactory;

        @SuppressWarnings("unchecked")
        Map beans = beanFactory.getBeansOfType(BaseAction.class);


        public void init() {
        /* for(String key: beans.keySet()){
        System.out.println(key + "/" + beans.get(key));
        }

        for(Map.Entry<String, Object> entry : beans.entrySet()){
        System.out.println(entry.getKey() + "/" + entry.getValue());

        }
        */
        String s;

        for (Iterator iterator = beans.entrySet().iterator(); iterator.hasNext() {
        Map.Entry entry = (Map.Entry) iterator.next();
        System.out.println("key= " + (String) entry.getKey());
        }
        }


        public void setBeanFactory(BeanFactory arg0) throws BeansException {
        if (arg0 instanceof ListableBeanFactory) {
        this.beanFactory = (ListableBeanFactory) arg0;
        }
        }

        }

        my mapping in applicationContext looks like this for now
        <bean id="StartupDependencyCheck" class="com.xxx.util.StartupDependencyCheck" init-method="init" scope="Singleton">
        </bean>

        Comment

        Working...
        X