【spring】ApplicationContext的实现

目录

        • 一、ClassPathXmlApplicationContext
          • 1.1 说明
          • 1.2 代码示例
          • 1.3 截图示例
        • 二、FileSystemXmlApplicationContext
          • 2.1 说明
          • 2.2 代码示例
          • 2.3 加载xml的bean定义示例
        • 三、AnnotationConfigApplicationContext
          • 3.1 说明
          • 3.2 代码示例
          • 3.3 截图示例
        • 四、AnnotationConfigServletWebServerApplicationContext
          • 4.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配置



	

	
		
	

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

【spring】ApplicationContext的实现_第1张图片

二、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());
	}

【spring】ApplicationContext的实现_第2张图片

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);
		}
	}
}

【spring】ApplicationContext的实现_第3张图片

三、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的方式是用来添加的
3.2 代码示例
package com.learning.application_context;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Config {
	@Bean
	public Student student(Card card){
		Student student = new Student();
		student.setCard(card);
		return student;
	}

	@Bean
	public 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 截图示例

【spring】ApplicationContext的实现_第4张图片

四、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;

@Configuration
public class WebConfig {
    // 定义一个tomcat服务工厂
    @Bean
    public ServletWebServerFactory servletWebServerFactory(){
        return new TomcatServletWebServerFactory();
    }

    @Bean
    public DispatcherServlet dispatcherServlet(){
        return new DispatcherServlet();
    }

    @Bean
    public DispatcherServletRegistrationBean registrationBean(DispatcherServlet dispatcherServlet){
        return new DispatcherServletRegistrationBean(dispatcherServlet, "/");
    }

    @Bean("/hello")
    public Controller controllerOne(){
        return new Controller() {
            @Override
            public 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;

@Configuration
public class WebConfig {
    // 定义一个tomcat服务工厂
    @Bean
    public ServletWebServerFactory servletWebServerFactory(){
        return new TomcatServletWebServerFactory();
    }

    @Bean
    public DispatcherServlet dispatcherServlet(){
        return new DispatcherServlet();
    }

    @Bean
    public DispatcherServletRegistrationBean registrationBean(DispatcherServlet dispatcherServlet){
        return new DispatcherServletRegistrationBean(dispatcherServlet, "/");
    }

    @Bean("/hello")
    public Controller controllerOne(){
        return new Controller() {
            @Override
            public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
                httpServletResponse.getWriter().print("hello");
                return null;
            }
        };
    }
}

4.2 截图示例

【spring】ApplicationContext的实现_第5张图片
【spring】ApplicationContext的实现_第6张图片

你可能感兴趣的:(spring,spring)