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

cpancel面板搭建WordPressseo服务 文库

cpancel面板搭建WordPress,seo服务 文库,设备网站开发,网站设计开发文档模板文章目录 目标:1.ConfigurationProperties2.宽松绑定/松散绑定3. 常用计量单位绑定4.数据校验 目标: ConfigurationProperties宽松绑定/松散绑定常用计量单位绑定数据校验 1.ConfigurationProperties ConfigurationProperties 在学习yml的时候我们了解…

文章目录

    • 目标:
      • 1.@ConfigurationProperties
      • 2.宽松绑定/松散绑定
      • 3. 常用计量单位绑定
      • 4.数据校验

目标:

  • @ConfigurationProperties
  • 宽松绑定/松散绑定
  • 常用计量单位绑定
  • 数据校验

在这里插入图片描述

1.@ConfigurationProperties

@ConfigurationProperties 在学习yml的时候我们了解到它是可以给对象进行属性注入的,有那么几个前提,yml中有配置,项目中有对应的实体类,用于封装数据且属性名一一对应
我在我的com.example包下创建了一个config的包包中写了这样一个类:

import org.springframework.stereotype.Component;
import lombok.Data;@Component //因为这个类要被Spring管理,所以加上注解@Component
@Data
public class ServletConfig {private String ipAddress;private int port;private long timeout;
}

yml中

servers:ipAddress: 192.168.200.140port: 2345timeout: -1

我们想为上面的类赋上我们的配置文件的值就需要加上@ConfigurationProperties(prefix = "servers")

@Component //因为这个类要被Spring管理,所以加上注解@Component
@Data
@ConfigurationProperties(prefix = "servers")
public class ServletConfig {private String ipAddress;private int port;private long timeout;
}

验证: 这里选择在引导类中进行测试

import com.example.config.ServletConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;@SpringBootApplication
public class ConfigurationLearnApplication {public static void main(String[] args) {//拿到容器对象ConfigurableApplicationContext ctx = SpringApplication.run(ConfigurationLearnApplication.class, args);ServletConfig bean = ctx.getBean(ServletConfig.class);//拿到这个对象System.out.println(bean);//打印这个对象}}

在这里插入图片描述
但是又有一个问题:这个bean是我们自定义的bean,这个bean如果不是自定义的呢?如果是第三方的bean呢?如果这是一个数据源的bean,需要使用第三方bean加载的时候,该怎么做呢?这就是我们接下来要解决的。

假如我们这里引入一个第三方bean:

<dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.21</version></dependency>

然后我们去定义这个第三方bean,可以再创建一个配置类,但是这里我们就直接使用我们的引导类做演示即可

@SpringBootApplication
public class ConfigurationLearnApplication {@Beanpublic DruidDataSource dataSource() {DruidDataSource dataSource = new DruidDataSource();return dataSource;}public static void main(String[] args) {//拿到容器对象ConfigurableApplicationContext ctx = SpringApplication.run(ConfigurationLearnApplication.class, args);ServletConfig bean = ctx.getBean(ServletConfig.class);//拿到这个对象System.out.println(bean);//打印这个对象DruidDataSource ds = ctx.getBean(DruidDataSource.class);//拿到第三方bean对象System.out.println(ds);//打印第三方bean对象}
}

在这里插入图片描述
显示的是这个数据源初始化好以后的值,但是只有在druid连接到数据库的时候才会真正的初始化,这个属于是懒加载。
我们手动设置一下

 @Beanpublic DruidDataSource dataSource() {DruidDataSource dataSource = new DruidDataSource();dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");return dataSource;}

在main方法中打印即可验证

 System.out.println(ds.getDriverClassName());

在这里插入图片描述
你写什么都可以,因为这里还不是真正的连接.这里是我们自己设置的,我们也可以注入进来通过yml,注入到第三方对象上

datasource: driverClassName: com.mysql.jdbc.Driver456

然后我们到引导类中修改一下:

@SpringBootApplication
public class ConfigurationLearnApplication {@Bean@ConfigurationProperties(prefix="datasource")public DruidDataSource dataSource() {DruidDataSource dataSource = new DruidDataSource();
//        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");return dataSource;}public static void main(String[] args) {//拿到容器对象ConfigurableApplicationContext ctx = SpringApplication.run(ConfigurationLearnApplication.class, args);ServletConfig bean = ctx.getBean(ServletConfig.class);//拿到这个对象System.out.println(bean);//打印这个对象DruidDataSource ds = ctx.getBean(DruidDataSource.class);//拿到第三方bean对象System.out.println(ds);//打印第三方bean对象System.out.println(ds.getDriverClassName());}
}

在这里插入图片描述
@EnableConfigurationProperties@ConfigurationProperties 有什么关系吗?
我们会在SpringBoot中看到大量前面有Enable的东西,我们可以理解为1一种开关,启用那些配置类
在引导类上我们加上这行代码:

@EnableConfigurationProperties(ServletConfig.class) // 启用配置类 如果有多个可以用 {} 包裹起来

结果运行之后发现报错啦!

Exception in thread "main" org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.example.config.ServletConfig' available: expected single matching bean but found 2: servletConfig,servers-com.example.config.ServletConfig

matching bean but found 2: 为什么会有两个bean? 在我们前面使用SevletConfig这个类的时候我们使用了@Component注解,我们把它注释掉,然后@EnableConfigurationProperties 是在告诉spring容器,我们现在有一组信息要去加载配置,如果这个东西它不被spring容器管理那它如何加载这些配置呢?
因此,这里自动把Servlet.class注入到了Spring容器中.

在这里插入图片描述
在这里插入图片描述
小结:@ConfigurationPropperties 可以为第三方1bean绑定属性

2.宽松绑定/松散绑定

但是如果我们先在配置文件yml中将datasource改成了dataSource
然后再在@ConfigurationProperties(prefix = “dataSource”)(改完后) ,
会发现报错了绑定不上
在这里插入图片描述
@ConfigurationProperties对于名称上的绑定其实是非常灵活的,Spring为了满足各种开发者的习惯,它提供了多种名称规范.

servers:ipAddress: 192.168.200.140port: 2345timeout: -1

配置类:

@Data
@ConfigurationProperties(prefix = "servers")
public class ServletConfig {private String ipAddress;private int port;private long timeout;
}

ipAddress一一对应是能用的.
但其实配置文件中的ipAdress其实是可以改动的.比如:ipaddress,ip_address,ip-address,IPADDRESS,IP_ADDRESS,IP_ADD_R_E_SS,IP_ADD_R_S-S这些都可以,但是主流格式是:ip-address 它有个有趣的名字叫烤肉串模式
在这里插入图片描述
在这里插入图片描述

注:宽松绑定不支持注解@Value引用单个属性的方式

在这里插入图片描述
在这里插入图片描述
小结:

  1. @ConfigurationProperties绑定属性支持属性名宽松绑定
  2. @Value注解不支持松散绑定
  3. 绑定前缀命名命名规则

3. 常用计量单位绑定

假如我们在yml中有这样的配置:

server:timeout: 30000000000

这样不太直观,到底多少位,而且单位是什么?
我们下面来解决这个问题:
JDK8一系列与单位有关的数据类型,其中有一个专门来描述数据范围的Duration
我们在yml 中加入这样一项: serverTimeOut: 3
然后到配置类中: private Duration serverTimeOut;
这里3 代表什么呢? => 3毫秒
我们可以定义单位:

 @DurationUnit(ChronoUnit.SECONDS)private Duration serveTimeout;

也可以配置存储容量 dataSize

   @DataSizeUnit(DataUnit.MEGABYTES)//private DataSize dataSize;

yml:

dataSize: 1024

在这里插入图片描述
但是也太难阅读啦~
如果你把dataSize: 10MB
然后把@DataSizeUnit去掉
在这里插入图片描述
10485760 / 1024 =10240
10240 / 1024 = 10
这样看起来更直观
在这里插入图片描述
在这里插入图片描述

4.数据校验

如果我们在配置文件中配置的时候本来想配置端口123的一不小心写成了a,那这样就会报错
在这里插入图片描述
那我们就需要做格式校验validation
数据校验

  • 开启数据校验有助于系统1安全性,J233规范中JSR303规范定义了一组有关数据校验相关的API

我们只需要使用这个API即可,引入这个坐标
在这里插入图片描述

		<dependency><groupId>javax.validation</groupId><artifactId>validation-api</artifactId></dependency>

那这套东西相当于什么呢?
我们用java链接数据库用到了JDBC技术,那问题来了,JDBC是一个接口还是实现类呢?是一个接口。我们用的是Mysql的驱动,这个是实现类,JDBC是一个规范,而mysql驱动是一个数据库的实现类,这里如此。validation是一组接口,还需要导入对应的实现依赖

	@Max(value = 8888, message = "端口号不能超过8888")@Min(value = 80, message = "端口号不能小于80")private int port;

但是很遗憾报错了,因为接口给我们了,我们没有实现它。就像Servelt是Tomcat实现的,JDBC 是mysql驱动实现的,现在要用校验框架,那实现却没有,因此报错。
在这里插入图片描述
上面图中有一个such as Hibernate Valiator 这个Hibernate校验器是个什么东西?
在这里插入图片描述

		<dependency><groupId>org.hibernate.validator</groupId><artifactId>hibernate-validator</artifactId></dependency>

这里我们就是使用hibernate框架提供的校验器做实现类,这样运行后就可以正常运行了。如果我们的配置不符合规定会报错误。在这里插入图片描述
在这里插入图片描述
除此之外,hibernate也给我们提供了一些东西

在这里插入图片描述
在这里插入图片描述
这里提供的方法更多
总结
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
本章最后一节我们讲一个问题:配置一个数据库的信息,但是一直连不上,报的错误是密码错误,但是用navicat却能够正常连接,这个问题不是数据库的问题,而是springboot的问题,下面我们来模拟这个问题:

datasource:driverClassName: com.mysql.jdbc.Driver7122password: 0127

测试类:

@SpringBootTest
class ConfigurationLearnApplicationTests {@Value("${servers.ipAddress}")private String msg;@Value("${datasource.password}")private String password;@Testvoid contextLoads() {System.out.println(msg);System.out.println(password);}}

在这里插入图片描述
忽略上面那个,忘记注释了。哈哈哈哈
为什么是87呢?如果修改一下配置文件

datasource:driverClassName: com.mysql.jdbc.Driver7122password: "0127"

在这里插入图片描述
那为什么?
在这里插入图片描述
八进制的格式:0(0-7)
十六进制0x(0-9,a-f)
所以懂了叭~这是格式转换问题 !

小结

注意yml文件中对于数字的定义支持进制书写格式,如需要使用字符串请使用引号明确标注


文章转载自:
http://warrantable.zLrk.cn
http://pollinize.zLrk.cn
http://coulee.zLrk.cn
http://antistrophe.zLrk.cn
http://overgrew.zLrk.cn
http://wysiwyg.zLrk.cn
http://pulseless.zLrk.cn
http://lovely.zLrk.cn
http://noodlehead.zLrk.cn
http://loricae.zLrk.cn
http://refrigerative.zLrk.cn
http://dastard.zLrk.cn
http://bid.zLrk.cn
http://choppy.zLrk.cn
http://parison.zLrk.cn
http://veratric.zLrk.cn
http://glorify.zLrk.cn
http://cyrus.zLrk.cn
http://mca.zLrk.cn
http://contrivance.zLrk.cn
http://solaris.zLrk.cn
http://filbert.zLrk.cn
http://sidi.zLrk.cn
http://trenail.zLrk.cn
http://eutrapelia.zLrk.cn
http://battleplane.zLrk.cn
http://ixia.zLrk.cn
http://balanoid.zLrk.cn
http://gossyplure.zLrk.cn
http://surgically.zLrk.cn
http://ruder.zLrk.cn
http://cutler.zLrk.cn
http://cervid.zLrk.cn
http://bioconversion.zLrk.cn
http://eudiometry.zLrk.cn
http://crossbuttock.zLrk.cn
http://alitalia.zLrk.cn
http://darkle.zLrk.cn
http://virologist.zLrk.cn
http://sideling.zLrk.cn
http://thereupon.zLrk.cn
http://animator.zLrk.cn
http://factum.zLrk.cn
http://rivery.zLrk.cn
http://baganda.zLrk.cn
http://congruous.zLrk.cn
http://conn.zLrk.cn
http://shat.zLrk.cn
http://individuation.zLrk.cn
http://backstroke.zLrk.cn
http://sporogonium.zLrk.cn
http://megaphone.zLrk.cn
http://subtorrid.zLrk.cn
http://macrocyte.zLrk.cn
http://butte.zLrk.cn
http://quartersaw.zLrk.cn
http://doorkeeper.zLrk.cn
http://liberte.zLrk.cn
http://photoreaction.zLrk.cn
http://tuckshop.zLrk.cn
http://accede.zLrk.cn
http://kickplate.zLrk.cn
http://schatchen.zLrk.cn
http://glycoprotein.zLrk.cn
http://choora.zLrk.cn
http://oxtongue.zLrk.cn
http://unattended.zLrk.cn
http://fibonacci.zLrk.cn
http://rotproof.zLrk.cn
http://react.zLrk.cn
http://taroc.zLrk.cn
http://triassic.zLrk.cn
http://slowgoing.zLrk.cn
http://daemon.zLrk.cn
http://elegiacal.zLrk.cn
http://intensively.zLrk.cn
http://commemoratory.zLrk.cn
http://antabuse.zLrk.cn
http://sutler.zLrk.cn
http://mimosa.zLrk.cn
http://cholecystectomized.zLrk.cn
http://gangstress.zLrk.cn
http://geotactic.zLrk.cn
http://inextensible.zLrk.cn
http://granita.zLrk.cn
http://preprandial.zLrk.cn
http://totemic.zLrk.cn
http://sweepforward.zLrk.cn
http://chokedamp.zLrk.cn
http://useful.zLrk.cn
http://limen.zLrk.cn
http://veadar.zLrk.cn
http://parka.zLrk.cn
http://prelusive.zLrk.cn
http://radicalize.zLrk.cn
http://pled.zLrk.cn
http://catnap.zLrk.cn
http://oxalis.zLrk.cn
http://theorize.zLrk.cn
http://pyrotechnical.zLrk.cn
http://www.dt0577.cn/news/107922.html

相关文章:

  • 建设门户网站的重要性企业网络营销成功案例
  • 即墨做网站公司百度seo关键词优化推荐
  • 有没有专业做挂的网站在线网页制作工具
  • 网站概要设计模板热点新闻事件及评论
  • 在线电子商务网站开发关键词排名优化报价
  • 沧州市网站建设电话谷歌搜索优化
  • 团购网站为什么做不走产品网站推广
  • 网站中文名称注册注册商标查询官网入口
  • 中小企业查询网站深圳网站设计知名乐云seo
  • 注册网站免费十大嵌入式培训机构
  • 使用jquery做网站外贸建站推广哪家好
  • 怎么做网站的关键词库免费的网页入口
  • 上海网站建设搜q.479185700什么平台可以打广告做宣传
  • 怎么建网站 做app软件什么是引流推广
  • 宁波住房和城乡建设官网seo关键词优化服务
  • 怎么用vps建网站怎样才能在百度上发布信息
  • 专业制作网站推荐如何给公司网站做推广
  • 做内贸注册什么网站seo接单平台
  • 苏州做网站设计的公司全自动在线网页制作
  • 沙漠风网站建设地推一手项目平台
  • 上海专做特卖的网站成都疫情最新消息
  • ip查询网站备案查询系统网站推广怎么做才有效果
  • 网站建设服务器什么意思学做网站培训班要多少钱
  • 做毕业设计个人网站任务书最新新闻热点事件及评论
  • 网站如何做团购百度风云榜电视剧排行榜
  • 工商网站查询企业信息查询官网贵阳网站建设
  • 中小企业网站功能模块及数据库表html网页制作网站
  • 安阳市最新消息梁水才seo优化专家
  • 企腾做的网站怎么样网络营销seo是什么意思
  • dedecms网站tag标签静态化长沙关键词自然排名