Spring容器BeanFactory和ApplicationContext怎么使用

发布时间:2023-05-05 15:49:08 作者:iii
来源:亿速云 阅读:114

这篇“Spring容器BeanFactory和ApplicationContext怎么使用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Spring容器BeanFactory和ApplicationContext怎么使用”文章吧。

将BeanFactory和ApplicationContext作为容器使用

在Spring中,BeanFactory和ApplicationContext是容器的两种实现方式,可以使用它们来管理对象的生命周期以及实现依赖注入等功能。

BeanFactory容器

BeanFactory是Spring容器中最基本的接口,它提供了定义和管理bean的基本方式。使用BeanFactory容器的步骤如下:

1.定义一个Bean对象

public class HelloBean { 
   private String message; 
   public void setMessage(String message) { 
      this.message = message; 
   } 
   public void sayHello() { 
      System.out.println("Hello " + message); 
   } 
}

2.在Spring的配置文件中声明一个bean对象

<beans> 
    <bean id="helloBean" class="com.example.HelloBean"> 
        <property name="message" value="World" /> 
    </bean>
</beans>

3.使用BeanFactory容器获取bean对象

BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("beans.xml")); 
HelloBean helloBean = (HelloBean) beanFactory.getBean("helloBean"); 
helloBean.sayHello();

ApplicationContext容器

ApplicationContext是BeanFactory的子接口,它是Spring容器的另一种实现方式。和BeanFactory容器相比,ApplicationContext容器提供了更加丰富的功能,例如国际化支持、事件发布、资源管理等。使用ApplicationContext容器的步骤如下: 1.定义一个Bean对象

public class HelloBean { 
    private String message; 
    public void setMessage(String message) { 
       this.message = message; 
    } 
    public void sayHello() { 
       System.out.println("Hello " + message); 
    } 
}

2.在Spring的配置文件中声明一个bean对象

<beans> 
    <bean id="helloBean" class="com.example.HelloBean"> 
        <property name="message" value="World" />
    </bean>
</beans>

3.使用ApplicationContext容器获取bean对象

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); 
HelloBean helloBean = (HelloBean) applicationContext.getBean("helloBean"); 
helloBean.sayHello();

Spring内嵌Web容器的过程

在Spring中,ApplicationContext容器有多个子类,其中包括了用于WEB开发的子类。这些子类可以直接启动Tomcat、Jetty等Web容器,使得我们可以在不使用第三方Web容器的情况下,直接在Spring应用内启动Web应用。下面以SpringBoot的Web容器启动为例,来说明ApplicationContext子类的具体源代码实现过程。 我们先来看一下SpringBoot的启动类:

@SpringBootApplication 
public class Application { 
    public static void main(String[] args) { 
       SpringApplication.run(Application.class, args); 
    } 
}

在这个启动类中,我们使用了SpringBoot提供的@SpringBootApplication注解。这个注解是一个组合注解,包含了多个其他注解,其中一个注解就是@Import({ ServletWebServerFactoryAutoConfiguration.class, WebMvcAutoConfiguration.class })。这个注解的作用是向Spring容器中注册两个自动配置类:ServletWebServerFactoryAutoConfiguration和WebMvcAutoConfiguration。

接着我们来看一下ServletWebServerFactoryAutoConfiguration这个类:

@Configuration(proxyBeanMethods = false)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass(Servlet.class)
@ConditionalOnMissingBean(value = { ServletWebServerFactory.class, WebServerFactoryCustomizerBeanPostProcessor.class })
public class ServletWebServerFactoryAutoConfiguration {
    @Bean
    public ServletWebServerFactoryCustomizer servletWebServerFactoryCustomizer(
            ObjectProvider<WebServerFactoryCustomizerBeanPostProcessor> webServerFactoryCustomizerBeanPostProcessor) {
        return new ServletWebServerFactoryCustomizer(
                webServerFactoryCustomizerBeanPostProcessor.getIfAvailable(Collections::emptyList));
    }
    @Bean
    @ConditionalOnMissingBean
    public TomcatServletWebServerFactory tomcatServletWebServerFactory(
            ObjectProvider<TomcatConnectorCustomizer> connectorCustomizers,
            ObjectProvider<TomcatContextCustomizer> contextCustomizers, Environment environment) {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
        factory.setEnvironment(environment);
        factory.setTomcatConnectorCustomizers(connectorCustomizers.orderedStream().collect(Collectors.toList()));
        factory.setTomcatContextCustomizers(contextCustomizers.orderedStream().collect(Collectors.toList()));
        return factory;
    }
}

可以看到,这个类标记为@Configuration注解,表示它是一个配置类。这个类提供了TomcatServletWebServerFactory这个bean定义,它利用Tomcat作为Web容器。这个类还提供了一个servletWebServerFactoryCustomizer的bean定义,它会在Web容器启动前对Web容器进行自定义的配置。

除了ServletWebServerFactoryAutoConfiguration之外,还有一些其他的自动配置类。例如:

在SpringBoot启动时,会将这些自动配置类都加载到Spring容器中。最终,我们就可以使用ApplicationContext容器来获取Web容器实例,代码如下:

@Bean
public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
    int port = environment.getProperty("server.port", Integer.class, 8080);
    return factory -> {
        factory.setPort(port);
    };
}
@Bean
public ServletWebServerFactory servletContainer() {
    return applicationContext.getBean(ServletWebServerFactory.class);
}

可以看到,这个代码中直接调用了ApplicationContext容器的getBean方法,来获取ServletWebServerFactory这个bean实例。

以上就是关于“Spring容器BeanFactory和ApplicationContext怎么使用”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。

推荐阅读:
  1. Spring JPA之find拓展方法有哪些
  2. Spring JPA find分页方法怎么使用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring beanfactory applicationcontext

上一篇:oracle逻辑备份exp导出指定表名时需要加括号问题怎么解决

下一篇:el-tree树怎么设置懒加载及设置默认勾选

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》