apolloconfig / apollo

Apollo is a reliable configuration management system suitable for microservice configuration management scenarios.
https://www.apolloconfig.com
Apache License 2.0
28.95k stars 10.19k forks source link

基于注解的使用方式导致context 上下初始化失败 #4991

Closed nixub closed 8 months ago

nixub commented 8 months ago

描述bug 我在这个应用比较老,spring的版本也不支持@configration 来是Apollo客户端实例化,所以,我按着文档提示的通过xml的方式来 加载 实例。但是他直接就报错了,是spring的版本太低了才导致不能正常注册bean吗?我一个spring4.0.2的也出现了同样的问题,不能通过xml 来注册Apollo实例

简洁明了地描述一下bug

复现

通过如下步骤可以复现:

  1. dac09fff6c07c7a4ee3d9d31887b74d

期望

简介明了地描述你希望正常情况下应该发生什么

截图

如果可以,附上截图来描述你的问题

额外的细节和日志

dosubot[bot] commented 8 months ago

🤖

您的问题可能是由于在较旧的应用程序中使用基于注解的方法,而Spring的版本较低导致的上下文初始化失败。根据文档,Apollo支持与Spring(Spring 3.1.1+)集成,并且支持更传统的基于XML的配置和当前更受欢迎的基于Java的配置。

如果您正在使用不支持基于注解的配置的旧版本Spring,您确实可以通过通过XML注册Apollo实例来解决此问题。文档提供了如何执行此操作的示例:

<?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:apollo="http://www.ctrip.com/schema/apollo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.ctrip.com/schema/apollo http://www.ctrip.com/schema/apollo.xsd">
    <apollo:config/>
    <bean class="com.ctrip.framework.apollo.spring.TestXmlBean">
        <property name="timeout" value="${timeout:100}"/>
        <property name="batch" value="${batch:200}"/>
    </bean>
</beans>

在上面的示例中,使用<apollo:config/>标签将Apollo配置注入到Spring环境中。然后使用从Apollo配置中获取的属性timeoutbatch配置TestXmlBean类。

在共享的上下文中,还提到Apollo支持Java Config和ConfigurationProperties将配置注入到bean对象中。它还支持几个新的注解以简化在Spring环境中的使用,例如@ApolloConfig,@ApolloConfigChangeListener和@ApolloJsonValue。但是,这些功能可能需要支持基于注解的配置的新版本Spring。

关于您的问题,我在Apollo的问题跟踪中找到了一个类似的已解决问题:在Spring 3.1.1中使用EnableApolloConfig时发生NPE。这个问题在2021年12月26日更新,通过一个拉取请求:修复在Spring 3.1.1中使用EnableApolloConfig时发生的NPE在2021年12月26日关闭。

如果您的问题仍然存在,或者您需要更多的帮助,欢迎您提供更多的信息,例如错误日志,配置和您正在使用的平台和操作系统。

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

nobodyiam commented 8 months ago

Consider starting with the official demo to grasp its functionality before integrating it into your project.

nixub commented 8 months ago

我是按着 文档去整合的。 但报这个 错classnotfound : beandefinitionRegistryPostProcessor; 我的spring bean.jar确实没有这个类,spring -version:2.5.6. 是低版本就没法 通过 加载这个bean了吗

nixub commented 8 months ago

这些老工程,总是遇到奇奇怪怪的问题,xml 除了那种方式,还有其它比较原始的方式 加载bean 吗

nobodyiam commented 8 months ago

The minimum Spring version supported by Apollo is 3.1.1. For older projects, you might want to use the Apollo API directly.

nixub commented 8 months ago

我已经升级了本地工程的spring相关依赖,和我之前整合成功的工程一致。其中有个小插曲。就是这个老工程没有slf4j 依赖,Apollo 依赖使用了,不容易被发现确实的是这个slf4j。我现在工程都正常了。唯独是java 1.6的,行里也取消了这些旧应用的计划。我打补丁失败了。不然可以试试。

nixub commented 8 months ago

我之前spring4.0.2 直接使用xml方式失败的原因是,我没有新建一个单独的xml文件来放置文档上的内容,导致出了问题。引以为戒。