SpringBoot项目优先级以及bean的管理
SpringBoot项目优先级以及bean的管理
一、优先级
1.1优先级(由低到高)
application.yaml(忽略)
application.yamlapplication.properties
java 系统属性(-Dxxx=xxx)
命令行参数(–xxx=xxx)
1.2在运行jar包时运行
java -Dserver.port=9000 -jar springboot-web-config-0.0.1-SNAPSHOT.jar --server.port=100100
(前面指定的是属性,后面指定的是命令行参数)
二、Spring的bean对象的获取
2.1总述
首先需要获取到IOC容器对象ApplicationContext

2.2代码
package com.itheima;import com.itheima.controller.DeptController;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;@SpringBootTest
class SpringbootWebConfig2ApplicationTests {// 先是获取到IOC容器对象@Autowiredprivate ApplicationContext applicationContext;//获取bean对象@Testpublic void testGetBean(){
// 通过bean的名称来获取bean对象,若没有对bean对象命名,默认是类名首字母小写,当然要强转为对应的类型DeptController bean1 = (DeptController) applicationContext.getBean("deptController");System.out.println(bean1);// 依据bean的类型获取bean对象DeptController bean2 = applicationContext.getBean(DeptController.class);System.out.println(bean2);// 依据bean的名称和类来获取DeptController bean3 = applicationContext.getBean("deptController", DeptController.class);System.out.println(bean3);}//bean的作用域@Testpublic void testScope(){for (int i = 0; i < 10; i++) {DeptController deptController = applicationContext.getBean(DeptController.class);System.out.println(deptController);}}@Autowiredprivate SAXReader saxReader;//第三方bean的管理@Testpublic void testThirdBean() throws Exception {//SAXReader saxReader = new SAXReader();Document document = saxReader.read(this.getClass().getClassLoader().getResource("1.xml"));Element rootElement = document.getRootElement();String name = rootElement.element("name").getText();String age = rootElement.element("age").getText();System.out.println(name + " : " + age);}@Testpublic void testGetBean2(){Object saxReader = applicationContext.getBean("reader");System.out.println(saxReader);}
}
三、bean对象的作用域
3.1总述
Spring的IOC默认是启动时创建,且默认是singleton,即单例。并且可以通过@Lazy注释来延迟到第一次使用bean的时候创建IOC容器。

3.2代码
package com.itheima.controller;import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;
import java.util.List;//默认Spring的IOC会在启动的时候创建并且实例化,延迟实例化到第一次使用的时候
@Lazy
//指定bean的作用域是多例的,即每使用一次bean就会创建一个bean
@Scope("prototype")
@RestController
@RequestMapping("/depts")
public class DeptController {@Autowiredprivate DeptService deptService;
//声明无参构造这样就知道什么时候创建的Controllerpublic DeptController(){System.out.println("DeptController constructor ....");}@GetMappingpublic Result list(){List<Dept> deptList = deptService.list();return Result.success(deptList);}@DeleteMapping("/{id}")public Result delete(@PathVariable Integer id) {deptService.delete(id);return Result.success();}@PostMappingpublic Result save(@RequestBody Dept dept){deptService.save(dept);return Result.success();}
}
四、第三方bean
4.1 总述

4.2代码
package com.itheima.config;import com.itheima.service.DeptService;
import org.dom4j.io.SAXReader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//加上@Configuration注解, 让当前类成为配置类
@Configuration //配置类
public class CommonConfig {//声明第三方bean@Bean //将当前方法的返回值对象交给IOC容器管理, 成为IOC容器bean//通过@Bean注解的name/value属性指定bean名称, 如果未指定, 默认是方法名
// 第三方bean:在bean对象的参数列表写入完成注入public SAXReader saxReader(DeptService deptService){System.out.println(deptService);return new SAXReader();}}