springboot篇】二. springboot配置详解

基于【springboot篇】一. idea搭建springboot项目

SpringBoot配置

1 创建Bean

1.1 定义Bean

package com.wpj.bean;

import lombok.*;

/** * ClassName: User * Description: * * @author JieKaMi * @version 1.0 * @date: 2020\1\16 0016 19:00 * @since JDK 1.8 */
@Data
public class User {

    private Integer id;

    private String username;

    private String password;

}

1.2 定义WebConfig

package com.wpj.config;

import com.wpj.bean.*;
import org.springframework.context.annotation.*;

/** * ClassName: WebConfig * Description: * * @author JieKaMi * @version 1.0 * @date: 2020\1\16 0016 18:59 * @since JDK 1.8 */
@Configuration // 表示这个配置相关的类
public class WebConfig {

    // springboot会调用添加@Bean这个函数,会把方法返回值放入spring容器中
    // bean的id为方法名
    @Bean
    public User user(){
        User user = new User();
        user.setId(1);
        user.setUsername("jieKaMi");
        user.setPassword("123456");
        return user;
    }
}

1.3 Test

package com.wpj.config;

import com.wpj.bean.*;
import org.springframework.context.annotation.*;

/** * ClassName: WebConfig * Description: * * @author JieKaMi * @version 1.0 * @date: 2020\1\16 0016 18:59 * @since JDK 1.8 */
@Configuration // 表示这个配置相关的类
public class WebConfig {

    // springboot会调用添加@Bean这个函数,会把方法返回值放入spring容器中
    // bean的id为方法名
    @Bean
    public User user(){
        User user = new User();
        user.setId(1);
        user.setUsername("jieKaMi");
        user.setPassword("123456");
        return user;
    }
}

2. 创建web组件(servlet,filter,interceptor)

2.1 初始化filter

2.2 这是原来通过web.xml初始化Filter的方式

<filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>com.wpj.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

2.3 通过springboot初始化Filter

2.3.1 定义Filter

package com.wpj.filter;

import javax.servlet.*;
import java.io.*;

/** * ClassName: EncodingFilter * Description: * * @author JieKaMi * @version 1.0 * @date: 2020\1\16 0016 19:20 * @since JDK 1.8 */
public class EncodingFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // 输出方便查看是否初始化成功
        System.out.println("encoding: " + filterConfig.getInitParameter("encoding"));
    }
    
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("EncodingFilter.doFilter");
        filterChain.doFilter(servletRequest,servletResponse);
    }
}

2.3.2 WebConfig定义方法返回FilterRegistrationBean

	@Bean
    public FilterRegistrationBean filterRegistrationBean(){
    
        FilterRegistrationBean encodingFilter = new FilterRegistrationBean();

        encodingFilter.setFilter(new EncodingFilter());
        encodingFilter.setName("EncodingFilter");
        encodingFilter.addUrlPatterns("/*");
        encodingFilter.addInitParameter("encoding","utf-8");

        return encodingFilter;
    }

2.3.3 测试

运行主启动类
springboot篇】二. springboot配置详解插图

2.3.4 容器启动时初始化Filter成功。。。。

3. SpringBoot配置文件( 属性文件)

  1. 给上面User添加几个属性并通过属性文件给User的属性赋值

    package com.wpj.bean;
    
    import lombok.*;
    import org.springframework.boot.context.properties.*;
    import org.springframework.format.annotation.*;
    import org.springframework.stereotype.*;
    
    import java.util.*;
    
    /** * ClassName: User * Description: * * @author JieKaMi * @version 1.0 * @date: 2020\1\16 0016 19:00 * @since JDK 1.8 */
    @Data
    @ConfigurationProperties(prefix = "user")
    @Component
    public class User {
    
        private Integer id;
        private String username;
        private String password;
    
        private List<String> email;
        private String[] loves;
        private Set<String> phones;
        private Map<String, String> maps;
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        private Date birthday;
    }
    
    
  2. 将上面WebConfig的user方法注释掉(避免后面通过属性文件配置User报错)springboot篇】二. springboot配置详解插图(1)

3.1 application.properties(少用)

user.id=10
user.username=admin
user.password=123456

# 接受集合的写法
user.emails[0]=wpj@qq.com
user.emails[1]=jiekami@qq.com
user.emails[2]=jieKaMi@qq.com

# 接受数组
user.loves[0]=Spring
user.loves[1]=SpringBoot
user.loves[2]=MyBatis-plus

# 接受set
user.phones[0]=1587
user.phones[1]=xxx
user.phones[2]=0279

# 接受map
user.maps[k1]=v1
user.maps[k2]=v2
user.maps[k3]=v3

# 时间
user.birthday=2020-01-16

springboot篇】二. springboot配置详解插图(2)
也可以通过注解取值

jdbc.username=root
	@Value("${jdbc.username}")
	private String username;

springboot篇】二. springboot配置详解插图(3)

3.2 application.yml (常用)

  1. 用application.yml替代application.properties,但又不想删除application.properties,可以将application.propertiesg改名为application.properties_,这样不用删除也不会加载到这个配置文件
  2. yml有严格的格式 分号: 后面要空格 tab一下代表一个级别
user:
  id: 10
  username: qfAdmin
  password: 456
  emails:
    - qfAdmin@1000phome.com
    - qfAdmin@163.com
    - qfAdmin@qq.com
  loves:
    - Spring
    - MyBatis
    - SpringBoot
  phones:
    - 123
    - 333
    - 444
  maps:
    k1: v1
    k2: v2
    k3: v3
  birthday: 2020-01-16

springboot篇】二. springboot配置详解插图(4)

3.2.1 引入依赖使yml带有提示

<!-- 添加这个依赖在yml中写自定义对象属性的时候有提示-->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-configuration-processor</artifactId>
	<optional>true</optional>
</dependency>

3.3 读取属性

读取属性用的最多的还是以下方式

@Value("${jdbc.username}")
private String username;
没有账号? 忘记密码?

社交账号快速登录