英文:
Hazelcast addItemListener with spring autowired
问题
public static void main(String[] args) {
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
IQueue<Object> queue1 = hazelcastInstance.getQueue("q");
queue1.addItemListener(new UserListener(), true);
}
@Component
public class UserListener implements ItemListener<Object> {
@Autowired
private UserService userService;
public void itemAdded(ItemEvent<Object> arg0) {
if (arg0 != null) {
try {
userService.process(arg0);
} catch (SQLException e) {
e.printStackTrace();
}
} else {
System.out.println("null");
}
}
public void itemRemoved(ItemEvent<Object> item) {
System.out.println("The item is removed succesfully");
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>hazelcastServer</groupId>
<artifactId>hazelcastServer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>3.12.9</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
</dependencies>
</project>
英文:
I'm trying to create a hazelcast-spring application with listeners but with no success for now. When i try to autowire my UserListener
is null
.
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
IQueue<Object> queue1 = hazelcastInstance.getQueue("q");
queue1.addItemListener(new UserListener(), true);
}
This is my main method and it's working ok when i add the item listener with new instance of UserListener class (my listener) but i need to use the spring autowired annotation because in the listener i have service->dao and i want everything to be autowired.Any ideas how can i accomplish this?
This is my whole code:
Main Class:
public static void main(String[] args) {
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
IQueue<Object> queue1 = hazelcastInstance.getQueue("q");
queue1.addItemListener(new UserListener(), true);
}
Listener Class:
@Component
public class UserListener implements ItemListener<Object> {
@Autowired
private UserService userService;
public void itemAdded(ItemEvent<Object> arg0) {
if (arg0 != null) {
try {
userService.process(arg0);
} catch (SQLException e) {
e.printStackTrace();
}
} else {
System.out.println("null");
}
}
public void itemRemoved(ItemEvent<Object> item) {
System.out.println("The item is removed succesfully");
}
}
POM.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>hazelcastServer</groupId>
<artifactId>hazelcastServer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>3.12.9</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
</dependencies>
</project>
答案1
得分: 1
这部分代码:
queue1.addItemListener(new UserListener(), true);
会创建一个新的UserListener
对象,绕过了Spring,因此任何标记为@Autowired
的字段都不会被实例化。
你需要尝试:
UserListener userListener = applicationContext.getBean(UserListener.class);
queue1.addItemListener(userListener, true);
可能值得检查一下userListener
对象是否为null,因为Spring组件扫描可能没有找到你的类并创建了一个@Bean
,因为组件扫描可能会选择性地扫描哪些包。
英文:
This part of your code
queue1.addItemListener(new UserListener(), true);
creates a new UserListener object bypassing Spring, so any fields annotated as @Autowired
don't get instantiated.
You need to try
UserListener userListener = applicationContext.getBean(UserListener.class)
queue1.addItemListener(userListener, true);
It's probably worth checking if userListener
object is null, as it might be the Spring component scanning hasn't found you class and created a @Bean, as component scanning can be selective on which packages it scans.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论