欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

  1. @Configuration
  2. @ComponentScan
  3. @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

Spring Boot每天10问--day01
其中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