Spring Boot每天10问--day01
程序员文章站
2022-04-15 18:24:01
1.spring boot中哪个注解负责开启组件扫描和自动配置?它由于那几个注解组成@SpringBootApplication@Configuration@ComponentScan@EnableAutoConfiguration2.spring boot中自定义配置可以放在哪些地方配置?1.单独的@Configuration标注的类里;2.可以加入到引导类中3.spring boot中什么用来继承版本号spring-boot-starter-parent继承版本号4.spring...
1.spring boot中哪个注解负责开启组件扫描和自动配置?它由于那几个注解组成
@SpringBootApplication
- @Configuration
- @ComponentScan
- @EnableAutoConfiguration
2.spring boot中自定义配置可以放在哪些地方配置?
1.单独的@Configuration标注的类里;
2.可以加入到引导类中
3.spring boot中什么用来继承版本号
spring-boot-starter-parent继承版本号
4.spring boot中起步依赖的名字有什么规则?
spring-boot-starter-前缀
5.如何排除传递依赖?
使用exclusion标签
org.springframework.boot
spring-boot-starter-web
com.fasterxml.jackson.databind
com.fasterxml.jackson.core
6.如何引入某个版本中的某个依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.3</version>
</dependency>
7. application.properties和application.yml文件能放在哪些位置?
1)外置:相对于应用程序运行目录的/config子目录里;
2)外置:在应用程序运行的目录里
3)内置:在config包内
4)内置:在Classpath根目录
优先级别由高到低,同一级别时,application.yml里的属性覆盖application.properties里的属性。
8.如何在controller中注入配置文件里的属性
在一个bean中加载配置属性
@Component
@ConfigurationProperties("amazon")
public class AmazonProperties {
private String associatedId;
public String getAssociatedId() {
return associatedId;
}
public void setAssociatedId(String associatedId) {
this.associatedId = associatedId;
}
}
在controller中对属性进行初始化
@Controller
@RequestMapping("/readingList")
public class BookListController {
private BookService bookService;
private AmazonProperties associatedId;
@Autowired
public BookListController(BookService bookService,AmazonProperties associatedId){
this.bookService=bookService;
this.associatedId=associatedId;
}
9.如何实现环境的配置切换
application.yml文件内容如下:
spring:
profiles:
active: test
其中application-test.yml中的配置如下如所示:
server:
port: 9082
spring:
datasource:
master:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://xxxxxxxx:3306/xxxxxx?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true&allowMultiQueries=TRUE
username: xxxxxx
password: xxxxxxx
thymeleaf:
cache: true
prefix: classpath:/templates/
suffix: .html
check-template-location: true
encoding: utf-8
mode: HTML
resources:
static-locations: classpath:/static/
amazon:
associateId: habuma-20
10.如何创建安全配置
在maven中引入配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
显示安全配置如下:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
ReaderService readerService;
@Override
protected void configure(HttpSecurity http) throws Exception{
http
.authorizeRequests()
.antMatchers("/")
.access("hasRole('READER')")
.and()
.formLogin()
.loginPage("/login")
.failureUrl("/login?error=true");
}
protected void configure(AuthenticationManagerBuilder auth) throws Exception{
auth.userDetailsService(new UserDetailsService() {
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
return readerService.findOneByUsername(username);
}
});
}
}
本文地址:https://blog.csdn.net/a1773570500/article/details/109591531
上一篇: 清明时节吃什么水果好 吃这些最好
下一篇: 《2020/03/30》java整理
推荐阅读
-
spring boot 文档学习笔记day01
-
Spring Boot 的 10 个核心模块
-
2021-08-10spring boot启动报错Error starting ApplicationContext(application和前端问题)
-
Spring Boot每天10问--day01
-
Spring Boot(10)——使用Redis
-
Spring Boot微服务项目实战(第2版)学习笔记-第10章集成MyBatis
-
现有web系统替换成Spring Boot2框架 之10 定时任务Quartz Scheduler spring bootmaven定时任务quartz
-
Spring Boot每天10问--day01
-
Spring Boot 的 10 个核心模块
-
spring boot 文档学习笔记day01