Announcement Announcement Module
Collapse
No announcement yet.
Problem initializing field without calling getBean() Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem initializing field without calling getBean()

    I've got the following piece of code, where I'd like to initialize map field without making a call to XmlBeanFactory#getBean() If this is possible please let me know what I am doing wrong...

    Code:
    package test;
    import org.jgroups.blocks.PartitionedHashMap;
    import org.springframework.beans.factory.xml.XmlBeanFactory;
    import org.springframework.core.io.ClassPathResource;
    public class JGroupsTest {
        /* I want this field to be initialized with Spring */
        private  PartitionedHashMap<String, String> map;
    
        public PartitionedHashMap<String, String> getMap() {
            return map;
        }
        public void setMap(PartitionedHashMap<String, String> map) {
            this.map = map;
        }
        public static void main(String[] args) {
            XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(
                    "context.xml"));
            new JGroupsTest().test();
        }
        public void test() {
            map.put("name", "Bela", 0); // NullPointerException is fired here
        }
    }

    Here is my context.xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!-- schema declaration ... -->
           
        <bean id="memcache" class="org.jgroups.blocks.PartitionedHashMap">
            <constructor-arg value="C:\JGroups\udp.xml"/>
            <constructor-arg value="demo-cluster"/>
        </bean>
        
        <bean id="jgroupsbean" class="test.JGroupsTest"
            p:map-ref="memcache"/>
    </beans>

  • #2
    Just use ClassPathXmlApplicationContext instead of XmlBeanFactory.

    Comment


    • #3
      Originally posted by denis.zhdanov View Post
      Just use ClassPathXmlApplicationContext instead of XmlBeanFactory.
      Thanks Denis, but i've got another problem now.
      I've changed my main method into:

      Code:
      public static void main(String[] args) {
              ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
          }
      so after adding a couple of spring jars that support ClassPathXmlApplicationContext functionality I ended up with the following problem:

      Code:
      Exception in thread "main" java.lang.NoSuchFieldError: ruleMemo
      	at org.springframework.expression.spel.generated.SpringExpressionsParser.<init>(SpringExpressionsParser.java:94)
      	at org.springframework.expression.spel.antlr.SpringExpressionsParserExtender.<init>(SpringExpressionsParserExtender.java:37)
      	at org.springframework.expression.spel.antlr.SpelAntlrExpressionParser.<init>(SpelAntlrExpressionParser.java:51)
      	at org.springframework.context.expression.StandardBeanExpressionResolver.<init>(StandardBeanExpressionResolver.java:57)
      	at org.springframework.context.support.AbstractApplicationContext.prepareBeanFactory(AbstractApplicationContext.java:441)
      	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
      	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
      	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
      	at test.JGroupsTest.main(JGroupsTest.java:24)
      I got that stack trace after adding antlr-3.1.3.jar file to my classpath (to resolve NoClassDefFoundError: org/antlr/runtime/Lexer)

      Tried to google it and found it to be fixed in 3.0.x release, (http://jira.springframework.org/browse/SPR-5819) Do you think its an old bug or just me doing something wrong?

      Comment


      • #4
        Originally posted by Royan View Post
        ...
        Tried to google it and found it to be fixed in 3.0.x release, (http://jira.springframework.org/browse/SPR-5819) Do you think its an old bug or just me doing something wrong?
        I'm not sure if that is already fixed. I just use antlr 3.0.1 instead.

        Comment


        • #5
          OK 3.0.1 did the trick but I still catch NPE, here is the code use
          Code:
              public static void main(String[] args) {
                  ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
                  JGroupsTest t = new JGroupsTest();
                  t.test();
              }
          What I'm trying to achieve is to avoid call to getBean("jgroupsbean") so I would not like to have the following code (though such code works):

          Code:
          public static void main(String[] args) {
                  ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
                  JGroupsTest t = (JGroupsTest) context.getBean("jgroupsbean");
                  t.test();
              }
          I only want the field "map" to be instantiated once I create JGroupsTest object

          Comment


          • #6
            The general spring ioc idea is to resolve all dependencies for the objects configured within the container. I.e. you setup a container and ask it for particular object.

            You are talking about resolving dependencies for the objects that were not created by the container. Luckily spring allows to do that via exploiting aop + aspectj weaving - 6.8.1. Using AspectJ to dependency inject domain objects with Spring but it requires additional setup and aop concept knowledge.

            Comment

            Working...
            X