当前位置: 首页 > news >正文

加强政府网站安全建设百度代理授权查询

加强政府网站安全建设,百度代理授权查询,企业网站建设制作多少钱,温州瓯北做网站1.PropertySource和PropertySources注解 1.1.PropertySource注解概述 PropertySource注解是Spring 3.1开始引入的配置类注解。通过**PropertySource注解可以将properties配置文件中的key/value存储到Spring的Environment中,Environment接口提供了方法去读取配置文…

1.@PropertySource和@PropertySources注解

1.1.@PropertySource注解概述

@PropertySource注解是Spring 3.1开始引入的配置类注解。通过**@PropertySource注解可以将properties配置文件中的key/value存储到SpringEnvironment中,Environment接口提供了方法去读取配置文件中的值,参数是properties配置文件中定义的key值。当然了,也可以使用@Value注解用${}占位符**为bean的属性注入值。

@PropertySource注解的源代码,如下所示

package org.springframework.context.annotation;import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;import org.springframework.core.io.support.PropertySourceFactory;@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(PropertySources.class)
public @interface PropertySource {String name() default "";String[] value();boolean ignoreResourceNotFound() default false;String encoding() default "";Class<? extends PropertySourceFactory> factory() default PropertySourceFactory.class;
}

从@PropertySource的源码中可以看出,可以通过@PropertySource注解指定多个properties文件,使用的形式如下所示:

@PropertySource(value={"classpath:/person.properties", "classpath:/car.properties"})

1.2.@PropertySources注解概述

@PropertySources注解的源码,如下所示:

package org.springframework.context.annotation;import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PropertySources {PropertySource[] value();
}

@PropertySources注解的源码比较简单,只有一个PropertySource[]数组类型的value属性,那我们如何使用@PropertySources注解指定配置文件呢?其实也很简单,使用如下所示的方式就可以了。

@PropertySources(value={@PropertySource(value={"classpath:/person.properties"}),@PropertySource(value={"classpath:/car.properties"}),
})

2.@PropertySource注解的用法

2.1.使用注解方式获取值

在工程的src/main/resources目录下创建一个配置文件person.properties,内容如下所示:

person.nickName=liqb

在Person类中新增一个nickName字段,如下所示:

package com.tianxia.springannotation.entity;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Value;import java.io.Serializable;/*** Person类* @author liqb* @date 2023-04-21 16:00**/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Person implements Serializable {/*** 姓名*/private String name;/*** 年龄*/private Integer age;/*** 昵称*/@Value("${person.nickName}")private String nickName;public Person(String name, Integer age) {this.name = name;this.age = age;}
}

在MainConfigOfPropertyValues配置类上添加一个@PropertySource注解,如下所示:

package com.tianxia.springannotation.config;import com.tianxia.springannotation.entity.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;/*** 配置类* @author liqb* @date 2023-05-04 11:28**/
@Configuration
// 使用@PropertySource读取外部配置文件中的key/value保存到运行的环境变量中,加载完外部的配置文件以后,使用${}取出配置文件中的值
@PropertySource(value={"classpath:/person.properties"})
public class MainConfigOfPropertyValues {@Bean("propertySource_person")public Person person() {return new Person();}
}

此时Person类的nickName字段的值为空。运行测试方法,输出结果如下所示:

/*** 测试类* @author liqb* @date**/
@Test
public void test() {ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class);Person person = applicationContext.getBean(Person.class);System.out.println(person);
}

在这里插入图片描述

此时Person类的nickName字段已经注入liqb这个值了

2.2.使用Environment获取值

使用@PropertySource注解读取外部配置文件中的key/value之后,是将其保存到运行的环境变量中了,所以我们也可以通过运行环境来获取外部配置文件中的值。

运行测试方法,输出结果如下所示:

/*** 使用Environment获取值* @author liqb* @date 2023-05-06 12:44*/
@Test
public void test01() {ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class);ConfigurableEnvironment environment = (ConfigurableEnvironment) applicationContext.getEnvironment();String property = environment.getProperty("person.nickName");System.out.println(property);
}

在这里插入图片描述

http://www.yidumall.com/news/56443.html

相关文章:

  • wordpress首页全屏广告宁波seo基础入门
  • 广州建设档案馆网站台州优化排名推广
  • 国外室内设计案例网站百度推广登录平台
  • 有效的网站建设公司十大外贸电商平台
  • 做经营行网站需要什么网络推广合作资源平台
  • 做网站对外贸有什么用全球疫情最新数据消息
  • dw做网站首页长宽设置多少网推渠道
  • 响应式app网站模板全球网站排行榜
  • 黄石做网站多少钱广州seo优化效果
  • 网站制作服务公司百度一下百度官方网
  • 做公众号编辑用什么网站网络营销师是做什么的
  • 河南省新冠肺炎疫情最新消息南京百度网站快速优化
  • 哪里有教用java做网站google chrome谷歌浏览器
  • 电商平台业务流程图网站关键词排名怎么优化
  • 深圳网站建设工作室seo工具软件
  • 给公司做网站数据分析腾讯企点客服
  • 假淘宝网站怎么做推广费用一般多少
  • 做网站的预算表百度关键词优化多久上首页
  • 未来软件网站建设手机百度app最新版下载
  • 烟台快速建站公司在线服务器网站
  • 高唐企业做网站推广互联网推广渠道有哪些
  • bootstrap购物网站模板下载seo行业岗位有哪些
  • 网站开发毕业设计代做seo建设者
  • 房产网站建站大连网络推广
  • 全球著名科技网站fifa最新排名出炉
  • wordpress布置网站教程全媒体运营师培训
  • 品牌推广的目的seo博客大全
  • 国外法院网站建设百度关键词推广网站
  • 哈尔滨网站建设多少钱百度网址是多少 百度知道
  • 网站建设需求计划书沈阳seo排名优化推广