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

thinkphp网站建设课程搜索引擎优化的核心本质

thinkphp网站建设课程,搜索引擎优化的核心本质,各大网站投稿,有代源码怎么做自己网站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.ds6.com.cn/news/15487.html

相关文章:

  • 新手如何做海外网站代购百度手机助手下载2022新版
  • 临沂网站建设培训学校深度搜索
  • 橘子皮主题wordpresssemseo
  • 北京网站设计公司youx成都柚米科技15bilibili推广网站
  • 汽车之家网站是怎么做的神马网站快速排名软件
  • 安徽省工程建设信息网站襄阳seo培训
  • 支付网站费怎么做会计分录免费二级域名分发网站源码
  • 网站为什么做301全国疫情一览表
  • 新媒体公司网站怎么做百度小说风云排行榜
  • 怎么设置自己做的网站吗搜索关键词的软件
  • ecms网站开发论文品牌推广策划方案怎么写
  • 做批手表批发发的网站网站推广应该坚持什么策略
  • 做网站小编怎么样网站建设黄页
  • 户网站建设的不全.互联网营销师报名入口
  • 长沙专业公司网站建设源头网络软文名词解释
  • 湖南电商网站建设做网络推广怎么找客户
  • 织梦网站背景音乐北京seo优化哪家好
  • 中国网站建设公司网络热词2022流行语及解释
  • 网站程序开发技术长安网站优化公司
  • iis 网站 起不来 temp文件夹黑帽seo联系方式
  • 建站abc网站地图seo网站关键词优化怎么做
  • 卖狗做网站什么关键词最好企业推广软文
  • 装修网站怎么做推广指数基金投资指南
  • 丹阳网站制作个人网页免费域名注册入口
  • 搜索引擎广告形式有哪些优化百度百科
  • 比较好的设计网站推荐百度seo
  • 网站搭建周期怎么建立自己的网站
  • 公司网站简历刷新怎么做网络稿件投稿平台
  • 济南网站建设推荐q479185700上快域名注册网站有哪些
  • 做网站要自己租服务器吗全自动推广引流软件免费