Java 在Spring中,new()对象实例不能使用afterProperties集属性

Java 在Spring中,new()对象实例不能使用afterProperties集属性,java,Java,我用弹簧靴2 在类中调试服务器BizChannelHandlerManager我想使用类 DebugServerDeviceStatusHandlerwith new(),但实例中的属性始终为null 如何使用new()方法设置属性 DebugServerDeviceStatusHandler: public class DebugServerDeviceStatusHandler implements InitializingBean { @Override public v

我用弹簧靴2

在类中调试服务器BizChannelHandlerManager我想使用类 DebugServerDeviceStatusHandlerwith new(),但实例中的属性始终为null

如何使用new()方法设置属性

DebugServerDeviceStatusHandler:

public class DebugServerDeviceStatusHandler implements InitializingBean {

    @Override
    public void afterPropertiesSet() throws Exception {
        this.platformEnum = PlatformEnum.DebugServer;
        this.mapInfo = ChannelThingMapInfoFactory.getInstance(platformEnum);
    }
}
DebugServerBizChannelHandlerManager:

public class DebugServerBizChannelHandlerManager extends AbstractChannelHandlerManager {



    @Override
    public ChannelPipeline tcpHandler(ChannelPipeline pipeline) {
        pipeline
                .addLast(new DebugServerDeviceStatusHandler())

在SpringBoot中,应该使用注释将类声明为组件或服务

@Component
public class DebugServerDeviceStatusHandler() 
要通过注入使用此类,需要@Autowired注释:

@Component
public class DebugServerBizChannelHandlerManager extends AbstractChannelHandlerManager {

    @Autowired
    DebugServerDeviceStatusHandler statusHandler;

    @Override
    public ChannelPipeline tcpHandler(ChannelPipeline pipeline) {
        pipeline
                .addLast(statusHandler)
不要忘记启用和配置组件扫描。
有关概念的进一步解释,请参阅spring boot中的

,您应该使用注释将类声明为组件或服务

@Component
public class DebugServerDeviceStatusHandler() 
要通过注入使用此类,需要@Autowired注释:

@Component
public class DebugServerBizChannelHandlerManager extends AbstractChannelHandlerManager {

    @Autowired
    DebugServerDeviceStatusHandler statusHandler;

    @Override
    public ChannelPipeline tcpHandler(ChannelPipeline pipeline) {
        pipeline
                .addLast(statusHandler)
不要忘记启用和配置组件扫描。
有关概念的进一步解释,请参见

Baceuse afterPropertiesSet()从未被调用。将DebugServerDeviceStatusHandler注入DebugServerBizChannelHandlerManager,然后将其添加到管道。@marc如果我想将多个DebugServerDeviceStatusHandler注入DebugServerBizChannelHandlerManager,我必须将@Scope(“prototype”)添加到DebugServerDeviceStatusHandler?如果使用@Scope(“prototype”),每次注入处理程序时,Spring都会创建一个新实例。另一种方法是创建多个处理程序类并将它们注入管道中。@marc如何将它们注入管道中?谢谢。使用@Autowired作为@stacker回答。如果有用,请向上投票:)Baceuse AfterPropertieSet()从未被调用。将DebugServerDeviceStatusHandler注入DebugServerBizChannelHandlerManager,然后将其添加到管道。@marc如果我想将多个DebugServerDeviceStatusHandler注入DebugServerBizChannelHandlerManager,我必须将@Scope(“prototype”)添加到DebugServerDeviceStatusHandler?如果使用@Scope(“prototype”),每次注入处理程序时,Spring都会创建一个新实例。另一种方法是创建多个处理程序类并将它们注入管道中。@marc如何将它们注入管道中?谢谢。使用@Autowired作为@stacker回答。如果有用,请投票:)