registration.addResourceLocations(this.resourceProperties.getStaticLocations());
if (this.servletContext != null) {
ServletContextResource resource = new ServletContextResource(this.servletContext, “/”);
registration.addResourceLocations(new Resource[]{resource});
}
});
}
}
先看这个:
if (!this.resourceProperties.isAddMappings()) {
logger.debug(“Default resource handling disabled”);
}
如果isAddMappings为false,就打印默认资源映射路径失效了。isAddMappings方法其实就是返回一个addMappings变量(在WebProperties中)
addMappings的含义就是运行访问静态资源,如果你设置成false,就是禁用所有静态资源映射。
站长在写这篇教程的时候,用的SpringBoot版本为2.5.1,是目前最新的版本。发现和之前的版本比起来,改动还是很大的。翻源码很麻烦,我就直接告诉你结论就行了。
默认的静态资源目录是:
new String[]{“classpath:/META-INF/resources/”, “classpath:/resources/”, “classpath:/static/”, “classpath:/public/”};
这点和以前是一样的,测试一下
访问什么路径能返回public.js呢?
在WebMvcProperties中,有一个staticPathPattern属性。
默认就是/**,所以,无论你访问什么,都可以被静态资源处理器接受。
启动项目,访问http://localhost:8888/public.js
成功访问,现在我们手动添加一个静态目录。
spring:
profiles: test
resources:
static-locations: [classpath:/my/]
访问http://localhost:8888/my.txt,也成功了。
最后,把静态资源开关关掉。
spring:
profiles: test
resources:
static-locations: [classpath:/my/]
add-mappings: false
这样就访问不到任何静态资源了,不过一般来说不用关。
再谈static-locations
static-locations表示自定义静态目录,如果配置了这个属性,默认的静态资源目录就会失效!
也可以用这个配置来访问本地资源
spring:
profiles: test
resources:
static-locations: file:C:/upload/article/
add-mappings: true
file:表示访问本地文件。
比如我现在要访问1.jpg
重启项目,访问 http://localhost:8888/20210415/1.jpg ,可以成功访问。
但是,其他静态资源就访问不了了。因为我们设置了static-locations,是一个覆盖操作,默认的目录都没了。
像访问这种本地文件的情况,一般是文件上传和下载的目录,这种的都是放在本地磁盘的,不会和项目绑定。
如果原来的目录我们也想要获取,又要访问本地文件该咋办呢?这个时候,就不要去配置static-locations,我们可以额外配置一个资源映射。
注释掉,正常情况下,用默认的资源映射绰绰有余了。难道那么多文件夹还不够你放资源吗。。。
(注意:上图中的resources也要注释掉,不然会报错)
然后,新建一个配置类。
package com.java18.vipmgr.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyWebResourcesHandler implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(“/upload/**”)
.addResourceLocations(“file:C:/upload/article/”);
}
}
重启,访问http://localhost:8888/upload/20210415/1.jpg就能看到图片了,并且也不会影响原来的资源目录。这才是项目中推荐的做法。
SpringBoot整合JDBC
================
添加jdbc依赖:
org.springframework.boot
spring-boot-starter-jdbc
mysql依赖本来就有:
mysql
mysql-connector-java
runtime
修改yml配置:
spring:
profiles: test
#resources:
#static-locations: file:C:/upload/article/
#add-mappings: true
#配置数据源
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/idea?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
username: root
password:
默认用的是mysql8.x的驱动包,url要注意加上时区,不然会报错的。
测试数据源是否加载成功:
@SpringBootTest
class DataTests {
@Autowired
DataSource dataSource;
@Test
void dataSource() {
System.out.println(dataSource.getClass());
}
}
打印:class com.zaxxer.hikari.HikariDataSource
这是springboot默认的数据源。
测试sql语句查询
@Test
void dataSource() {
//System.out.println(dataSource.getClass());
List<Map<String, Object>> users = jdbcTemplate.queryForList(“select * from users”);
System.out.println(users);
}
结果:[{id=1, username=zhangsan, password=123, points=1000}]
数据库:
举一反三:
1.为什么jdbcTemplate能直接用?
因为SpringBoot有自动配置,条件是:
@ConditionalOnClass({DataSource.class, JdbcTemplate.class})
@ConditionalOnSingleCandidate(DataSource.class)
DataSource已经配好了,JdbcTemplate因为引入了spring-boot-starter-jdbc,肯定也是有的。所以就直接自动配置了。
再看JdbcTemplateConfiguration:
SpringBoot整合Druid数据源
====================
总结
我们总是喜欢瞻仰大厂的大神们,但实际上大神也不过凡人,与菜鸟程序员相比,也就多花了几分心思,如果你再不努力,差距也只会越来越大。
面试题多多少少对于你接下来所要做的事肯定有点帮助,但我更希望你能透过面试题去总结自己的不足,以提高自己核心技术竞争力。每一次面试经历都是对你技术的扫盲,面试后的复盘总结效果是极好的!
https://img-blog.csdnimg.cn/img_convert/50ec3da53cd356a055ac370628147875.png)
再看JdbcTemplateConfiguration:
SpringBoot整合Druid数据源
====================
总结
我们总是喜欢瞻仰大厂的大神们,但实际上大神也不过凡人,与菜鸟程序员相比,也就多花了几分心思,如果你再不努力,差距也只会越来越大。
面试题多多少少对于你接下来所要做的事肯定有点帮助,但我更希望你能透过面试题去总结自己的不足,以提高自己核心技术竞争力。每一次面试经历都是对你技术的扫盲,面试后的复盘总结效果是极好的!
[外链图片转存中…(img-yewj0hHB-1714300166244)]