我想从xml文件中注入端口值,但似乎无效。这是我的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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<bean id="serverSocket" class="milosz.filimowski.KomunikacjaSpring.ServerSocketMy">
<property name="port" value="3111" />
<property name="port2" value="911" />
</bean>
<bean id="server" class="milosz.filimowski.KomunikacjaSpring.Server">
<constructor-arg ref="serverSocket" />
</bean>
</beans>
我试图在我的ServerSocket类中注入端口值。
public class ServerSocketMy extends ServerSocket {
static int port = 6066;
int port2;
public ServerSocketMy() throws IOException {
super(port);
System.out.println("PORT2: "+port2);
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public void setPort2(int port2) {
this.port2 = port2;
}
}
程序运行时一切正常但端口值没有变化。
答案 0 :(得分:2)
更深入地研究bean lifecycle and dependency injection。对于您的示例,请注意:
tableView
是在一个阶段构建的。MySocketServer
的值仍为port2
(默认值)。