springboot配置

复习:
配置SSM
MAVEN ==》 依赖(一堆的)
web.xml (servlet, filter, listener...)
spring-mvc.xml
spring-mybatis.xml
mapper....
缺点:
忒麻烦(配置文件多)
容易出错
花费得时间长
如果从0开始配置一个 hello world(url)ssm项目
一, springboot
最重要得2个特点
1,自动配置spring
自动管理bean,自动扫描,自动注入(要在被注入得成
员变量上面追加@Autowire)
2,零配置
没有xml配置文件,全部采用注解的方式
二, springboot的启动类

点击查看代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Springboot01Application {

    public static void main(String[] args) {
        SpringApplication.run(Springboot01Application.class, args);
    }

}
1, @SpringBootApplication: 是srpingboot的核心注 解, java程序的入口是main方法 一个springboot项目, 只有一个加了 【@SpringbootApplication】注解的main方法, 这个类也就是springboot的唯一的入口类 2, springboot会自动扫描并管理类(bean), 自动扫描 的路径是: 启动类(也就是加了@SpringbootApplicaiton注解 的类)所在的同级包, 以及下级包里面所有的bean(加了注解的类(controller、service、conmponent、repository个注 解中的任意一个))

三, springboot的配置
1, springboot支持的2中格式的配置形式
① 传统的application.properties
② yaml格式的配置(文件的后缀名称 .yml)
2,配置文件中的属性值的读取(2种)
① 注入的方式
在成员变量上面追加注解@Value("${xxxxx}")

application.properties配置文件
#tomcat????
server.port=8888
#???path
server.servlet.context-path=/class009

#mysql?????
mysql.driver=com.mysql.cj.jdbc.Driver
mysql.url=jdbc:mysql://192.168.0.41:3306/class009_xiaobai?characterEncoding=utf8
mysql.username=root
mysql.password=123456a?
读取配置文件代码
package com.bh.controller;

import com.bh.config.DBConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
public class HelloController {
    /*注入的方式读取配置文件*/
    @Value("${mysql.driver}")
    private String driver;
    @Value("${mysql.url}")
    private String url;
    @Value("${mysql.username}")
    private String username;
    @Value("${mysql.password}")
    private String password;
    @Autowired
    private DBConfig dbConfig;
    @Autowired
    private Date date;
    @RequestMapping("/hello")
    public @ResponseBody  String hello(){
        //System.out.println(password);

        //System.out.println(dbConfig.getUsername());

        System.out.println(date);
        return "sssss";
    }
}

② 单独的POJO类 在类名上面追加 @Component (被spring容器管理) @ConfigurationProperties(prefix = "mysql") 注意: 成员变量的 变量名,要给配置文件种的key 要 一模一样(包括大小写)
新建pojo类DBConfig
package com.bh.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/*配置文件的读取*/
@Component
@ConfigurationProperties(prefix = "mysql")
public class DBConfig {
    private String driver;
    private String url;
    private String username;
    private String password;

    public String getDriver() {
        return driver;
    }

    public void setDriver(String driver) {
        this.driver = driver;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

读取配置文件
package com.bh.controller;

import com.bh.config.DBConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
public class HelloController {
    /*读取配置文件*/;
    @Autowired
    private DBConfig dbConfig;
    @Autowired
    private Date date;
    @RequestMapping("/hello")
    public @ResponseBody  String hello(){
       
        System.out.println(dbConfig.getUsername());

        System.out.println(date);
        return "sssss";
    }
}

四,springboot读取外部的配置文件(springboot除了两个传统的配置文件都属于外部配置文件,例如spring.xml)

1,在springboot的启动类上面追加
@ImportResource("classpath:spring.xml")

2, 可以同时加载多个配置配件
@ImportResource(locations =
{"xxx1.xml","xxx2.xml"})
or
@ImportResource("classpath:spring.xml")
五, springboot的java配置方式(不想被springboot的启动类自动管理)
1,新建一个类
2, 在这个类的上面追加 @Configuration 注解
3, 在这个类的具体的方法上面 增加 @Bean 注解
4, springboot 推荐使用这种方式

SpringConfig
package com.bh.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Date;

@Configuration
public class SpringConfig {
    @Bean
    public Date getDate(){
        Date date = new Date();
        return date;

    }
}
对象注入
package com.bh.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
public class HelloController {
    @Autowired
    private Date date;
    @RequestMapping("/hello")
    public @ResponseBody  String hello(){
        System.out.println(date);
        return "sssss";
    }
}

posted @ 2023-06-27 19:53  liangkuan  阅读(35)  评论(0)    收藏  举报