SpringBoot准备

学习内容源自b站

一、前导

IOC 反转控制 
简单说就是创建对象由以前的程序员自己new 构造方法来调用,变成了交由Spring创建对象
DI 依赖注入 
简单地说就是拿到的对象的属性,已经被注入好相关值了,直接使用即可

BeanFactory为IoC容器,而称ApplicationContext为应用上下文

1.通过xml文件将配置加载到IOC容器中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
     <!--若没写id,则默认为com.test.Man#0,#0为一个计数形式-->
    <bean id="man" class="com.test.Man"></bean>
</beans>

public class Test {
    public static void main(String[] args) {
        //加载项目中的spring配置文件到容器
        //ApplicationContext context = new ClassPathXmlApplicationContext("resouces/applicationContext.xml");
        //加载系统盘中的配置文件到容器
        ApplicationContext context = new FileSystemXmlApplicationContext("E:/Spring/applicationContext.xml");
        //从容器中获取对象实例
        Man man = context.getBean(Man.class);
        man.driveCar();
    }
}

2.通过java注解的方式将配置加载到IOC容器


@Configuration
public class ManConfig {
    @Bean
    public Man man() {
        return new Man(car());
    }
    @Bean
    public Car car() {
        return new QQCar();
    }
}

public class Test {
    public static void main(String[] args) {
        //从java注解的配置中加载配置到容器
        ApplicationContext context = new AnnotationConfigApplicationContext(ManConfig.class);
        //从容器中获取对象实例
        Man man = context.getBean(Man.class);
        man.driveCar();
    }
}

二、配置文件

1.环境


jdk1.8
maven3.x
IDEA2017
springboot1.5.9.release

在maven的settings.xml中添加

<profile>
          <id>jdk-1.8</id>
          <activation>
            <activeByDefault>true</activeByDefault>
            <jdk>1.8</jdk>
          </activation>
          <properties>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
                <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
          </properties>
</profile>

2.写一个功能:浏览器发送hello请求,服务器接收请求处理,相应helloworld字符串

 

idea的maven设置看how2j

创建一个maven工程(jar)
项目名:spring-boot-01-haluo

SpringBoot准备

@SpringBootApplication
public class SbootApplication {
    public static void main(String[] args) {
        SpringApplication.run(SbootApplication.class, args);
    }
}
@springbootapplication 标注一个主程序类,说明这是springboot

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello() {
        return "Hello SpringBoot.";
    }
}
@Controller 处理请求
@RequestMapping 接收请求

新建包 com.how2java.springboot.web, 然后在其下新建类HelloController
这个类就是Spring MVC里的一个普通的控制器
@RestController 是spring4里的新注解,是@ResponseBody和@Controller的缩写

3.简化部署
见图
双击package

SpringBoot准备

SpringBoot准备

SpringBoot准备

4.主程序类,主入口类
@SpringBootApplication:说明这个类是springboot的主类

进入SpringBootApplication
@java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE})
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@java.lang.annotation.Documented
@java.lang.annotation.Inherited
@org.springframework.boot.SpringBootConfiguration
@org.springframework.boot.autoconfigure.EnableAutoConfiguration

@SpringBootConfiguration:springboot的配置类

@Configuration:配置类上来标注这个注解
配置类----------配置文件;配置类也是容器的一个组件;@Component

5.配置文件

SpringBoot使用全局的配置文件

application.properties
application.yml 见图SpringBoot准备

 

配置文件注入

配置文件
person:
  lastName: zhangsan
  age: 18
  boss: false
  birth: 2017/12/12
  maps: {k1: v1, k2: 12}
  lists:
    - lisi
    - zhaoliu
  dog:
    name: 小狗
    age: 12

javabean:
/**
 * 将配置文件中配置的每一个属性的值,映射到这个组件中
 * @ConfigurationProperties: 告诉springboot将本类中所有属性和配置文件中的属性进行绑定
 *          prefix = "person":配置文件中哪个下面的所有属性进行一一映射

 *
 * 只有这个组件是容器中的组件,才能使用容器提供的@ConfigurationProperties功能
 */
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    private String lastName;
    private Integer age;
    private Boolean boss;
    private Date birth;

    private Map<String, Object> maps;
    private List<Object> lists;
    private Dog dog;

导入配置文件处理器
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
     </dependency>

properties配置省略

@ConfigurationProperties和@Value获取值的区别

SpringBoot准备

配置@PropertySource、@ImportResource、@Bean

@PropertySource:加载指定的配置文件

@ImportResource:导入Spring的配置文件,让配置文件里的内容生效
@ImportResource(locations = {"classpath:beans.xml"})
导入Spring的配置文件让其生效

SpringBoot推荐给容器中添加组件的方式

不编写配置文件了,使用全注解的方式

1.配置类=====Spring配置文件


@Configuration:指明类是配置类
@Bean:给容器添加组件

@Configuration
public class MyAppConfig {
    //将方法的返回值添加到容器中;容器中这个组件默认的id就是方法名
    @Bean
    public HelloService helloService() {
        return new HelloService();
    }
}

测试类:

@Autowired
ApplicationContext ioc;

@Test
public void testHelloService() {
    boolean b = ioc.containBean("helloService");
    syso(b);
}

Profile
Profile是Spring对不同环境提供不同配置功能的支持

1.多profile文件

2.yml支持多文档块方式

3.**指定profile

配置文件的加载位置和加载优先级

外部配置文件

三、日志

日志使用

1.默认配置

 Logger logger = LoggerFactory.getLogger(getClass());

    @Test
    void contextLoads() {

        //日志级别由低到高
       logger.trace("trace");
       logger.debug("debug");
       //springboot默认使用的是info级别
       logger.info("info");
       logger.warn("warn");
       logger.error("error");
    }