码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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
  • 相关阅读:
    【java学习】面向对象特征之一:封装和隐藏(23)
    叹服,阿里自述 SpringCloud 微服务:入门 + 实战 + 案例,一网打尽
    深入POD
    R语言ggplot2可视化:可视化分面散点图的标签数据为词云、使用ggrepel包的geom_text_repel函数避免词云标签互相重叠
    2023年【电工(中级)】考试资料及电工(中级)找解析
    Mysql的分布式事务原理理解
    Python+requests编写的自动化测试项目
    万宾科技智能井盖传感器特点介绍
    基于BS的在线考试系统设计与开发
    oracle数据库报列表中最大表达式为1000错误
  • 原文地址:https://blog.csdn.net/amadeus_liu2/article/details/133336929
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号