• spring复习:(62)自定义PropertySourcesPlaceHolderConfigurer


    一、在resources目录下建立配置文件my.properties

    age=33
    
    • 1

    二、自定义PropertySourcesPlaceholderConfigurer

    package cn.edu.tju.service2;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
    
    @Configuration
    @PropertySource("my.properties")
    public class MyConfig {
        @Bean
        public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer()
        {
            PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
            configurer.setPlaceholderPrefix("#{");
            configurer.setPlaceholderSuffix("}");
            return configurer;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    三、在bean中使用placeholder

    package cn.edu.tju.service2;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Service;
    
    @Service
    public class DemoService {
        @Value("#{age}")
        private int age;
    
        public int getAge(){
            return age;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    四、配置文件,配置PropertySourcesPlaceholderConfigurer

    
    
        
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    五、用于测试的主类

    package cn.edu.tju;
    
    import cn.edu.tju.anno.MovieRecommender;
    import cn.edu.tju.anno.MovieRecommender2;
    import cn.edu.tju.service2.DemoService;
    import cn.edu.tju.study.service.anno.domain.MyValueCalculator;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Start20 {
        public static void main(String[] args) {
            ApplicationContext applicationContext = new
                    ClassPathXmlApplicationContext("beans20.xml");
            DemoService bean = applicationContext.getBean(DemoService.class);
            System.out.println(bean.getAge());
    
    
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
  • 相关阅读:
    在低容错业务场景下落地微服务的实践经验
    【博客492】使用iptables trace跟踪iptables流量
    分销系统助力全行业新业态商业发展,分销系统案例分享
    【编译原理】手工打造词法分析器
    两年Java开发工作经验面试总结
    使用aop注解,实现日志增强,增强方式为前置,后置,环绕
    数组指针的使用
    Unraid 使用技巧集合
    bjpowernode_MyBatis
    k8s-项目测试环境部署
  • 原文地址:https://blog.csdn.net/amadeus_liu2/article/details/133336929