当前位置: 首页 > 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.yayakq.cn/news/386074/

相关文章:

  • 网站头部设计优化全国工商网站查询企业信息
  • 网站欢迎页制作信誉好的盐城网站建设
  • 宣传网站制作关键词 网站
  • 网站建设柒金手指花总14什么网站有教做衣服视频的
  • 朝阳凌源网站建设网易163企业邮箱注册
  • 网站开发网站设计案例seo 优化教程
  • 网站内容要求做网站jsp和php
  • 新商盟网站开发时间多少钱 网站建设
  • 京东建站模板成都cms建站
  • 成都私人视频网站制作平台tv网站建设
  • 网站建设客户怎么寻找手机网页qq登录
  • 推广农村特色产品建设网站方案网站为何不显示百度商桥对话框
  • 泰安网站建设广告wordpress表格不显示
  • 免费制作个人网站的软件免费的软件下载大全安装
  • 网站设计论文提纲asp网站qq登录
  • 公司网站格式软文世界平台
  • 建立手机网站移动互联网服务管理中心官网
  • 做dw网站图片怎么下载地址电子商务能从事什么工作
  • 厦门市建设局报表网站wordpress加导航菜单
  • 东莞市做网站的公司服务五象新区开发建设指挥部网站
  • 网站设计论文题目北京个人注册公司
  • 智能家居网站模板jsp做的知名网站
  • 网站关键字怎么修改wordpress主题分为多章节
  • 网站建设排名奇艺地域php怎么创建网站
  • 编程除了做网站还能干什么wordpress抽奖主题
  • 宁波企业网站seo宝塔建设的网站火车头发布失败
  • 网站优化报表新农村建设投诉网站
  • 上海工信部网站备案设计投稿的网站有什么
  • wordpress子目录建站怎么选择主题网站如何做好内链
  • 开发网站网络公司百度app免费下载安装最新版