|
22、构造方法注入和设值注入有什么区别?
请注意以下明显的区别:
1、在设值注入方法支持大部分的依赖注入,如果我们仅需要注入int、string和long型的变量,我们不要用设值的方法注入。
对于基本类型,如果我们没有注入的话,可以为基本类型设置默认值。在构造方法注入不支持大部分的依赖注入,
因为在调用构造方法中必须传入正确的构造参数,否则的话为报错。
2、设值注入不会重写构造方法的值。如果我们对同一个变量同时使用了构造方法注入又使用了设置方法注入的话,
那么构造方法将不能覆盖由设值方法注入的值。很明显,因为构造方法尽在对象被创建时调用。
3、在使用设值注入时有可能还不能保证某种依赖是否已经被注入,也就是说这时对象的依赖关系有可能是不完整的。
而在另一种情况下,构造器注入则不允许生成依赖关系不完整的对象。
4、在设值注入时如果对象A和对象B互相依赖,在创建对象A时Spring会抛出sObjectCurrentlyInCreationException异常,
因为在B对象被创建之前A对象是不能被创建的,反之亦然。所以Spring用设值注入的方法解决了循环依赖的问题,
因对象的设值方法是在对象被创建之前被调用的。
23、Spring框架中有哪些不同类型的事件?
Spring的ApplicationContext 提供了支持事件和代码中监听器的功能。
我们可以创建bean用来监听在ApplicationContext 中发布的事件。ApplicationEvent类和在ApplicationContext接口中处理的事件,
如果一个bean实现了ApplicationListener接口,当一个ApplicationEvent 被发布以后,bean会自动被通知。
- public class AllApplicationEventListener implements ApplicationListener < ApplicationEvent >
- {
- @Override
- public void onApplicationEvent(ApplicationEvent applicationEvent)
- {
- //process event
- }
- }
复制代码 Spring 提供了以下5中标准的事件:
1、上下文更新事件(ContextRefreshedEvent):该事件会在ApplicationContext被初始化或者更新时发布。
也可以在调用ConfigurableApplicationContext 接口中的refresh()方法时被触发。
2、上下文开始事件(ContextStartedEvent):当容器调用ConfigurableApplicationContext的Start()
方法开始/重新开始容器时触发该事件。
3、上下文停止事件(ContextStoppedEvent):当容器调用ConfigurableApplicationContext的Stop()
方法停止容器时触发该事件。
4、上下文关闭事件(ContextClosedEvent):当ApplicationContext被关闭时触发该事件。
容器被关闭时,其管理的所有单例Bean都被销毁。
5、请求处理事件(RequestHandledEvent):在Web应用中,当一个http请求(request)结束触发该事件。
除了上面介绍的事件以外,还可以通过扩展ApplicationEvent 类来开发自定义的事件。
- public class CustomApplicationEvent extends ApplicationEvent
- {
- public CustomApplicationEvent ( Object source, final String msg )
- {
- super(source);
- System.out.println("Created a Custom event");
- }
- }
复制代码 为了监听这个事件,还需要创建一个监听器:
- public class CustomEventListener implements ApplicationListener < CustomApplicationEvent >
- {
- @Override
- public void onApplicationEvent(CustomApplicationEvent applicationEvent) {
- //handle event
- }
- }
复制代码 之后通过applicationContext接口的publishEvent()方法来发布自定义事件。
- CustomApplicationEvent customEvent = new CustomApplicationEvent(applicationContext, "Test message");
- applicationContext.publishEvent(customEvent);
复制代码 24、FileSystemResource和ClassPathResource有何区别?
在FileSystemResource 中需要给出spring-config.xml文件在你项目中的相对路径或者绝对路径。
在ClassPathResource中spring会在ClassPath中自动搜寻配置文件,所以要把ClassPathResource 文件放在ClassPath下。
如果将spring-config.xml保存在了src文件夹下的话,只需给出配置文件的名称即可,因为src文件夹是默认。
简而言之,ClassPathResource在环境变量中读取配置文件,FileSystemResource在配置文件中读取配置文件。
25、Spring 框架中都用到了哪些设计模式?
Spring框架中使用到了大量的设计模式,下面列举了比较有代表性的:
1、代理模式—在AOP和remoting中被用的比较多。
2、单例模式—在spring配置文件中定义的bean默认为单例模式。
3、模板方法—用来解决代码重复的问题。比如. RestTemplate, JmsTemplate, JpaTemplate。
4、前端控制器—Spring提供了DispatcherServlet来对请求进行分发。
5、视图帮助(View Helper )—Spring提供了一系列的JSP标签,高效宏来辅助将分散的代码整合在视图里。
6、依赖注入—贯穿于BeanFactory / ApplicationContext接口的核心理念。
7、工厂模式—BeanFactory用来创建对象的实例。
转自:http://blog.itmyhome.com/2015/07/spring-interview/
|
|