Springboot - 4.ApplicationContext

✍1. ApplicationContext:

当在Spring Boot中使用ApplicationContext时,您可以遵循以下详细示例来了解每个概念和用法,同时考虑了Spring Boot的自动配置和便利性:

🎷1. ApplicationContext接口:

ApplicationContext是Spring IoC容器的更高级别接口,除了BeanFactory的功能外,还提供了更多的功能,如国际化、事件传播等。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class MyComponent {

    private final ApplicationContext applicationContext;

    @Autowired
    public MyComponent(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    public void useBean() {
        MyService myService = applicationContext.getBean(MyService.class);
        myService.doSomething();
    }
}

🎷2. 默认实现:

在Spring Boot中,默认的ApplicationContext实现是AnnotationConfigApplicationContext,用于基于注解的配置。

🎷3. Bean的生命周期:

ApplicationContext也负责管理Bean的生命周期,与BeanFactory相同。

🎷4. Bean的获取:

使用getBean()方法从容器中获取Bean,与BeanFactory相同。

MyService myService = applicationContext.getBean(MyService.class);

🎷5. 延迟初始化:

默认情况下,ApplicationContext同样支持延迟初始化,只有在需要时才创建Bean实例。

🎷6. 扩展和自定义:

您可以实现BeanPostProcessor接口等来自定义Bean的创建和初始化过程,与BeanFactory相同。

🎷7. 注解配置:

使用注解定义Bean。

import org.springframework.stereotype.Component;

@Component
public class MyService {
    // Bean implementation
}

🎷8. 注解扫描:

使用注解扫描自动注册标记了@Component的Bean,与BeanFactory相同。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
        MyService myService = context.getBean(MyService.class);
        myService.doSomething();
    }
}

🎷9. 自动装配:

使用@Autowired注解进行自动装配,与BeanFactory相同。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MyController {

    private final MyService myService;

    @Autowired
    public MyController(MyService myService) {
        this.myService = myService;
    }

    // Controller logic
}

通过这些详细示例,您可以更深入地理解在Spring Boot中如何使用ApplicationContext来管理和使用Bean,同时利用Spring Boot的自动配置和便利性。ApplicationContext提供了更多的功能,如事件传播和国际化,使得应用程序更加灵活和功能丰富。

✍2. 如何使用ApplicationContext:

在Spring Boot中,ApplicationContext是一个中央接口,它提供了应用程序运行时的配置信息。ApplicationContextBeanFactory的子接口,它添加了更多的企业级功能,比如从一个属性文件中解析文本信息。

🎷1. 注入ApplicationContext:

  • 我们可以通过实现ApplicationContextAware接口或者直接在Bean中注入ApplicationContext来使用它。

    @Component
    public class ExampleBean implements ApplicationContextAware {
    
        private ApplicationContext applicationContext;
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            this.applicationContext = applicationContext;
        }
    
        public void doSomething() {
            // 使用applicationContext
            AnotherBean anotherBean = (AnotherBean) applicationContext.getBean(AnotherBean.class);
            anotherBean.doSomething();
        }
    
    }
    
    @Component
    public class AnotherBean {
    
        public void doSomething() {
            System.out.println("AnotherBean doSomething 方法被调用");
        }
    
    }
    

    在这个示例中,ExampleBean实现了ApplicationContextAware接口,这样Spring容器会自动注入ApplicationContext。然后,在doSomething方法中,我们使用applicationContext获取AnotherBean的实例,并调用它的doSomething方法。

🎷2. 使用@Autowired注解:

  • 我们可以使用@Autowired注解直接在Bean中注入ApplicationContext

    @Component
    public class ExampleBean {
    
        @Autowired
        private ApplicationContext applicationContext;
    
        public void doSomething() {
            // 使用applicationContext
            AnotherBean anotherBean = (AnotherBean) applicationContext.getBean(AnotherBean.class);
            anotherBean.doSomething();
        }
    
    }
    
    @Component
    public class AnotherBean {
    
        public void doSomething() {
            System.out.println("AnotherBean doSomething 方法被调用");
        }
    
    }
    

    在这个示例中,我们使用@Autowired注解直接在ExampleBean中注入ApplicationContext。然后,在doSomething方法中,我们使用applicationContext获取AnotherBean的实例,并调用它的doSomething方法。

🎷3. 使用ApplicationListener接口:

  • 我们可以实现ApplicationListener接口来监听应用程序的事件。

    @Component
    public class ExampleBean implements ApplicationListener<ContextRefreshedEvent> {
    
        @Override
        public void onApplicationEvent(ContextRefreshedEvent event) {
            System.out.println("应用程序上下文刷新");
        }
    
    }
    

    在这个示例中,ExampleBean实现了ApplicationListener接口,这样它会监听ContextRefreshedEvent事件。当应用程序上下文刷新时,onApplicationEvent方法会被调用。

✍3. BeanFactory和ApplicationContext:

在Spring Framework中,BeanFactory和ApplicationContext是用来管理和提供应用程序组件(即Java对象或"bean")的容器。它们在Spring框架中扮演着不同的角色,并适用于不同的使用场景。

🎷1. BeanFactory:

  • 作用:BeanFactory是Spring的IoC(控制反转)容器的基本形式,负责创建、管理和提供bean实例,从而实现了将对象的创建和配置与应用程序代码解耦的原则。
  • 特点:BeanFactory是轻量级容器,只在请求时才会实例化bean,从而实现了延迟加载,这在性能方面具有优势。它提供了基本的IoC功能,但在功能上相对较少。
  • 示例:在移动设备或资源受限的嵌入式系统中,使用BeanFactory可以降低内存和资源消耗,只有在需要时才实例化bean,如下所示:
    BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
    MyBean myBean = beanFactory.getBean(MyBean.class);
    

🎷2. ApplicationContext:

  • 作用:ApplicationContext是BeanFactory的更高级扩展,提供了更多的功能和特性,如国际化、事件传播、AOP(面向切面编程)集成等。
  • 特点:ApplicationContext在容器初始化时就实例化并配置所有bean,因此在容器启动时就完成了所有bean的创建。它是功能更强大、更全面的容器。
  • 示例:在Web应用程序中,使用ApplicationContext可以方便地管理国际化资源、处理事件和应用AOP,如下所示:
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    MessageSource messageSource = applicationContext.getBean(MessageSource.class);
    

🎷3. 总结:

  • 如果应用程序对性能和资源消耗有严格要求,可以选择使用BeanFactory来实现延迟加载和轻量级容器。例如,在嵌入式系统中。
  • 如果应用程序需要更多功能,如国际化、事件传播、AOP等,或者需要在启动时立即实例化所有bean,应选择使用ApplicationContext。例如,在Web应用程序或企业级应用中。

在实际开发中,许多应用程序会倾向于使用ApplicationContext,因为它提供了更多的便利和功能。但根据不同的需求和环境,选择合适的容器类型可以更好地满足应用程序的需求。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yueerba126

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值