java yml定义list_Java框架之SpringBoot 09-Web构建-yml-模块-注解

SpringBoot

Spring Boot是一站式整合所有应用框架的框架,简化Spring应用开发,约定大于配置,去繁从简,开箱即用,准生产环境的运行时应用监控框架

快速构建 SpringBoot 应用必须联网创建,使用 Eclipse 或 Idea,选择好 SpringBoot 版本及 Spring Starter 模块即可

1) 自动生成主程序类,用于启动项目 2) 自动生成静态资源目录及属性配置文件 3) 自动增加pom.xml相关依赖配置

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.1.12.RELEASE

cn.gcaca

hello

0.0.1-SNAPSHOT

1.8

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-maven-plugin

在static文件夹下存放静态资源文件

application.properties 或 application.yml 配置文件名是固定的,定义配置加载属性文件规则

YAML基本语法

l  使用缩进表示层级关系

l  缩进时不允许使用Tab键,只允许使用空格。

l  缩进的空格数目不重要,只要相同层级的元素左侧对齐即可

l  大小写敏感

字面量:普通的值(数字,字符串,布尔)

l  k: v:字面值直接书写即可;字符串默认不用加上单引号或者双引号;

l  " ":双引号;不会转义字符串里面的特殊字符;特殊字符会作为本身想表示的意思 例 : name: "zhangsan n lisi":输出;zhangsan 换行 lisi

l  ' ':单引号;会转义特殊字符,特殊字符最终只是一个普通的字符串数据 例 : name: 'zhangsan n lisi':输出;zhangsan n lisi

对象、Map(属性和值)(键值对)

l  k: v:通过缩进在下一行来写对象的属性和值表示其关系;注意缩进

l  对象还是k: v的方式

l  行内写法  friends: {lastName: zhangsan,age: 18}

friends:

lastName: zhangsan

age:20

数组(List、Set)

l  行内写法  pets: [cat,dog,pig]

用- 值表示数组中的一个元素

pets:

‐ cat

‐ dog

自动配置原理

1)、SpringBoot 帮我们配好了所有的应用场景

2)、SpringBoot 中会有很多的 xxxxAutoConfigurarion(帮我们给容器中自动装配好组件)

3)、xxxxAutoConfigurarion 给容器中配组件的时候,组件默认的属性一般都是从 xxxProperties 中获取这些属性的值

4)、xxxProperties 是和配置文件绑定的(属性一 一对应)

5)、如默认值不符合我们的需要只需改掉这些默认配置即可;

6)、如果默认的组件我们不用;还可以自定义组件。

SpringBoot 的一个最大策略:自定义组件用自己的,否则,使用默认的。

自定义配置类

@SpringBootConfigurationpublic classAppConfig {

@BeanpublicInternalResourceViewResolver internalResourceViewResolver(){

InternalResourceViewResolver resolver= newInternalResourceViewResolver();

resolver.setPrefix("/templates/");

resolver.setSuffix(".html");returnresolver;

}//补充:获取ApplicationContext 对象

public static voidmain(String[] args) {

ApplicationContext context= new AnnotationConfigApplicationContext(AppConfig.class);

context.getBean("myBean");

}

}

SpringBoot 构建 WEB 应用项目

构建 web,jdbc,mybatis,mysql,redis,thymeleaf 等相关组件

增加 Druid 数据源

com.alibaba

druid

1.1.12

application.yml 配置相关属性

spring:

datasource:

username: root

password: 12345

url: jdbc:mysql://localhost:3306/scw?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai

driver-class-name: com.mysql.cj.jdbc.Driver

mybatis:

config-location: classpath:/mybatis/mybatis-config.xml

mapper-locations: classpath:/mybatis/mapper/*.xml

主程序配置类

//主程序扫描加载 mapper

@MapperScan("cn.gc.dao")//集成事务管理,在Service接口中增加@Transactional

@EnableTransactionManagement//整合Web组件,在配置类增加@WebServlet @WebFilter @WebListener

@ServletComponentScan

@SpringBootApplicationpublic classSpringMybatisApplication {public static voidmain(String[] args) {

SpringApplication.run(SptingMybatisApplication.class, args);

}

}

Druid 配置类及监控后台管理

@SpringBootConfigurationpublic classDruidConfig {//引入yml文件配置属性

@Bean

@ConfigurationProperties(prefix= "spring.datasource")public DataSource dataSource() throwsSQLException {

DruidDataSource dataSource= newDruidDataSource();

dataSource.setFilters("stat");//配置监控统计拦截的filters

returndataSource;

}//配置Druid的监控 1、配置一个管理后台的Servlet

@BeanpublicServletRegistrationBean statViewServlet() {

ServletRegistrationBean bean= new ServletRegistrationBean(new StatViewServlet(), "/druid/*");

Map initParams = new HashMap<>();

initParams.put("loginUsername", "admin");

initParams.put("loginPassword", "12345");

initParams.put("allow", "");//默认就是允许所有访问

initParams.put("deny", "");//拒绝哪个ip访问

bean.setInitParameters(initParams);returnbean;

}//2、配置一个web监控的filter

@BeanpublicFilterRegistrationBean webStatFilter() {

FilterRegistrationBean bean= newFilterRegistrationBean();

bean.setFilter(newWebStatFilter());

Map initParams = new HashMap<>();

initParams.put("exclusions", "*.js,*.css,/druid/*");//排除过滤

bean.setInitParameters(initParams);

bean.setUrlPatterns(Arrays.asList("/*"));returnbean;

}

}

SpringBoot 相关模块

3858e36c162a1d53ced597ecfeaee328.png

007c62b38de287b56e147ee2541b6391.png

86d0224cb42c61d911b1f03d8dadc143.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值