这篇文章主要介绍“ApplicationContext继承接口功能及与BeanFactory区别是什么”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“ApplicationContext继承接口功能及与BeanFactory区别是什么”文章能帮助大家解决问题。
BeanFactory是Spring中最基本的接口,它是Spring IoC容器中最底层的接口,提供了IoC容器最基本的形式,它具有最基本的 IoC 功能,负责管理 Spring Bean 的生命周期,通过 BeanFactory 可以获取指定 Bean 的实例。 ApplicationContext是BeanFactory的子接口,ApplicationContext继承了BeanFactory接口的全部功能,同时还提供了其他的一些功能。ApplicationContext是Spring中最重要的接口之一,它是Spring容器的具体实现,在BeanFactory的基础上添加了一些更加实用的功能,比如资源加载、事件发布、AOP和事务等。 因此,可以认为ApplicationContext继承了BeanFactory,拥有了更多的功能和扩展性。
ResourceLoader:加载资源文件
MessageSource:国际化消息的源头
ApplicationEventPublisher:应用事件发布
EnvironmentCapable:获取当前应用的环境信息
ListableBeanFactory:提供了批量操作Bean的方法
HierarchicalBeanFactory:层次性的BeanFactory,支持子容器
AutowireCapableBeanFactory:Bean自动装配的BeanFactory
BeanDefinitionRegistry:定义和注册BeanDefinition的接口
ConfigurableApplicationContext:ApplicationContext可配置的接口,对外提供了修改bean定义、激活环境、注册shut-down hook等能力。
public class TestBean {
private String name;
public TestBean() {
System.out.println("constructor of TestBean is invoked");
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public class BeanFactoryExample {
public static void main(String[] args) {
Resource resource = new ClassPathResource("spring-config.xml");
BeanFactory factory = new XmlBeanFactory(resource);
TestBean testBean = (TestBean) factory.getBean("testBean");
System.out.println("The name is : " + testBean.getName());
}
}
其中,ClassPathResource是一个用于从classpath中加载资源文件的类。XmlBeanFactory是Spring提供的一个实现了BeanFactory接口的IoC容器实例。在实例化XmlBeanFactory时,需要提供一个Resource对象,这里传入的是spring-config.xml文件。
public class ResourceLoaderExample {
public static void main(String[] args) throws IOException {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
ResourceLoader loader = context;
Resource resource = loader.getResource("classpath:test.txt");
InputStream inputStream = resource.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
}
}
这段示例代码通过ApplicationContext
实例化了一个ResourceLoader
对象,然后通过getResource
方法加载了"classpath:test.txt"
文件。这个文件在classpath路径下,所以可以使用 classpath:
前缀来定位文件。
首先,需要在Spring配置文件中定义一个资源文件:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages" />
</bean>
然后,在Java代码中使用注入方式获取 MessageSource
实例:
public class MessageSourceExample {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
MessageSource messageSource = (MessageSource) context.getBean("messageSource");
String message = messageSource.getMessage("test", null, Locale.getDefault());
System.out.println("The message is : " + message);
}
}
这个示例代码中,我们通过 messageSource.getMessage("test", null, Locale.getDefault())
方法根据当前默认的语言获取了 messages.properties
中 test
属性的值。
我们可以在启动时发布一个事件:
public class MyApplicationEvent extends ApplicationEvent {
public MyApplicationEvent(Object source) {
super(source);
}
}
public class PublishEventExample {
public static void main(String[] args) {
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
context.addApplicationListener(new ApplicationListener<MyApplicationEvent>() {
@Override
public void onApplicationEvent(MyApplicationEvent event) {
System.out.println("MyApplicationEvent received!");
}
});
context.publishEvent(new MyApplicationEvent("Hello World!"));
context.close();
}
}
当程序成功运行时,我们可以看到输出了 MyApplicationEvent received!
的信息。
这个接口用于获取当前运行的环境信息:
public class EnvironmentExample {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
Environment environment = context.getEnvironment();
System.out.println("The environment is : " + environment.getProperty("os.name"));
}
}
这个接口中提供了一些批量操作Bean的方法:
public class ListableBeanFactoryExample {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
ListableBeanFactory beanFactory = context.getBeanFactory();
String[] beanNames = beanFactory.getBeanDefinitionNames();
for (String beanName : beanNames) {
System.out.println(beanName);
}
}
}
这个接口用于完成Bean的自动装配:
public class AutowireCapableBeanFactoryExample {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
AutowireCapableBeanFactory beanFactory = context.getAutowireCapableBeanFactory();
TestBean testBean = new TestBean();
beanFactory.autowireBean(testBean);
System.out.println("The name is : " + testBean.getName());
}
}
这个接口用于注册Bean:
public class BeanDefinitionRegistryExample {
public static void main(String[] args) {
XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("spring-config.xml"));
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
BeanDefinition beanDefinition = new RootBeanDefinition(TestBean.class);
registry.registerBeanDefinition("testBean", beanDefinition);
TestBean testBean = (TestBean) beanFactory.getBean("testBean");
System.out.println("The name is : " + testBean.getName());
}
}
这个示例代码中,我们通过 BeanDefinitionRegistry
接口实现了向容器注册 TestBean
的过程。
这个接口用于修改bean定义、激活环境、注册shut-down hook等能力:
public class ConfigurableApplicationContextExample {
public static void main(String[] args) {
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
ConfigurableEnvironment environment = context.getEnvironment();
environment.setActiveProfiles("dev");
context.refresh();
TestBean testBean = (TestBean) context.getBean("testBean");
System.out.println("The name is : " + testBean.getName());
}
}
在这个示例代码中,我们通过 ConfigurableApplicationContext
接口修改了当前的运行环境,激活了dev配置文件并重新刷新了容器,然后获取了 testBean
的实例并输出它的名称。
关于“ApplicationContext继承接口功能及与BeanFactory区别是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://juejin.cn/post/7224772830779637817