织梦网站联系我们的地图怎么做,北京效果好的网站推广,网站做315认证,免费购物系统目录 一、ClassPathXmlApplicationContext1.1 说明1.2 代码示例1.3 截图示例 二、FileSystemXmlApplicationContext2.1 说明2.2 代码示例2.3 加载xml的bean定义示例 三、AnnotationConfigApplicationContext3.1 说明3.2 代码示例3.3 截图示例 四、AnnotationConfigServletWebSe… 目录 一、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 version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean idcard classcom.learning.application_context.Card/bean idstudent classcom.learning.application_context.Studentproperty namecard refcard/property/bean
/beans4. 测试类
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;Configuration
public 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;Configuration
public 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;Configuration
public 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 截图示例