我是 Spring 新手,我目前所在的项目正在使用 Spring IOC。这是我的应用程序的当前设置,我试图将其表示为独立 下面的类在服务器启动期间作为进程启动
客户端.java
public final class Client {
public static void main(String[] args) {
try {
ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml");
String[] beans = ctx.getBeanDefinitionNames();
for (String string : beans) {
System.out.println(beans);
}
} catch (Throwable t) {
t.printStackTrace();
}
}
}
context.xml
<!-- <context:component-scan base-package="com.tradeking" /> -->
<context:annotation-config />
<bean id="stream-core" class="com.StreamHandler" scope="singleton" init-method="init">
<constructor-arg>
<ref bean="streamingthread"/>
</constructor-arg>
</bean>
<bean id="streamingthread" class="com.StreamingThread" scope="singleton" >
</bean>
</beans>
StreamHandler.java
package com;
public class StreamHandler {
private StreamingThread streamThread;
public StreamHandler(StreamingThread streamThread) {
this.streamThread = streamThread;
}
public void init() {
this.streamThread.start();
}
}
StreamingThread.java
package com;
import java.util.HashSet;
import java.util.Set;
public class StreamingThread extends Thread {
private Set<String> streamSet = new HashSet();
public void run() {
while (true) {
for (int i = 0; i < 12; i++) {
streamSet.add("Test" + 1);
System.out.println("Run Called");
}
}
}
}
现在我的问题是,我有另一个名为 UbscHandler 的类,我需要在其中访问 StreamingThread 类中存在的 streamSet
所以我尝试了这种方式
如图所示,在 context.xml 文件中再添加一个 bean id
<context:annotation-config />
<bean id="streamingthreadnew" class="com.StreamingThread" scope="singleton" >
@Autowired
@Qualifier("streamingthreadnew")
private StreamingThread sThread;
我在这里面临2个问题
- 我无法访问 streamSet,因为它是私有的。
2.我可以使用 Spring 样式访问该特定类,而不是再创建一个 bean id。
编辑部分
现在我得到
Hi Streamed Thread Called0
java.lang.NullPointerException
at com.Client.anotherMethod(Client.java:33)
at com.Client.main(Client.java:26)
这是我的 context.xml
<bean id="streamingthread" class="com.StreamingThread" scope="singleton" >
</bean>
</beans>
**This is StreamingThread**
package com;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client{
@Autowired
@Qualifier("streamingthread")
private StreamingThread streamingthread;
public void run()
{
while(true)
{
}
}
public static void main(String[] args) {
try {
ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml");
String[] beans = ctx.getBeanDefinitionNames();
for (String string : beans) {
}
Client c = new Client();
c.anotherMethod();
} catch (Throwable t) {
t.printStackTrace();
}
}
public void anotherMethod()
{
Set set = streamingthread.getData();
System.out.println(set.size());
}
}
这是我的 StreaminThread 类
package com;
import java.util.HashSet;
import java.util.Set;
public class StreamingThread extends Thread {
int i=0;
StreamingThread()
{
System.out.println("Hi Streamed Thread Called"+i);
}
private Set<String> streamSet = new HashSet();
public Set getData()
{
return streamSet;
}
public void run() {
/*while (true) {
for (int i = 0; i < 12; i++) {
streamSet.add("Test" + 1);
//s System.out.println("Run Called"+i);
}
}*/
}
}