如何高效总结获取Spring上下文的方法?

2026-05-26 08:331阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计652个文字,预计阅读时间需要3分钟。

如何高效总结获取Spring上下文的方法?

一、前言+计算重温Spring,后续可能会每周发一篇,有空就搞搞;

二、获取上下文的几种方式+AnnotationConfigApplicationContext:从一个或多个基于Java的配置类中加载Spring应用上下文。

一 前言

打算重温spring,以后可能每周会发一篇吧,有空就搞搞;

二 获取上下文的几种方式

如何高效总结获取Spring上下文的方法?

  • AnnotationConfigApplicationContext:从一个或多个基于Java的配置类中加载Spring应用上下文。
  • AnnotationConfigWebApplicationContext:从一个或多个基于Java的配置类中加载Spring Web应用上下文。
  • ClassPathXmlApplicationContext:从类路径下的一个或多个XML配置文件中加载上下文定义。
  • FileSystemXmlapplicationcontext:从文件系统下的一个或多个XML配置文件中加载上下文定义。
  • XmlWebApplicationContext:从Web应用下的一个或多个XML配置文件中加载上下文定义

2.1 准备工作

被单实体

public class Sheet { // 颜色 private String color; // 长度 private String length; // 省略 set get }

sheet.xml 里面注入了Bean Sheet, 并且默认初始化 color值为red;

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="sheet" class="com.zszxz.bean.Sheet"> <property name="color" value="pink"></property> </bean> </beans>

2.2FileSystemXmlapplicationcontext 获取上下文

FileSystemXmlApplicationContext 构造器参数中需要指定sheet.xml具体文件系统路径;获得上下文之后再通过getBean方法获取Bean Sheet; 拿到对象后使用getColor 方法打印颜色,为pink;

public static void main(String[] args) { // xml路径 String path = "C:\\java\\workspaceforresource\\study-spring\\obtain-bean-way\\src\\main\\resources\\sheet.xml"; // 从文件系统中获取上下文 ApplicationContext applicationContext = new FileSystemXmlApplicationContext(path); // 获取bean Sheet sheet = (Sheet) applicationContext.getBean("sheet"); // pink System.out.println(sheet.getColor()); }

2.3ClassPathXmlApplicationContext获取上下文

ClassPathXmlApplicationContext 传入参数是类路径下sheet.xml的路径;

public static void main(String[] args) { // 获取上下文 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("sheet.xml"); // 获得实例 Sheet sheet = (Sheet) applicationContext.getBean("sheet"); // pink System.out.println(sheet.getColor()); }

2.4AnnotationConfigApplicationContext获取上下文

AnnotationConfigApplicationContext 获取上下文,是通过java配置的方式获取上下文;知识追寻者这边需要进行java配置,内容如下,等同于之前的sheet.xml

/** * @Author lsc * <p> sheet配置类等同于sheet.xml</p> */ @Configuration public class SeetConfig { // 往配置类中注入Bean @Bean public Sheet sheet(){ // 创建对象 Sheet sheet = new Sheet(); // 设置属性 sheet.setColor("pink"); return sheet; } }

获取方式如下,传入AnnotationConfigApplicationContext 参数是SeetConfig.class

public static void main(String[] args) { // 获取上下文 ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SeetConfig.class); // 获得实例 Sheet sheet = (Sheet) applicationContext.getBean("sheet"); // pink System.out.println(sheet.getColor()); }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计652个文字,预计阅读时间需要3分钟。

如何高效总结获取Spring上下文的方法?

一、前言+计算重温Spring,后续可能会每周发一篇,有空就搞搞;

二、获取上下文的几种方式+AnnotationConfigApplicationContext:从一个或多个基于Java的配置类中加载Spring应用上下文。

一 前言

打算重温spring,以后可能每周会发一篇吧,有空就搞搞;

二 获取上下文的几种方式

如何高效总结获取Spring上下文的方法?

  • AnnotationConfigApplicationContext:从一个或多个基于Java的配置类中加载Spring应用上下文。
  • AnnotationConfigWebApplicationContext:从一个或多个基于Java的配置类中加载Spring Web应用上下文。
  • ClassPathXmlApplicationContext:从类路径下的一个或多个XML配置文件中加载上下文定义。
  • FileSystemXmlapplicationcontext:从文件系统下的一个或多个XML配置文件中加载上下文定义。
  • XmlWebApplicationContext:从Web应用下的一个或多个XML配置文件中加载上下文定义

2.1 准备工作

被单实体

public class Sheet { // 颜色 private String color; // 长度 private String length; // 省略 set get }

sheet.xml 里面注入了Bean Sheet, 并且默认初始化 color值为red;

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="sheet" class="com.zszxz.bean.Sheet"> <property name="color" value="pink"></property> </bean> </beans>

2.2FileSystemXmlapplicationcontext 获取上下文

FileSystemXmlApplicationContext 构造器参数中需要指定sheet.xml具体文件系统路径;获得上下文之后再通过getBean方法获取Bean Sheet; 拿到对象后使用getColor 方法打印颜色,为pink;

public static void main(String[] args) { // xml路径 String path = "C:\\java\\workspaceforresource\\study-spring\\obtain-bean-way\\src\\main\\resources\\sheet.xml"; // 从文件系统中获取上下文 ApplicationContext applicationContext = new FileSystemXmlApplicationContext(path); // 获取bean Sheet sheet = (Sheet) applicationContext.getBean("sheet"); // pink System.out.println(sheet.getColor()); }

2.3ClassPathXmlApplicationContext获取上下文

ClassPathXmlApplicationContext 传入参数是类路径下sheet.xml的路径;

public static void main(String[] args) { // 获取上下文 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("sheet.xml"); // 获得实例 Sheet sheet = (Sheet) applicationContext.getBean("sheet"); // pink System.out.println(sheet.getColor()); }

2.4AnnotationConfigApplicationContext获取上下文

AnnotationConfigApplicationContext 获取上下文,是通过java配置的方式获取上下文;知识追寻者这边需要进行java配置,内容如下,等同于之前的sheet.xml

/** * @Author lsc * <p> sheet配置类等同于sheet.xml</p> */ @Configuration public class SeetConfig { // 往配置类中注入Bean @Bean public Sheet sheet(){ // 创建对象 Sheet sheet = new Sheet(); // 设置属性 sheet.setColor("pink"); return sheet; } }

获取方式如下,传入AnnotationConfigApplicationContext 参数是SeetConfig.class

public static void main(String[] args) { // 获取上下文 ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SeetConfig.class); // 获得实例 Sheet sheet = (Sheet) applicationContext.getBean("sheet"); // pink System.out.println(sheet.getColor()); }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。