Giter Site home page Giter Site logo

spring-value-annotation's Introduction

@Value 相关的问题

有两个关于@Value 的注解的例子, 对 @Value 不是很理解

example1

@SpringBootApplication
public class SpringApplicationAnnotationExample1 {

    @Value("${friend.ages}")
    private List<Long> friendAges;

    public static void main(String[] args) {
        ApplicationContext applicationContext = SpringApplication.run(SpringApplicationAnnotationExample1.class, args);
        SpringApplicationAnnotationExample1 valueExample1 = applicationContext.getBean(SpringApplicationAnnotationExample1.class);
        System.out.println(valueExample1.friendAges);

    }

}

这个例子 能将 String 类型的 friend.ages="12,22,33" converter List 的 [12,22,33]

example2

@SpringBootApplication
@PropertySource("classpath:application.properties")
public class SpringApplicationAnnotationExample2 {

    @Value("${friend.ages}")
    private List<Long> friendAges;

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(SpringApplicationAnnotationExample2.class);
        context.refresh();
        SpringApplicationAnnotationExample2 valueExample1 = context.getBean(SpringApplicationAnnotationExample2.class);
        System.out.println(valueExample1.friendAges);

    }

}

这个例子 却不能 converter 成功 而且还报错了

Failed to convert value of type 'java.lang.String' to required type 'java.util.List'; nested exception is java.lang.NumberFormatException: For input string: "12,22,33"

我觉得很诧异

question

  • example1 能成功转化, example2 则不能

  • question2 是为啥 要加@PropertySource("classpath:application.properties") 这个注解, example1则会默认读到 application.properties 中属性值

回答 问题

  • example1能转换成功是 example2不能

    • example1的应用上下文 ConfigurableListableBeanFactory example2 则是 AnnotationConfigApplicationContext

    • 核心点是因为 ConfigurableListableBeanFactory 有这样的一个方法
      context.getBeanFactory().setConversionService(context.getEnvironment().getConversionService()); 代码演示

    • 给example2注册ApplicationConversionService 我用的BeanPostProcessor实现

  public class CustomBeanFactoryModify implements BeanFactoryPostProcessor {

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        beanFactory.setConversionService(new ApplicationConversionService());
    }
}

问题分析

我对比了两个应用上线文

这样代码返回了有差异

spring-value-annotation's People

Contributors

wenchaomartin avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.