手机微网站价,兰州市做网站建设的公司,wordpress主题demo,济南专业网站建设一、Spring面试题
1、Bean的生命周期
1.调用bean的构造方法创建Bean
2.通过反射调用Set方法设 bean的属性
3.如果Bean 实现了实现了BeanNameAware接口#xff0c;Spring将调用setBeanName()#xff0c;设置 Bean的name
4.如果Bean实现了BeanFactoryAware接口#xff0c…一、Spring面试题
1、Bean的生命周期
1.调用bean的构造方法创建Bean
2.通过反射调用Set方法设 bean的属性
3.如果Bean 实现了实现了BeanNameAware接口Spring将调用setBeanName()设置 Bean的name
4.如果Bean实现了BeanFactoryAware接口Spring将调用setBeanFactory()把bean factory设置给Bean
5.如果存在 BeanPostProcessor接口Spring 将 调用它们的 postProcessBeforeInitialization预初始化方法在Bean初始化前对其进行处理
6.如果Bean 实现InitializingBean接口spring将调用它的afterPropertiesSet方法然后调用xml定义的init-method 方法两个方法作用类似都是初始化bean的时候执行
7.如果存在beanPostProcessor,Spring将调用postProcessAfterInitialization(后初始化)方法在Bean初始化后对其进行处理
8.1 如果Bean为单例的话那么容器会返回Bean给用户并存入缓存池。如果Bean实现了DisposableBean接口Spring将调用它的destory方法然后调用在xml中定义的 destory-method方法这两个方法作用类似都是在Bean实例销毁前执行。
8.2 如果Bean是多例的话容器将Bean返回给用户剩下的生命周期由用户控制。
2.BeanFactory和FactoryBean的区别
1.BeanFacotry:管理Bean的容器Spring中生成的Bean都是由这个接口的实现来管理的
2.FactoryBean: 通常是用来创建比较复杂的bean一般的bean 直接用xml配置即可但如果一个bean的创建过程中涉及到很多其他的bean 和复杂的逻辑直接用xml配置比较麻烦这时可以考虑用FactoryBean可以隐藏实例化复杂Bean的细节
3.BeanFactory和ApplicationContext有什么区别
BeanFactory和ApplicationContext是Spring的两大核心接口都可以当做Spring的容器。其中ApplicationContext是BeanFactory的子接口。
两者区别如下
1、功能上的区别。BeanFactory是Spring里面最底层的接口包含了各种Bean的定义读取bean配置文档管理bean的加载、实例化控制bean的生命周期维护bean之间的依赖关系。
ApplicationContext接口作为BeanFactory的派生除了提供BeanFactory所具有的功能外还提供了更完整的框架功能如继承MessageSource、支持国际化、统一的资源文件访问方式、同时加载多个配置文件等功能。
2、加载方式的区别。BeanFactroy采用的是延迟加载形式来注入Bean的即只有在使用到某个Bean时(调用getBean())才对该Bean进行加载实例化。这样我们就不能发现一些存在的Spring的配置问题。如果Bean的某一个属性没有注入BeanFacotry加载后直至第一次使用调用getBean方法才会抛出异常。
而ApplicationContext是在容器启动时一次性创建了所有的Bean。这样在容器启动时我们就可以发现Spring中存在的配置错误这样有利于检查所依赖属性是否注入。 ApplicationContext启动后预载入所有的单例Bean那么在需要的时候不需要等待创建bean因为它们已经创建好了。
相对于基本的BeanFactoryApplicationContext 唯一的不足是占用内存空间。当应用程序配置Bean较多时程序启动较慢。
3、创建方式的区别。BeanFactory通常以编程的方式被创建ApplicationContext还能以声明的方式创建如使用ContextLoader。
4、注册方式的区别。BeanFactory和ApplicationContext都支持BeanPostProcessor、BeanFactoryPostProcessor的使用但两者之间的区别是BeanFactory需要手动注册而ApplicationContext则是自动注册。
4.Bean注入容器有哪些方式
1、Configuration Bean
Configuration用来声明一个配置类然后使用 Bean 注解用于声明一个bean将其加入到Spring容器中。
Configuration
public class MyConfiguration {Beanpublic Person person() {Person person new Person();person.setName(大彬);return person;}
}2、通过包扫描特定注解的方式
ComponentScan放置在我们的配置类上然后可以指定一个路径进行扫描带有特定注解的bean然后加至容器中。
特定注解包括Controller、Service、Repository、Component
Component
public class Person {//...
}ComponentScan(basePackages com.dabin.test.*)
public class Demo1 {public static void main(String[] args) {AnnotationConfigApplicationContext applicationContext new AnnotationConfigApplicationContext(Demo1.class);Person bean applicationContext.getBean(Person.class);System.out.println(bean);}
}3、Import注解导入
Import注解平时开发用的不多但是也是非常重要的在进行Spring扩展时经常会用到它经常搭配自定义注解进行使用然后往容器中导入一个配置文件。
ComponentScan
/*把用到的资源导入到当前容器中*/
Import({Person.class})
public class App {public static void main(String[] args) throws Exception {ConfigurableApplicationContext context SpringApplication.run(App.class, args);System.out.println(context.getBean(Person.class));context.close();}
}4、实现BeanDefinitionRegistryPostProcessor进行后置处理。
在Spring容器启动的时候会执行 BeanDefinitionRegistryPostProcessor 的 postProcessBeanDefinitionRegistry 方法就是等beanDefinition加载完毕之后对beanDefinition进行后置处理可以在此进行调整IOC容器中的beanDefinition从而干扰到后面进行初始化bean。
在下面的代码中我们手动向beanDefinitionRegistry中注册了person的BeanDefinition。最终成功将person加入到applicationContext中。
public class Demo1 {public static void main(String[] args) {AnnotationConfigApplicationContext applicationContext new AnnotationConfigApplicationContext();MyBeanDefinitionRegistryPostProcessor beanDefinitionRegistryPostProcessor new MyBeanDefinitionRegistryPostProcessor();applicationContext.addBeanFactoryPostProcessor(beanDefinitionRegistryPostProcessor);applicationContext.refresh();Person bean applicationContext.getBean(Person.class);System.out.println(bean);}
}class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {Overridepublic void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {AbstractBeanDefinition beanDefinition BeanDefinitionBuilder.rootBeanDefinition(Person.class).getBeanDefinition();registry.registerBeanDefinition(person, beanDefinition);}Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {}
}5、使用FactoryBean接口
如下图代码使用Configuration Bean的方式将 PersonFactoryBean 加入到容器中这里没有向容器中直接注入 Person而是注入 PersonFactoryBean然后从容器中拿Person这个类型的bean。
Configuration
public class Demo1 {Beanpublic PersonFactoryBean personFactoryBean() {return new PersonFactoryBean();}public static void main(String[] args) {AnnotationConfigApplicationContext applicationContext new AnnotationConfigApplicationContext(Demo1.class);Person bean applicationContext.getBean(Person.class);System.out.println(bean);}
}class PersonFactoryBean implements FactoryBeanPerson {Overridepublic Person getObject() throws Exception {return new Person();}Overridepublic Class? getObjectType() {return Person.class;}
}5.bean的作用域
1、singleton单例Spring中的bean默认都是单例的。
2、prototype每次请求都会创建一个新的bean实例。
3、request每一次HTTP请求都会产生一个新的bean该bean仅在当前HTTP request内有效。
4、session每一次HTTP请求都会产生一个新的bean该bean仅在当前HTTP session内有效。
5、global-session全局session作用域。