不能转换为 ThreadPoolTaskExecutor
cannot be cast to ThreadPoolTaskExecutor
我必须编写一个 class 接受套接字连接并使用 Spring
将其传递给处理程序 class
我的听众 Class 是
public class Listener {
static Logger log = Logger.getLogger(Listener.class.getName());
public static void main(String args[]) throws IOException {
int port = 0;
if (args.length < 1) {
System.out.println("Usage: java -jar hovel.jar listenPortNumber");
System.exit(1);
} else {
port = Integer.parseInt(args[0]);
}
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
ThreadPoolTaskExecutor taskExecutor = (ThreadPoolTaskExecutor) context.getBean("TCPHandler");
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Listening on TCP port " + port);
for (;;) {
Socket sock = serverSocket.accept();
taskExecutor.execute(new TCPHandler(sock));
}
}
}
我的处理程序 class 是
public class TCPHandler implements Runnable {
private Socket moduleSocket;
public TCPHandler(Socket sock) {
moduleSocket = sock;
}
public void run() {
}
}
我的 Bean.xml 是
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="TCPHandler" class="org.hovel.server.TCPHandler">
<constructor-arg type="java.net.Socket">
<null />
</constructor-arg>
</bean>
<bean id="taskExecutor"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="5" />
<property name="maxPoolSize" value="10" />
<property name="WaitForTasksToCompleteOnShutdown" value="true" />
</bean>
</beans>
当我传递端口号时出现这个错误
**
Exception in thread "main" java.lang.ClassCastException:
org.hovel.server.TCPHandler cannot be cast to
org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor at
org.hovel.server.Listener.main(Listener.java:27)*
**
请帮助我
您在此处请求 TCPHandler
bean:
context.getBean("TCPHandler")
那 不是 一个 ThreadPoolTaskExecutor
,所以我不知道你为什么期望它是。如果你想检索执行器,你应该获取 that bean,它的 ID 为 taskExecutor
.
请注意,目前您实际上并没有以任何有用的方式使用 TCPHandler
bean,因为您正在 main
方法中显式构建 TCPHandler
。
我必须编写一个 class 接受套接字连接并使用 Spring
将其传递给处理程序 class我的听众 Class 是
public class Listener {
static Logger log = Logger.getLogger(Listener.class.getName());
public static void main(String args[]) throws IOException {
int port = 0;
if (args.length < 1) {
System.out.println("Usage: java -jar hovel.jar listenPortNumber");
System.exit(1);
} else {
port = Integer.parseInt(args[0]);
}
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
ThreadPoolTaskExecutor taskExecutor = (ThreadPoolTaskExecutor) context.getBean("TCPHandler");
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Listening on TCP port " + port);
for (;;) {
Socket sock = serverSocket.accept();
taskExecutor.execute(new TCPHandler(sock));
}
}
}
我的处理程序 class 是
public class TCPHandler implements Runnable {
private Socket moduleSocket;
public TCPHandler(Socket sock) {
moduleSocket = sock;
}
public void run() {
}
}
我的 Bean.xml 是
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="TCPHandler" class="org.hovel.server.TCPHandler">
<constructor-arg type="java.net.Socket">
<null />
</constructor-arg>
</bean>
<bean id="taskExecutor"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="5" />
<property name="maxPoolSize" value="10" />
<property name="WaitForTasksToCompleteOnShutdown" value="true" />
</bean>
</beans>
当我传递端口号时出现这个错误
**
Exception in thread "main" java.lang.ClassCastException: org.hovel.server.TCPHandler cannot be cast to org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor at org.hovel.server.Listener.main(Listener.java:27)*
**
请帮助我
您在此处请求 TCPHandler
bean:
context.getBean("TCPHandler")
那 不是 一个 ThreadPoolTaskExecutor
,所以我不知道你为什么期望它是。如果你想检索执行器,你应该获取 that bean,它的 ID 为 taskExecutor
.
请注意,目前您实际上并没有以任何有用的方式使用 TCPHandler
bean,因为您正在 main
方法中显式构建 TCPHandler
。