Хочу инициализировать поле map с помощью Spring'а но при этом избежать вызова getBean() Подскажите, где я не прав
| Код | package test;
import org.jgroups.blocks.PartitionedHashMap; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource;
public class JGroupsTest {
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 }
}
|
Context.xml:
| Код | <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <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>
|
Выхлоп:
| Код | Exception in thread "main" java.lang.NullPointerException at test.JGroupsTest.test(JGroupsTest.java:28) at test.JGroupsTest.main(JGroupsTest.java:24)
|
|