当前位置: 首页 > news >正文

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();}}
http://www.dtcms.com/a/438468.html

相关文章:

  • 厦门建站系统建设新年贺卡图片 手工制作
  • 怎么自己做视频网站制作网站协议
  • 从0死磕全栈之Next.js App Router动态路由详解:从入门到实战
  • RAG 入门全流程代码实战
  • Simulink模型转NuSMV模型(2006)
  • 内蒙古网站建设百度网站建设以什么盈利
  • 工业网站建设wordpress设置新浪邮箱
  • WSDL 文档:理解与使用
  • 网站建设丨金手指15设计师联盟官网效果图
  • Python3 MongoDB 使用指南
  • 沁恒微 RISC-V 芯片开发工具 MounRiver Studio 使用
  • IDEA快速上手指南!
  • 做网站用户充值提现吉林网站推广公司
  • 深圳建科技有限公司网站首页中国建筑业协会
  • [Windows] 利用AI写的一款自用多功能密码管理器开源
  • 4. 矩阵代数
  • 蛋白质的性质和研究方法
  • 【专业词典】PDCA
  • 云南网站推广大连集团网站建设
  • Sora 2 的社交野心:AI 如何重构内容社交产品逻辑?
  • 免费行情网站看男科一般花多少钱
  • 突破规模瓶颈的密钥:混合专家模型(MoE)的架构演进与应用
  • 厦门最早做网站的公司曲阳网站建设在哪
  • Spring Boot 的 7 大核心优势
  • GitLab入门教程:打开DevOps全流程的大门
  • 旅行社网站规划与建设的流程中国建设银行网站主要功能
  • 【408计组】3.2 主存储器的基本组成
  • 长沙门户网站建设公司徐典超 网站建设
  • 百度做任务的网站网站建设中古典武侠中文字幕
  • 【pytest】finalizer 执行顺序:FILO 原则