【spring】ApplicationContext的实现,日月神卡
cpugpu芯片开发光刻机
半导体技术突破
4
文件名:【spring】ApplicationContext的实现,日月神卡
【spring】ApplicationContext的实现
目录 一、ClassPathXmlApplicationContext1.1 说明1.2 代码示例1.3 截图示例 二、FileSystemXmlApplicationContext2.1 说明2.2 代码示例2.3 加载xml的bean定义示例 三、AnnotationConfigApplicationContext3.1 说明3.2 代码示例3.3 截图示例 四、AnnotationConfigServletWebServerApplicationContext4.1 说明4.2 代码示例4.2 截图示例 一、ClassPathXmlApplicationContext 1.1 说明 1. 较为经典的容器,基于classpath下xml格式的配置文件来创建 2. 在类路径下读取xml文件 3. 现在已经很少用了,做个了解 1.2 代码示例 1. 学生类 package com.learning.application_context;public class Student {private Card card;public void setCard(Card card){this.card = card;}public Card getCard(){return this.card;}} 2. 卡类 package com.learning.application_context;public class Card {} 3. xml配置 <?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"><bean id="card" class="com.learning.application_context.Card"/><bean id="student" class="com.learning.application_context.Student"><property name="card" ref="card"></property></bean></beans> 4. 测试类 package com.learning.application_context;import org.springframework.context.support.ClassPathXmlApplicationContext;public class ApplicationContextTest {public static void main(String[] args) {testClassPathXmlApplicationContext();}private static void testClassPathXmlApplicationContext(){ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("class-path-xml-application-context.xml");for (String beanDefinitionName : classPathXmlApplicationContext.getBeanDefinitionNames()) {System.out.println(beanDefinitionName);}System.out.println(classPathXmlApplicationContext.getBean(Student.class).getCard());}} 1.3 截图示例 二、FileSystemXmlApplicationContext 2.1 说明 1. 基于读取文件系统的xml来创建 2.2 代码示例 package com.learning.application_context;import org.springframework.context.support.FileSystemXmlApplicationContext;public class ApplicationContextTest {public static void main(String[] args) {testFileSystemXmlApplicationContext();}private static void testFileSystemXmlApplicationContext(){// 具体配置的路径按实际情况来写FileSystemXmlApplicationContext fileSystemXmlApplicationContext = new FileSystemXmlApplicationContext("D:\\IdeaProject\\spring-framework\\spring-learning\\src\\main\\resources\\class-path-xml-application-context.xml");for (String beanDefinitionName : fileSystemXmlApplicationContext.getBeanDefinitionNames()) {System.out.println(beanDefinitionName);}System.out.println(fileSystemXmlApplicationContext.getBean(Student.class).getCard());}} private static void testFileSystemXmlApplicationContext(){// 绝对目录//FileSystemXmlApplicationContext fileSystemXmlApplicationContext = new FileSystemXmlApplicationContext("D:\\IdeaProject\\spring-framework\\spring-learning\\src\\main\\resources\\class-path-xml-application-context.xml");// 相对目录,但要设置工作目录FileSystemXmlApplicationContext fileSystemXmlApplicationContext = new FileSystemXmlApplicationContext("src\\main\\resources\\class-path-xml-application-context.xml");for (String beanDefinitionName : fileSystemXmlApplicationContext.getBeanDefinitionNames()) {System.out.println(beanDefinitionName);}System.out.println(fileSystemXmlApplicationContext.getBean(Student.class).getCard());} 2.3 加载xml的bean定义示例 package com.learning.application_context;import org.springframework.beans.factory.support.DefaultListableBeanFactory;import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;import org.springframework.core.io.ClassPathResource;public class ApplicationContextTest {public static void main(String[] args) {loadXmlBeanDefinitionTest();}private static void loadXmlBeanDefinitionTest(){DefaultListableBeanFactory defaultListableBeanFactory = new DefaultListableBeanFactory();System.out.println("读取前的BeanDefinitionName====");for (String beanDefinitionName : defaultListableBeanFactory.getBeanDefinitionNames()) {System.out.println(beanDefinitionName);}XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(defaultListableBeanFactory);xmlBeanDefinitionReader.loadBeanDefinitions(new ClassPathResource("class-path-xml-application-context.xml"));System.out.println("读取后的BeanDefinitionName====");for (String beanDefinitionName : defaultListableBeanFactory.getBeanDefinitionNames()) {System.out.println(beanDefinitionName);}}} 三、AnnotationConfigApplicationContext 3.1 说明 1. 较为经典的容器,基于java配置类来创建 2. 会默认添加几个后处理器org.springframework.context.annotation.internalConfigurationAnnotationProcessor org.springframework.context.annotation.internalAutowiredAnnotationProcessor org.springframework.context.annotation.internalCommonAnnotationProcessor org.springframework.context.event.internalEventListenerProcessor org.springframework.context.event.internalEventListenerFactory 3. 用xml的方式是用<context:annotation-config/>来添加的 3.2 代码示例 package com.learning.application_context;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class Config {@Beanpublic Student student(Card card){Student student = new Student();student.setCard(card);return student;}@Beanpublic Card card(){return new Card();}} package com.learning.application_context;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class ApplicationContextTest {public static void main(String[] args) {testAnnotationConfigApplicationContext();}private static void testAnnotationConfigApplicationContext(){AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(Config.class);for (String beanDefinitionName : annotationConfigApplicationContext.getBeanDefinitionNames()) {System.out.println(beanDefinitionName);}}} 3.3 截图示例 四、AnnotationConfigServletWebServerApplicationContext 4.1 说明 1. 可以加载tomcat服务来工作2. 需要加载springboot相关的jar包 4.2 代码示例 package com.learning.application_context;import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletRegistrationBean;import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;import org.springframework.boot.web.servlet.server.ServletWebServerFactory;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.DispatcherServlet;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@Configurationpublic class WebConfig {// 定义一个tomcat服务工厂@Beanpublic ServletWebServerFactory servletWebServerFactory(){return new TomcatServletWebServerFactory();}@Beanpublic DispatcherServlet dispatcherServlet(){return new DispatcherServlet();}@Beanpublic DispatcherServletRegistrationBean registrationBean(DispatcherServlet dispatcherServlet){return new DispatcherServletRegistrationBean(dispatcherServlet, "/");}@Bean("/hello")public Controller controllerOne(){return new Controller() {@Overridepublic ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {httpServletResponse.getWriter().print("hello");return null;}};}} package com.learning.application_context;import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletRegistrationBean;import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;import org.springframework.boot.web.servlet.server.ServletWebServerFactory;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.DispatcherServlet;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@Configurationpublic class WebConfig {// 定义一个tomcat服务工厂@Beanpublic ServletWebServerFactory servletWebServerFactory(){return new TomcatServletWebServerFactory();}@Beanpublic DispatcherServlet dispatcherServlet(){return new DispatcherServlet();}@Beanpublic DispatcherServletRegistrationBean registrationBean(DispatcherServlet dispatcherServlet){return new DispatcherServletRegistrationBean(dispatcherServlet, "/");}@Bean("/hello")public Controller controllerOne(){return new Controller() {@Overridepublic ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {httpServletResponse.getWriter().print("hello");return null;}};}} 4.2 截图示例
同类推荐
-

【QT】Ubuntu 编译安装 QT 5.12.7 源码,我国6g研发正式启动
查看 -

【QT】信号和槽(15),卓越亚马逊图书
查看 -

【QT】容器类的迭代,东芝as100(东芝kt as10)
查看 -

【Qt】QML-04:自定义变量(属性)property,transphone(qt 自定义属性)
查看 -

【Qt一坑】qt编译出现“常量中有换行符”,geak魔戒
查看 -

【Qt之元对象系统】,夏新大v进步版(qt 元对象系统)
查看 -

【ROS】Nav2源码之nav2_behavior_tree详解,tcl s850
查看 -

【RabbitMQ(day3)】扇形交换机和主题交换机的应用,索爱w610c
查看 -

【React-Router】路由导航,微星780g主板(微星7808主板)
查看