spring配置文件
SpringBoot读取外部配置文件的方法

SpringBoot读取外部配置⽂件的⽅法1.SpringBoot配置⽂件SpringBoot使⽤⼀个以application命名的配置⽂件作为默认的全局配置⽂件。
⽀持properties后缀结尾的配置⽂件或者以yml/yaml后缀结尾的YAML的⽂件配置。
以设置应⽤端⼝为例properties⽂件⽰例(application.properties):server.port=80YAML⽂件⽰例(application.yml):server:port: 80在properties和yml/yaml配置⽂件同时存在的情况下, 在同⼀⽬录下,properties配置优先级 > YAML(YML)配置优先级2.配置⽂件⽬录SpringBoot配置⽂件可以放置在多种路径下,不同路径下的配置优先级有所不同。
可放置⽬录(优先级从⾼到低)./config/ (当前项⽬路径config⽬录下);./ (当前项⽬路径下);classpath:/config/ (类路径config⽬录下);classpath:/ (类路径config下).优先级由⾼到底,⾼优先级的配置会覆盖低优先级的配置;SpringBoot会从这四个位置全部加载配置⽂件并互补配置;我们可以从ConfigFileApplicationListener这类便可看出,其中DEFAULT_SEARCH_LOCATIONS属性设置了加载的⽬录:private static final String DEFAULT_SEARCH_LOCATIONS = "classpath:/,classpath:/config/,file:./,file:./config/";接着getSearchLocations⽅法中去逗号解析成Set,其中内部类Loader负责这⼀配置⽂件的加载过程,包括加载profile指定环境的配置,以application+’-’+name格式的拼接加载。
基于springBoot配置文件properties和yml中数组的写法

基于springBoot配置⽂件properties和yml中数组的写法⽬录springBoot配置⽂件properties和yml数组写法这两种⽅法你选择哪种都可以.properties和.yml的写法区别springBoot配置⽂件properties和yml数组写法这⾥介绍⼀下springBoot中的两种⽂件配置⽅式中数组的使⽤,也就是集合。
以下是我springBoot中使⽤的 application.properties ⽂件其实很好理解,我的configs是⼀个集合,configs[0].appid代表我配置的第⼀个对象中的appid的值miniapp.configs[0].appid = 111111miniapp.configs[0].secret= 222222miniapp.configs[0].token = 333333miniapp.configs[0].aesKey = 444444miniapp.configs[0].msgDataFormat = JSONminiapp.configs[1].appid = 111miniapp.configs[1].secret = 222miniapp.configs[1].token = 333miniapp.configs[1].aesKey = 444miniapp.configs[1].msgDataFormat = JSON这个是使⽤application.yml的⽅式,因为YAML 本⾝⽀持 list 类型,所以可以在 application.yml ⽂件中添加:yml如果配置普通字符串miniapp:configs:- appid: 111secret: 222token: 333aesKey: 444msgDataFormat: JSON- appid: 111secret: 222token: 333aesKey: 444msgDataFormat: JSON这两种⽅法你选择哪种都可以下⾯展⽰类代码的写法:package com.platform.miniprogram;import lombok.Data;import org.springframework.boot.context.properties.ConfigurationProperties;import ponent;import java.util.List;/*** @Classname WxMaProperties* @Description TODO* @Date 2020/10/10 10:48* @Created by lyc*/@Data@ConfigurationProperties(prefix = "miniapp")@Componentpublic class WxMaProperties {private List<Config> configs;@Datapublic static class Config {/*** 设置微信⼩程序的appid*/private String appid;/*** 设置微信⼩程序的Secret*/private String secret;/*** 设置微信⼩程序消息服务器配置的token*/private String token;/*** 设置微信⼩程序消息服务器配置的EncodingAESKey*/private String aesKey;/*** 消息格式,XML或者JSON*/private String msgDataFormat;}}解释:@Data就是省略了get/set⽅法你可以直接删掉写成get/set@ConfigurationProperties(prefix = "miniapp")prefix 这个前缀⼀定要写对configs是集合的名字,要和配置表中的信息⼀致。
Spring的xml文件详解

Spring的xml⽂件详解spring的xml配置⽂件头:<?xml version="1.0" encoding="UTF-8"?><beans xmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance"xmlns:context="/schema/context"xsi:schemaLocation="/schema/beans/schema/beans/spring-beans-3.0.xsd/schema/context/schema/context/spring-context-3.0.xsd">...<!--中间xml⽂件部分-->.....</beans>⼀直在复制黏贴,但是不知道作⽤是什么,不理解的话常出错。
xmlns和命名空间⾸先,介绍⼀下xmlns的作⽤,如下所⽰,⼀个 xml ⽂档中如果包含如下两种定义不同,但是名称相同的元素, xml 解析器是⽆法解析的,因为它不能确定当你调⽤document.getElementsByTagName("book") 时应该返回哪个元素。
<!-- 这⾥的 table 元素描述的是⼀个表格--><table><tr><td>Apples</td><td>Bananas</td></tr></table><!-- 这⾥的 table 元素描述的是⼀个家居桌⼦--><table><name>African Coffee Table</name><width>80</width><length>120</length></table>这时候可以通过在名称增加前缀解决这个问题<!-- 这⾥的 table 元素描述的是⼀个表格--><h:table> <!--添加了前缀 h --><h:tr><h:td>Apples</h:td><h:td>Bananas</h:td></h:tr></h:table><!-- 这⾥的 table 元素描述的是⼀个表格--><f:table> <!--添加了前缀 f --><f:name>African Coffee Table</f:name><f:width>80</f:width><f:length>120</f:length></f:table>由此,引⼊⼀个概念命名空间,通过增加前缀表⽰不同的那是不同命名空间下的table,从⽽解决了⽭盾,但是不同的⼈都有⾃⼰创建的不同的命名空间来描述同样的东西,不利于xml⽂件信息的解析,⽐如说,同样都是⽔果,可以从颜⾊和⾹味不同⾓度来定义成如下两种形式:<!--按照⽔果⾹味来定义--><perfume:fruit><name>....</name><perfume>.....</perfume></perfume:fruit><!--按照⽔果颜⾊来定义--><color:fruit><name>....</name><color>....</color></color:fruit>为此,w3c(万维⽹联盟)对于⼀些类型,定义了对应的命名空间和这些类型的标准,xml解释器碰到这些类型的时候就会通过这些标准去解析这类型的标签,为了确保命名空间的唯⼀,所以不同的命名空间的通常使⽤URL作为被识别的id,如下例⼦:xmlns:xsi="/2001/XMLSchema-instance"这句话的作⽤是当前引⼊了⼀个叫做xsi的命名空间,xsi可以在接下来要使⽤该命名空间时所使⽤的,如下:<xsi:schemaLocation="...... ......">⽽这个很长的字符串,则是xsi这个名称空间被xml解释器内部所识别的时候所真正使⽤的id,但也本⾝只是被当做⼀个字符串名字去处理,xml解释器根据这个id去获取它对应的标准,从⽽知道这个命名空间定义有什么样的标签(xml解释器⾃带有⼀些通⽤的命名空间的标准),这个字符串虽然看起来是URL,但是和对应的⽹页上的信息没有关系,只是⽤来提供命名空间唯⼀性的作⽤,⽹址有时可以被打开,上⾯会有关于该命名空间的信息。
springboot配置文件里部分配置未生效的解决

springboot配置⽂件⾥部分配置未⽣效的解决springboot 配置⽂件⾥部分配置未⽣效最近⽤springboot搭了个项⽬,上线过段时间就会出现卡死,猜测是数据库连接池的连接被占满,⽤的连接池是druid,于是给项⽬加上了⼀个数据库连接池监控。
代码如下:@Configurationpublic class DruidConfiguration {/**** 注册⼀个StatViewServlet** @return**/@Beanpublic ServletRegistrationBean DruidStatViewServle2() {// org.springframework.boot.context.embedded.ServletRegistrationBean提供类的进⾏注册.ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");// 添加初始化参数:initParams// ⽩名单:// servletRegistrationBean.addInitParameter("allow", "127.0.0.1");// IP⿊名单 (存在共同时,deny优先于allow) : 如果满⾜deny的话提⽰:Sorry, you are not// permitted to view this page.// servletRegistrationBean.addInitParameter("deny", "192.168.1.73");// 登录查看信息的账号密码.servletRegistrationBean.addInitParameter("loginUsername", "admin");servletRegistrationBean.addInitParameter("loginPassword", "admin");// 是否能够重置数据.servletRegistrationBean.addInitParameter("resetEnable", "false");return servletRegistrationBean;}/**** 注册⼀个:filterRegistrationBean** @return**/@Beanpublic FilterRegistrationBean druidStatFilter2() {FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());// 添加过滤规则.filterRegistrationBean.addUrlPatterns("/*");// 添加不需要忽略的格式信息.filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid2/*");return filterRegistrationBean;}}于是重启项⽬,进⼊监控页⾯发现与配置⽂件⾥⾯的部分配置对应不上,当时也没在意,以为是显⽰的默认配置。
spring-mybatis.xml配置文件

spring-mybatis.xml<?xml version="1.0" encoding="UTF-8"?><beans xmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance" xmlns:p ="/schema/p"xmlns:context="/schema/context"xmlns:mvc="/schema/mvc"xsi:schemaLocation="/schema/bea ns/schema/beans/spri ng-beans-3.1.xsd/schema/context /schema/context/sp ring-context-3.1.xsd/schema/mvc/schema/mvc/spring -mvc-4.0.xsd"><!-- 自动扫描 --><context:component-scan base-package=".hnust" /><!-- 引入配置文件 --><bean id="propertyConfigurer"class="org.springframework.beans.factory.config.Prope rtyPlaceholderConfigurer"><property name="location" value="classpath:jdbc.prope rties" /></bean><bean id="dataSource" class="mons.dbcp.BasicDat aSource"destroy-method="close"><property name="driverClassName" value="${driver}" /><property name="url" value="${url}" /><property name="username" value="${username}" /><property name="password" value="${password}" /><!-- 初始化连接大小 --><property name="initialSize" value="${initialSize}">< /property><!-- 连接池最大数量 --><property name="maxActive" value="${maxActive}"></pro perty><!-- 连接池最大空闲 --><property name="maxIdle" value="${maxIdle}"></propert y><!-- 连接池最小空闲 --><property name="minIdle" value="${minIdle}"></propert y><!-- 获取连接最大等待时间 --><property name="maxWait" value="${maxWait}"></propert y></bean><!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSes sionFactoryBean"><property name="dataSource" ref="dataSource" /><!-- 自动扫描mapping.xml文件 --><property name="mapperLocations" value="classpath:com /cn/hnust/mapping/*.xml"></property></bean><!-- DAO接口所在包名,Spring会自动查找其下的类 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigure r"><property name="basePackage" value=".hnust.dao" /><property name="sqlSessionFactoryBeanName" value="sql SessionFactory"></property></bean><!-- (事务管理)transaction manager, use JtaTransactionManager f or global tx --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSource TransactionManager"><property name="dataSource" ref="dataSource" /> </bean></beans>。
Idea新建springboot工程,需要使用外部的配置文件

Idea新建springboot⼯程,需要使⽤外部的配置⽂件需求:使⽤Idea新建springboot⼯程,需要使⽤外部的配置⽂件,整体的⽬录结构如下:Spring Boot启动会扫描以下位置的application.properties或者application.yml⽂件作为spring boot的默认配置⽂件-file:/config/-file:./-classpath:/config/-classpath:/-以上是按照优先级从⾼到低的顺序,所有位置的⽂件都会被加载,⾼优先级的配置内容会覆盖低优先级配置内容。
-我们也可以通过配置spring.config.location来改变默认配置。
按照流⾏的说法,应该是可以加载的。
直接新建config⽂件夹,添加配置⽂件后好像不⾏。
解决⽅案:通过如下配置解决了IDEA⾥的运⾏问题:1、设置working directory到src⽂件夹2、设置config⽂件夹resources:设置后可以启动成功:打包问题打包不需要特殊配置,需要如下插件:<plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin>打包操作如下:打包后⽣成jar, 添加config⽂件夹下的配置⽂件启动:java -jar demo.jar 后报错如下:报错信息: no subdirectories found for mandatory directory location ‘file:./config/*/’.没明⽩为啥这样。
spring一些标签的作用contextloaderlistener和dispatcherservlet两者的配置文件的区别
spring一些标签的作用contextloaderlistener和dispatcherservlet两者的配置文件的区别本文将这样展开:1. 简单讲解Spring中的<context:component-scan /><context:annotation-config /><mvc:annotation-driven /> 这三者的作用2. 讲解下我们经常看到的在web.xml文件中定义的ContextLoaderListener和DispatcherServlet的区别<context:annotation-config />这个标签告诉Spring到bean类中寻找一些annotation定义的类,这些annotation基本如下:@Autowired @PostConstruct @PreDestroy @Resource 等。
需要注意的是。
这个标签并没有激活@Transactional 和@TransactionAttribute <context:component-scanbase-package=""/>这个标签用于告诉Spring搜索我们指定的包下面以及一些需要被自动注入的bean。
默认支持的annotation:@Component @Repository @Service @Controller需要注意的是:这个标签页实现了和annotation-config同样的效果。
<mvc:annotation-driven />刚开始的时候我也很困惑为什么需要配置这个标签,但是当我们在配置SpringMVC的控制器的时候,我们发现有RequestMapping的存在,这个标签的作用之一就是告诉Spring去检测RequestMapping。
springboot启动项目加载配置文件中的常量
springboot启动项⽬加载配置⽂件中的常量我们在开发中通常会遇到定义常量,但是如果写在java代码⾥不利于优化,于是这⾥我们将常量定义在配置⽂件⾥,步骤如下;1.在配置⽂件application.yml定义常量aliyun:oss:file:endpoint: keyid: LTAIsOB7X12kCHTGX81keysecret: ASy5lz2Mwr5KIVEUY3eDhFFi2jD1RkPCbucketname: eric.fang2.创建⼀个类实现InitializingBean接⼝,重写afterPropertiesSet()⽅法,加上@component注解,定义变量,使⽤@Value注解将值注⼊,然后定义常量⽅便访问,最后让常量赋值@Componentpublic class ConstantPropertiesUtil implements InitializingBean {@Value("${aliyun.oss.file.endpoint}")private String endpoint;@Value("${aliyun.oss.file.keyid}")private String keyid;@Value("${aliyun.oss.file.keysecret}")private String keysecret;@Value("${aliyun.oss.file.bucketname}")private String bucketname;//定义常量,为了能够使⽤public static String ENDPOINT;public static String KEYID;public static String KEYSECRET;public static String BUCKEYNAME;@Overridepublic void afterPropertiesSet() throws Exception {ENDPOINT=endpoint;KEYID=keyid;KEYSECRET=keysecret;BUCKEYNAME=bucketname;}}3.可以通过类直接进⾏调⽤列如:ConstantPropertiesUtil.ENDPOINT。
Springboot常用注解及配置文件加载顺序详解
Springboot常⽤注解及配置⽂件加载顺序详解Springboot常⽤注解及底层实现1、@SpringBootApplication:这个注解标识了⼀个SpringBoot⼯程,她实际上是另外三个注解的组合,分别是:@SpringBootConfiguration:源码可以看到,这个注解除了元注解外,实际就只有⼀个@Configuration,把该类变成⼀个配置类,表⽰启动类也是⼀个配置类;@EnableAutoConfiguration:是开启⾃动配置的功能,向Spring容器中导⼊了⼀个Selector,⽤来加载ClassPath下SpringFactories中所定义的⾃动配置类,将这些⾃动加载为配置Bean;由@AutoConfigurationPackage和@Import组成,前者表⽰让包中的类能够被⾃动扫描到spring容器中;使⽤import是往Spring容器中导⼊⼀个组件,将主配置类的所在包及⼦包所有组件扫描加载到Spring容器;Springboot在启动的时候,从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值,将这些值作为⾃动配置类导⼊到容器中,⾃动配置类就⽣效,帮我们进⾏⾃动配置⼯作。
以前需要我们⾃⼰配置的东西,⾃动配置类都帮我们完成了。
@ComponentScan:标识扫描路径,因为默认是没有配置实际扫描路径的,所以SpringBoot扫描的路径是启动类所在的当前⽬录;2、@Bean注解:⽤来定义Bean,类似于XML中的<bean>标签,Spring在启动时,会对加了@Bean注解的⽅法进⾏解析,将⽅法的名字作为beanName,并通过执⾏⽅法得到bean对象;3、@Controller、@Service、@ResponseBody、@AutowiredSpringboot中配置⽂件的加载顺序优先级从⾼到低,⾼优先级的配置覆盖低优先级的配置,所有配置会形成互补配置;1、命令⾏参数。
spring配置详解
spring配置详解1.前⾔公司⽼项⽬的后台,均是基于spring框架搭建,其中还⽤到了log4j.jar等开源架包。
在新项⽬中,则是spring和hibernate框架均有使⽤,利⽤了hibernate框架,来实现持久化,简化sql操作等。
Hibernate配置⽂件可以有两种格式,⼀种是 hibernate.properties,另⼀种是hibernate.cfg.xml。
后者稍微⽅便⼀些,当增加hbm映射⽂件的时候,可以直接在 hibernate.cfg.xml ⾥⾯增加,不必像 hibernate.properties 必须在初始化代码中加⼊。
我们新项⽬中使⽤的是hibernate.cfg.xml格式。
不过在本⽂中不将细述,后续有机会再补上。
公司项⽬中,中间件主要有tomcat,webshpere,WebLogic。
以下,将对项⽬中spring基本配置,log4j的配置,还有中间件的相关参数配置做⼀个初步的介绍。
2.spring配置——以⽼GIS项⽬为例⼦GISV13中的配置涉及到了SpringMVC,IOC,AOP, Quartz⽅⾯的配置。
配置的实现是通过注记配置和XML配置来合作实现。
这⾥,我将按照Spring的配置流程,将其他⼏个⽅⾯的配置融合其中,来进⾏全⾯解析。
2.1SpringMVC的配置2.1.1.web.xml的配置Web程序中,当中间件启动时,中间件会⾸先读取web.xml中的配置。
在web.xml中可以配置监听器,过滤器,servlet映射等等。
在Spring 框架中,我们主要需配置容器初始化时读取的spring容器配置⽂件的路径以及springMVC中的分发器DispatcherServlet。
在GISV13的web.xml中,我们定义了如下内容:InitGISConfigServlet定义了容器启动时,⾸先要运⾏这个⽅法。
然后servletname为MVC的这部分便是定义了springMVC的分发器以及此servlet所对应的加载配置⽂件的路径。