Spring 容器

Spring 容器【生产和管理bean】

Spring 容器_第1张图片

BeanFactory【Spring容器的顶层接口】

public interface BeanFactory {
	String FACTORY_BEAN_PREFIX = "&";

	Object getBean(String name) throws BeansException;
	Object getBean(String name, Object... args) throws BeansException;
	<T> T getBean(String name, Class<T> requiredType) throws BeansException;
	<T> T getBean(Class<T> requiredType) throws BeansException;
	<T> T getBean(Class<T> requiredType, Object... args) throws BeansException;

	<T> ObjectProvider<T> getBeanProvider(Class<T> requiredType);
	<T> ObjectProvider<T> getBeanProvider(ResolvableType requiredType);
	
	String[] getAliases(String name);
    @Nullable
	Class<?> getType(String name) throws NoSuchBeanDefinitionException;
	@Nullable
	Class<?> getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException;
	
	boolean containsBean(String name);
	boolean isSingleton(String name) throws NoSuchBeanDefinitionException;
	boolean isPrototype(String name) throws NoSuchBeanDefinitionException;
	boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException;
	boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException;
}

HierarchicalBeanFactory
ListableBeanFactory

ApplicationContext
ListableBeanFactory, HierarchicalBeanFactory
EnvironmentCapable, MessageSource, ResourcePatternResolver, ApplicationEventPublisher,

AutowireCapableBeanFactory

启动 Spring 容器


<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="demo1Service" class="com.example.demo.demo1.Demo1ServiceImpl"/>
    <bean id="demo1Service2" class="com.example.demo.demo1.Demo1ServiceImpl2"/>
beans>

FileSystemXmlApplicationContext

Spring 容器_第2张图片

//在当前路径下加载配置文件
ApplicationContext context = newFileSystemXmlApplicationContext("demo1.xml");
//在当前路径下加载 demo1.xml、demo2.xml、demo3.xml 配置文件
String[] locations = {"demo1.xml", "demo1.xml", "demo1.xml"};
ApplicationContext context = new FileSystemXmlApplicationContext(locations );

//在classpath路径下加载配置文件
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:demo1.xml");

//在 D:/project/demo1.xml 下配置文件
ApplicationContext context = new FileSystemXmlApplicationContext("D:/project/demo1.xml");

ClassPathXmlApplicationContext

Spring 容器_第3张图片

//默认在 ClassPath 中寻找 xml 配置文件,根据 xml 文件内容来构建 ApplicationContext
ApplicationContext context = new ClassPathXmlApplicationContext("demo1.xml");

//在 绝对路径 中寻找 xml 配置文件,根据 xml 文件内容来构建 ApplicationContext
ApplicationContext context = new ClassPathXmlApplicationContext("file:D:/project/demo1.xml");

AnnotationConfigApplicationContext

Spring 容器_第4张图片
实现了Bean定义的注册、Bean的实例化、事件、生命周期、国际化等功能

获取容器中的 Bean 实例

Demo1Service demo1Service = context.getBean("demo1Service", Demo1Service.class);
demo1Service.demo1();

Demo1Service demo1Service2 = context.getBean("demo1Service2", Demo1Service.class);
demo1Service2.demo1();

Bean 的生命周期

你可能感兴趣的:(Spring,Java,spring,java,后端)