南宁市西乡塘区建设局网站,圣矢网络重庆网站建设优化推广公司,如何用源码搭建网站,网站维护产品推介目录
Spring篇
1、请举例说明Qualifier注解
2、构造方法注入和设值注入有什么区别#xff1f;
3、Spring框架中有哪些不同类型的事件#xff1f;
4、FileSystemResource和ClassPathResource有什么区别#xff1f;
5、Spring框架中都用到了哪些设计模式#xff1f;
6…目录
Spring篇
1、请举例说明Qualifier注解
2、构造方法注入和设值注入有什么区别
3、Spring框架中有哪些不同类型的事件
4、FileSystemResource和ClassPathResource有什么区别
5、Spring框架中都用到了哪些设计模式
6、开发中主要使用Spring的什么技术
7、简述AOP和IOC概念
8、Spring中如何配置Bean
9、IOC容器对Bean的生命周期 Spring篇
1、请举例说明Qualifier注解 Qualifier 注解意味着可以在被标注 bean 的字段上可以自动装配。 Qualifier 注解可以用来取消 Spring 不能取消的 bean 应用。 下面的示例将会在 Customer 的 person 属性中自动装配 person 的值。 public class Customer{ Autowired private Person person;
} 下面我们要在配置文件中来配置 Person 类。 bean idcustomer classcom.somnus.common.Customer / bean idpersonA classcom.somnus.common.Person property namename valuelokesh /
/bean bean idpersonB classcom.somnus.common.Person property namename valuealex /
/bean Spring 会知道要自动装配哪个 person bean 么不会的但是运行上面的示例时会抛出下面的异常 Caused by:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.howtodoinjava.common.Person] is defined: expected single matching bean but found 2: [personA, personB] 要解决上面的问题需要使用 Quanlifier 注解来告诉 Spring 容器要装配哪个 bean public class Customer{ Autowired Qualifier(personA) private Person person;
} 2、构造方法注入和设值注入有什么区别 请注意以下明显的区别 在设值注入方法支持大部分的依赖注入如果我们仅需 要注入 int、string 和 long 型的变量我们不要用设值的方法注入。对于基本类型如果我们没有注入的话可以为基本类型设置默认值。在构造方法 注入不支持大部分的依赖注入因为在调用构造方法中必须传入正确的构造参数否则的话为报错。 设值注入不会重写构造方法的值。如果我们对同一个变量同时使用了构造方法注入又使用了设置方法注入的话那么构造方法将不能覆盖由设值方法注入的值。很明显因为构造方法尽在对象被创建时调用。 在使用设值注入时有可能还不能保证某种依赖是否已经被注入也就是说这时对象的依赖关系有可能是不完整的。而在另一种情况下构造器注入则不允许生成依赖关系不完整的对象。 在设值注入时如果对象 A 和对象 B 互相依赖在创建对象 A 时 Spring 会抛出 sObjectCurrentlyInCreationException 异常因为在 B 对象被创建之前 A 对象是不能被创建的反之亦然。所以 Spring 用设值注入的方法解决了循环依赖的问题因对象的设值方法是在对象被创建之前被调用的。 3、Spring框架中有哪些不同类型的事件 Spring 的 ApplicationContext 提供了支持事件和代码中监听器的功能。 我们可以创建 bean 用来监听在 ApplicationContext 中发布的事件。 ApplicationEven t类和在 ApplicationContext 接口 中处理的事件如果一个 bean 实现了ApplicationListener 接口当一个 ApplicationEvent 被发布以后 bean 会自动被通知。 public class AllApplicationEventListener implements ApplicationListenerApplicationEvent { Override public void onApplicationEvent(ApplicationEvent applicationEvent) { //process event }
} Spring 提供了以下 5 中标准的事件 上下文更新事件ContextRefreshedEvent该事件会在 ApplicationContext 被初始化或者 更新时发布。也可以在调用 ConfigurableApplicationContext 接口中的 refresh()方法时被触发。 上下文开始事件ContextStartedEvent当容器调用 ConfigurableApplicationContext 的 Start()方法开始/重新开始容器时触发该事件。 上下文停止事件ContextStoppedEvent当容器调用 ConfigurableApplicationContext 的 Stop()方法停止容器时触发该事件。 上下文关闭事件ContextClosedEvent当 ApplicationContext 被关闭时触发该事件。容器被关闭时其管理的所有单例 Bean 都被销毁。 请求处理事件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); 4、FileSystemResource和ClassPathResource有什么区别 在 FileSystemResource 中需要给出 spring-config.xml 文件在你项目中的相对路径或者绝对路径。在 ClassPathResource 中 spring 会在 ClassPath 中自动搜寻配置文件所以要把 ClassPathResource 文件放在 ClassPath 下。 如果将 spring-config.xml 保存在了 src 文件夹下的话只需给出配置文件的名称即可因为 src 文件夹是默认。 简而言之 ClassPathResource 在环境变量中读取配置文件 FileSystemResource 在配置文件中读取配置文件。 5、Spring框架中都用到了哪些设计模式 Spring 框架中使用到了大量的设计模式下面列举了比较有代表性的 代理模式—在 AOP 和 remoting 中被用的比较多。 单例模式—在 spring 配置文件中定义的 bean 默认为单例模式。模板方法—用来解决代码重复的问题。比如. RestTemplate, JmsTemplate, JpaTemplate。 前端控制器—Spring 提供了 DispatcherServlet 来对请求进行分发。 视图帮助(View Helper )—Spring 提供了一系列的 JSP 标签高效宏来辅助将分散的代码整合在视图里。 依赖注入—贯穿于 BeanFactory / ApplicationContext 接口的核心理念。 工厂模式—BeanFactory 用来创建对象的实例 6、开发中主要使用Spring的什么技术 IOC 容器管理各层的组件 使用 AOP 配置声明式事务 整合其他框架. 7、简述AOP和IOC概念 AOPAspect Oriented Program 面向 ( 方面 ) 切面的编程 Filter( 过滤器 ) 也是一种 AOP。AOP 是一种新的方法论 是对传统 OOP(Object-Oriented Programming, 面向对象编程 ) 的补充 。AOP 的主要编程对象是切面(aspect) 而切面模块化横切关注点 . 可以举例通过事务说明。 IOCInvert Of Control 控制反转。 也成为 DI( 依赖注入 ) 其思想是反转资源获取的方向 。 传统的资源查找方式要求组件向容器发起请求查找资源。 作为回应 容器适时的返回资源 。 而应用了IOC 之后 则是容器主动地将资源推送 给它所管理的组件 组件所要做的仅是选择一种合适的方式来接受资源。 这种行 为也被称为查找的被动形式。 8、Spring中如何配置Bean Bean 的配置方式 : 通过全类名反射、通过工厂方法静态工厂方法 实 例工厂方法、FactoryBean 9、IOC容器对Bean的生命周期 通过构造器或工厂方法创建 Bean 实例 为 Bean 的属性设置值和对其他 Bean 的引用 将 Bean 实 例 传 递 给 Bean 后 置 处 理 器 的 postProcessBeforeInitialization 方法 调用 Bean 的初始化方法(init-method)将 Bean 实 例 传 递 给 Bean 后 置 处 理 器 的 postProcessAfterInitialization 方法Bean 可以使用了 当容器关闭时, 调用 Bean 的销毁方法(destroy-method)