Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在spring中刷新AnnotationconfigwebapplicationContext之前,如何删除/忽略对象?_Java_Spring - Fatal编程技术网

Java 在spring中刷新AnnotationconfigwebapplicationContext之前,如何删除/忽略对象?

Java 在spring中刷新AnnotationconfigwebapplicationContext之前,如何删除/忽略对象?,java,spring,Java,Spring,我有两个独立的应用程序。当我的子应用程序启动时,我们会更新一些bean并刷新注释ConfigWebApplicationContext对象,但在刷新上下文后,我的MqttConnection对象开始连接和断开连接。 我不想刷新我的Mqttconnection对象 请建议如何在刷新之前从AnnotationconfigwebapplicationContext忽略/删除MqttConnection对象 静止的{ AnnotationConfigWebApplicationContext上下文=新

我有两个独立的应用程序。当我的子应用程序启动时,我们会更新一些bean并刷新
注释ConfigWebApplicationContext
对象,但在刷新上下文后,我的
MqttConnection
对象开始连接和断开连接。
我不想刷新我的
Mqttconnection
对象

请建议如何在刷新之前从
AnnotationconfigwebapplicationContext
忽略/删除
MqttConnection
对象


静止的{
AnnotationConfigWebApplicationContext上下文=新的AnnotationConfigWebApplicationContext();
regirster(MqttConnection.class);
register(Config.class);
if(checkChildServiceup()){
类[]configClasses=getServletConfigClasses();
如果(!ObjectUtils.isEmpty(configClasses)){
this.context.register(configClasses);
}
this.context.refresh();
}
}
公共静态类getServletConfigClasses(){
重新运行新类[]{AppConfig.Class,devService.Class,DbConfiguration.Class};
}
公共布尔静态checkChildServiceup(){
while(true){
如果(向上){
返回true;
打破
}
}

在刷新期间,春季会发生以下情况:

  • 它扫描配置的所有“资源”(XML、java@Config文件、用@Component及其派生词注释的类等等),并创建Bean定义,它是每个Bean的元数据。注意,Bean定义是一个元对象,而不是Bean对象本身

  • 调用名为“BeanFactoryPostProcessor”的钩子。这些钩子允许更改bean定义注册表—一个聚合步骤中解析的所有“信息”的对象

  • 初始化bean:创建、自动连接等

  • 因此,您可能需要实现自己的BeanFactoryPostProcessor,它将动态检查我看到的属性集,并从注册表中删除
    MqttConnection
    的bean定义

    @Component // (or register in @Configuration annotated class):
    // this must be a spring driven bean by itself, 
    // it resolves it but "puts aside" because it 
    // recognizes that its a special "hook" 
    
    public class SampleBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    
      @Override
      public void postProcessBeanFactory (
                ConfigurableListableBeanFactory beanFactory)
                throws BeansException {
    
           if(<WHATEVER_PROPERTY_CHANGED__PERFORM_THE CHECK)) {
             ((BeanDefinitionRegistry) beanFactory).removeBeanDefinition("myBean");
          }
      }
    }
    
    @Component//(或在@Configuration注释类中注册):
    //这一定是一个spring驱动的bean,
    //它解决了问题,但“搁置”了,因为
    //认识到这是一个特殊的“钩子”
    公共类SampleBeanFactoryPostProcessor实现BeanFactoryPostProcessor{
    @凌驾
    公共无效后处理BeanFactory(
    可配置列表可配置beanFactory(beanFactory)
    抛出BeansException{
    
    如果(谢谢,但是正在刷新的一些对象正在使用@Autowired Mqtt Connection,它们没有使用旧的引用,因此它们失败了。那么,您希望如何处理这些组件呢?您可以更改bean定义,为Mqtt Connection或其他什么创建一个无操作代理?也可以使用bean后处理器来实现这一点……I看到你已经接受了答案,但是删除了……你用这个方法遇到了什么困难吗?如果你找到一个替代的解决方案-考虑共享它为社区的利益。下一次,请检查你粘贴的代码是好的。你的<代码>返回< /代码>被写为<代码>eak;
    之后返回true;
    ,这使得它无法访问代码并且无法编译。