`
wiselyman
  • 浏览: 2076973 次
  • 性别:
  • 来自: 合肥
博主相关
  • 博客
  • 微博
  • 相册
  • 收藏
  • 博客专栏
    点睛spring4.1
    浏览量:80745
    点睛spring mvc4...
    浏览量:129940
    社区版块
    • ( 11)
    • ( 19)
    • ( 0)
    存档分类
    最新评论

    spring boot使用自定义的properties

          spring boot使用application.properties默认了很多配置。但需要自己添加一些配置的时候,我们应该怎么做呢。

     

    spring boot使用自定义的properties -欧洲杯足彩官网

    如:

     

    wisely2.name=wyf2
    wisely2.gender=male2

     

    @configurationproperties(prefix = "wisely2")
    public class wisely2settings {
    	private string name;
    	private string gender;
    	public string getname() {
    		return name;
    	}
    	public void setname(string name) {
    		this.name = name;
    	}
    	public string getgender() {
    		return gender;
    	}
    	public void setgender(string gender) {
    		this.gender = gender;
    	}
    }

     

    如我新建一个wisely.properties

    wisely.name=wangyunfei
    wisely.gender=male

     

    @configurationproperties(prefix = "wisely",locations = "classpath:config/wisely.properties")
    public class wiselysettings {
    	private string name;
    	private string gender;
    	public string getname() {
    		return name;
    	}
    	public void setname(string name) {
    		this.name = name;
    	}
    	public string getgender() {
    		return gender;
    	}
    	public void setgender(string gender) {
    		this.gender = gender;
    	}
    }
    

     

     

    @springbootapplication
    @enableconfigurationproperties({wiselysettings.class,wisely2settings.class})
    public class demoapplication {
        public static void main(string[] args) {
            springapplication.run(demoapplication.class, args);
        }
    }
    

    在别的bean中可直接注入

    @controller
    public class testcontroller {
    	@autowired
    	wiselysettings wiselysettings;
    	@autowired
    	wisely2settings wisely2settings;
    	@requestmapping("/test")
    	public @responsebody string test(){
    		system.out.println(wiselysettings.getgender() "---" wiselysettings.getname());
    		system.out.println(wisely2settings.getgender() "===" wisely2settings.getgender());
    		return "ok";
    	} 
    }
    

     

    新书推荐《javaee开发的颠覆者: spring boot实战》,涵盖spring 4.x、spring mvc 4.x、spring boot企业开发实战。

     

    京东地址:

    当当地址:

    亚马逊地址: 

    淘宝地址:

     

     

     

    或自己在京东、淘宝、亚马逊、当当、互动出版社搜索自选。

     


    6
    2
    分享到:
    |
    评论
    16 楼 yangjianzhouctgu 2018-05-24  
    汪老师,你好,我想就spring boot测试方面的问题请教一下您:一个好的测试用例应该是可以在任何时候的都可以执行成功的,对执行结果有直接影响的就是测试上下文了,在spring boot之前(applicationcontext.xml时),unitils可以用来准备测试前的数据,测试后回归掉,这就保证了一个测试用例可以在任何时候都可以执行成功。请问您一下,spring boot的测试是怎么保证这一点的呢?目前有这样的测试工具吗?我在网上看见有spring-test-dbunit这个工具,但是它提供的功能与unitils的功能相差较远。谢谢您
    15 楼 2017-05-09  
    汪老师,你好!
    我的springboot项目中有一个拦截器,我想在拦截器中注入参数类,但是总是无法注入,报空指针,请看下面的代码:

    @componentscan("com.abc")
    @enablescheduling
    @enableautoconfiguration
    @enableconfigurationproperties({globalsettings.class}) 
    public class myproject extends webmvcconfigureradapter{
        @bean
        public usersecurityinterceptor getmyinterceptor(){
            return new usersecurityinterceptor();
        }
        public void addinterceptors(interceptorregistry registry) {
            usersecurityinterceptor()).addpathpatterns("/my**");
            registry.addinterceptor(getmyinterceptor()).addpathpatterns("/my**");
            super.addinterceptors(registry);
        }
        ......
    }

    @component
    public class usersecurityinterceptor implements handlerinterceptor {
        @autowired 
        globalsettings globalsettings; 

        public boolean prehandle(httpservletrequest request,
    httpservletresponse response, object handler) throws exception {

        system.out.println("这里报空指针:" globalsettings.getappname());


    请问这是什么原因造成的呢? springboot加载bean的时候,有顺序要求吗?
    谢谢!

    14 楼 2017-05-09  
    18940818955 写道
    locations 这个属性没了?


    看看这个文章http://fabiomaffioletti.me/blog/2017/02/09/spring-configuration-properties-locations-deprecation-another-approach/
    13 楼 2017-05-09  
    18940818955 写道
    locations 这个属性没了?

    我也发现没了,这可如何是好
    12 楼 2017-02-07  
    locations 这个属性没了?
    11 楼 2017-02-02  
    太麻烦了。。。。。  
    10 楼 2017-01-09  
    按照书中第五章例子,启动之后不能访问页面,启动日志报
    cannot determine embedded database driver class for database type none
    请问这个是哪里没有配置吗
    我是用的版本为1.4.3.release
    9 楼 2017-01-03  
    请问我在拦截器中定义一个
    @autowired 
    wiselysettings wiselysettings;
    为什么是null呢,需要什么配置吗?谢谢
    8 楼 2016-11-30  
    荆人七十 写道
    如果我有一个配置类和多个格式相同内容不同的配置文件,我想根据这几个配置文件生成几个不同的配置类实例,用注解应该怎么做?

    找到了
    定义好类
    public class dataconfig {
    private string url;
    private string driverclassname;
    private string username;
    private string password;
    private tomcat tomcat;
            ...getter and setter...
    }

    在配置类里面定义
    @configuration
    public class propertiesconftest {
    @bean(name="edmsdataconfig")
    @configurationproperties(prefix = "datasource.edms")
    public dataconfig edmsdataconfig(){
    return new dataconfig();
    }

    @bean(name="uicdataconfig")
    @configurationproperties(prefix = "datasource.uic")
    public dataconfig uicdataconfig(){
    return new dataconfig();
    }
    }
    7 楼 2016-11-30  
    荆人七十 写道
    如果我有一个配置类和多个格式相同内容不同的配置文件,我想根据这几个配置文件生成几个不同的配置类实例,用注解应该怎么做?

    我也有这个问题,不知道怎么处理
    6 楼 2016-09-23  
    如果我有一个配置类和多个格式相同内容不同的配置文件,我想根据这几个配置文件生成几个不同的配置类实例,用注解应该怎么做?
    5 楼 2016-08-12  
    您好,看到你书上security的部分 页面有个hasrole(‘role_admin’)方法判断用户角色 但是为什么换了个名称就不起作用了呢 比如 hasrole('sys_admin') 就无效
    4 楼 wiselyman 2016-08-05  
    ilyyhb 写道
    你书里的源代码在哪下载啊?

    http://www.broadview.com.cn/?#book/bookdetail/bookdetailall.jsp?book_id=b47a1c54-5747-4628-b3e2-b8bc4899a97e&isbn=978-7-121-28208-9
    3 楼 2016-08-02  
    你书里的源代码在哪下载啊?
    2 楼 2016-07-12  
    同楼上,我也合肥的。
    1 楼 2016-07-07  
    刚买的你的书。。难得的好书,顺便说一句我也是合肥的

    相关推荐

      1.28 spring boot使用自定义的properties 1.29 改变自动扫描的包 1.30 spring boot junit单元测试 1.31 springboot启动时的banner设置 1.32 spring boot 文件上传(多文件上传) 1.33 导入时如何定制spring-boot依赖...

      spring boot创建自定义starter的完整步骤。解压后用idea打开三个工程,依次maven-lifecycle-install spring-boot-starter-autoconfigurer , spring-boot-starter, spring-boot-starter-test, 然后运行spring-boot...

      8 spring boot自定义starters 136 8.1 概述 136 8.2 步骤 137 9 更多springboot整合示例 144 10 spring boot与缓存 145 10.1 jsr107缓存规范 145 10.2 spring的缓存抽象 146 10.2.1 基本概念 146 10.2.2 整合项目 ...

      主要介绍了在springboot下读取自定义properties配置文件的方法,文中涉及到了spring-boot中读取config配置文件的两种方式,需要的朋友可以参考下

      自定义的spring-boot的hbase starter,为hbase的query和更新等操作提供简易的api并集成spring-boot的auto configuration 版本 本项目版本 hbase版本 1.0.0 hbase1.1.2 打包 修改相关的maven私服地址 gradle clean ...

      涵盖使用spring boot 进行java ee 开发的绝大数应用场景,包含:web 开发、数据访问、安全控制、批处理、异步消息、系统集成、开发与部署、应用监控、分布式系统开发等。 第一部分 点睛spring 4.x 第1 章 spring ...

      弹簧靴样板快速启动您的spring boot项目。 它包括spring security的自定义实现。特征使用jdbcauthentication和mysql的基于角色的访问控制; 自定义注册,登录和错误页面; 一个简单的web应用程序,显示不同的导航栏...

      自定义的spring-boot的dubbo starter,为spring-boot相关的项目使用dubbo提供简易的方式并集成spring-boot的auto configuration 版本 本项目版本 dubbo版本 1.0.0 dubbox自定义版本2.8.4-banyan 打包 修改相关的...

      涵盖使用spring boot 进行java ee 开发的绝大数应用场景,包含:web 开发、数据访问、安全控制、批处理、异步消息、系统集成、开发与部署、应用监控、分布式系统开发等。 第一部分 点睛spring 4.x 第1 章 spring ...

      涵盖使用spring boot 进行java ee 开发的绝大数应用场景,包含:web 开发、数据访问、安全控制、批处理、异步消息、系统集成、开发与部署、应用监控、分布式系统开发等。 第一部分 点睛spring 4.x 第1 章 spring ...

      使用spring boot v. 5. 了解spring boot特性 vi. 6. 迁移到生存环境 vii. 7. 高级主题 3. ii. 开始 i. 8. spring boot介绍 ii. 9. 系统要求 i. 9.1. servlet容器 iii. 10. spring boot安装 i. 10.1. 为java开发者...

      入门-springboot-helloworld 06、尚硅谷_springboot_入门-helloworld细节-场景启动器(starter) 07、尚硅谷_springboot_入门-helloworld细节-自动配置 08、尚硅谷_springboot_入门-使用向导快速创建spring boot应用...

      * 使用自定义 properties * 改变默认包扫᧿ * 自定义启动 banner * 导入 spring xml 配置文件 * 热部署 * 监控和管理生产环境 * starter 详解 * 依赖的版本 * 文件上传 * 集成 redis 缓存 * 之 spring cache * 集成 ...

      springboot学习笔记 spring基础 spring概述 spring的简史 xml配置 注解配置 java配置 spring概述 spring的模块 核心容器corecontainer ... spring-boot-starter-

      要开始使用该项目,只需签出该项目并按照application.properties设置数据库配置,然后将application.java作为java应用程序运行即可。 我的博客上提供了完整的解释此项目使用 spring boot 1.5.8。发布 java 8 的...

      spring和依赖注入使用针对rest api的主要spring生态系统项目,例如spring framework,spring mvc,spring boot,spring data jpa,spring security oauth和spring hateoas。 还了解依赖注入如何与spring,ioc容器,...

      查看application.properties以查看各种自定义。 您需要以user / changeme身份登录。 查看security.user.password属性以获取更多详细信息。 您还可以通过运行配置启用一个dev配置文件,该配置公开 h2 控制台并完全...

      使用gradle的spring boot入门项目 该项目旨在通过可用于部署的rest服务使您尽快启动并运行。 它使用以下技术: java sprint boot gradle 它演示了以下内容: 服务,域对象和测试客户端的单独项目 端到端单元测试 ...

      multi-properties springboot引入多个配置文件 1.自定义environmentpostprocessor的实现类,在回调中...org.springframework.boot.env.environmentpostprocessor=com.github.xujsh.springboot.config.envpostprocessor

      该项目使用spring boot,与数据库的连接和与邮件服务器的连接。先决条件全局安装的 , (至少8个)和 。 该项目应使用以下数据库: / 根据您需要的数据库系统编辑application.properties (请参阅安装)。 安装git ...

    global site tag (gtag.js) - google analytics
    网站地图