当前位置:首页 >> 半导体技术突破 >> 【spring】ApplicationContext的实现,日月神卡

【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 截图示例

协助本站SEO优化一下,谢谢!
关键词不能为空
同类推荐
«    2025年12月    »
1234567
891011121314
15161718192021
22232425262728
293031
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
文章归档
网站收藏
友情链接