10 个 Spring 相关的的面试题

1.什么是 IoC 容器?

2.解释下 Spring Bean 的生命周期

IoC 容器是 Spring Bean 居住的地方,Spring Bean 仅仅需要  pojo,从下面的几个步骤来分析 Bean 的生命周期:

寻找 Bean Definition (如 bean.xml)=> set 其 property(包含 

    @Autowired(required = false)
    public void setMovieFinder(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }

    @Autowired
    public void setMovieFinder(Optional<MovieFinder> movieFinder) {
        ...
    }

) => setBeanName (包含 Primary 与 Qualifier 注解)=> 使用 JSR-250 @PostConstruct and @PreDestroy 代替 @Bean(initMethod = "init") 和 @Bean(destroyMethod = "cleanup")

3.什么是 BeanFactroy ?与  ApplicationContext 区别 (见 点击

将 Bean 配置文件包装为 实际的代码

BeanFactory factory = new XmlBeanFactory(new FileInputStream("beans.xml"));

ApplicationContext 包含 BeanFactroy 所有功能,如果没有特殊需求一般使用 BeanFactory。功能对比:

Feature BeanFactory ApplicationContext

Bean instantiation/wiring

Yes

Yes

Integrated lifecycle management

No

Yes

Automatic BeanPostProcessorregistration

No

Yes

Automatic BeanFactoryPostProcessorregistration

No

Yes

Convenient MessageSource access (for internalization)

No

Yes

Built-in ApplicationEvent publication mechanism

No

Yes

233

猜你喜欢

转载自www.cnblogs.com/lemos/p/9609675.html