Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NPE occurred when using EnableApolloConfig with Spring 3.1.1 #4178

Closed
3 tasks done
nobodyiam opened this issue Dec 25, 2021 · 1 comment · Fixed by #4180
Closed
3 tasks done

NPE occurred when using EnableApolloConfig with Spring 3.1.1 #4178

nobodyiam opened this issue Dec 25, 2021 · 1 comment · Fixed by #4180
Labels
area/client apollo-client bug Categorizes issue or PR as related to a bug.

Comments

@nobodyiam
Copy link
Member

  • I have checked the discussions
  • I have searched the issues of this repository and believe that this is not a duplicate.
  • I have checked the FAQ of this repository and believe that this is not a duplicate.

Describe the bug
When using @EnableApolloConfig with Spring 3.1.1, NullPointerException will occur.

To Reproduce
Steps to reproduce the behavior:

  1. Mark a configuration class with @EnableApolloConfig
@Configuration
@EnableApolloConfig(value = "application", order = 10)
public class AppConfig {
  @Bean
  public NormalBean normalBean(@Value("${batch:100}") int batch) {
    NormalBean bean = new NormalBean();
    bean.setBatch(batch);
    return bean;
  }
}
  1. Run the application with Spring version 3.1.1
  2. NullPointerException will occur
Exception in thread "main" java.lang.NullPointerException
	at com.ctrip.framework.apollo.spring.spi.DefaultApolloConfigRegistrarHelper.resolveNamespaces(DefaultApolloConfigRegistrarHelper.java:52)
	at com.ctrip.framework.apollo.spring.spi.DefaultApolloConfigRegistrarHelper.registerBeanDefinitions(DefaultApolloConfigRegistrarHelper.java:29)
	at com.ctrip.framework.apollo.spring.annotation.ApolloConfigRegistrar.registerBeanDefinitions(ApolloConfigRegistrar.java:20)
	at org.springframework.context.annotation.ConfigurationClassParser.processImport(ConfigurationClassParser.java:331)
	at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:228)
	at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:149)
	at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:126)
	at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:263)
	at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:203)
	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:617)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:446)
	at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)
	at com.ctrip.framework.apollo.demo.spring.AnnotationApplication.main(AnnotationApplication.java:12)

Expected behavior
The application should work well

Screenshots
If applicable, add screenshots to help explain your problem.

Additional Details & Logs

  • Version
  • Error logs
  • Configuration
  • Platform and Operating System
@nobodyiam
Copy link
Member Author

After some investigation, here is what I found now.

  1. The ApolloConfigRegistrar and DefaultApolloConfigRegistrarHelper are marked EnvironmentAware in resolved embedded value in annotation @ApolloConfigChangeListener #3349
    see 6ce0932#diff-99cf529f565d3db2b0cb6d6c6a6ccdc92d0404cbb897cb61dcee90636bb12db1R24

  2. We use the Environment to resolve the namespace placeholders
    see 6ce0932#diff-0bd5d05308645d08b006bdca711e6954b0305d1315e8b0d4625f2b68337261dbR52

  3. However, ApolloConfigRegistrar and DefaultApolloConfigRegistrarHelper are both of type ImportBeanDefinitionRegistrar, which doesn't support EnvironmentAware until Spring 3.2.x
    see spring-projects/spring-framework@146a66f

  4. So the environment variable will always be null if running with Spring versions prior to 3.2.x, which explains why npe occurred.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/client apollo-client bug Categorizes issue or PR as related to a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants