1.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中标准的事件:
- 上下文更新事件(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) { }
}
之后通过applicationContext接口的publishEvent()方法来发布自定义事件。
CustomApplicationEvent customEvent = new CustomApplicationEvent(applicationContext,“Test message”);
applicationContext.publishEvent(customEvent);
2.BeanFactory 和 ApplicationContext 有什么区别?
BeanFactory 可以理解为含有 bean 集合的工厂类
- BeanFactory 包含了 bean 的定义,以便在接收到客户端请求时将对应的bean实例化。
- BeanFactory 还能在实例化对象的时生成协作类之间的关系,此举将 bean自身与 bean客户端的配置中解放出来。
- BeanFactory 还包含了 bean生命周期的控制,调用客户端的初始化方法(initializationMethods)和销毁方法(destruction Methods)。
从表面上看,ApplicationContext 如同 BeanFactory 一样具有 bean 定义、bean 关联关系的设置,根据请求分发bean的功能。但ApplicationContext在此基础上还提供了其他的功能
- 提供了支持国际化的文本消息
- 统一的资源文件读取方式
- 在监听器中注册的bean的事件
以下是三种较常见的 ApplicationContext 实现方式:
-
ClassPathXmlApplicationContext:从 classpath 的 XML配置文件中读取上下文,并生成上下文定义。应用程序上下文从程序环境变量中取得。
ApplicationContext context = new ClassPathXmlApplicationContext(“application.xml”); -
FileSystemXmlApplicationContext :由文件系统中的XML配置文件读取上下文。
ApplicationContext context = new FileSystemXmlApplicationContext(“application.xml”); -
XmlWebApplicationContext:由Web应用的XML文件读取上下文。
3.如何在Spring配置设置元数据方式?
将Spring配置到应用开发中有以下三种方式:
- 基于XML的配置
- 基于注解的配置
- 基于Java的配置
4.什么是Spring inner beans?
在Spring框架中,无论何时bean被使用时,当仅被调用了一个属性。一个明智的做法是将这个bean声明为内部bean。内部bean可以用setter注入“属性”和构造方法注入“构造参数”的方式来实现。比如,在我们的应用程序中,一个Customer类引用了一个Person类,我们的要做的是创建一个 Person的实例,然后在Customer内部使用。
public class Customer{
private Person person;
}
public class Person{
private String name;
private String address;
private int age;
}
内部bean的声明方式如下:
<bean id="CustomerBean" class="com.my.common.Customer">
<property name="person"> <bean class="com.gupaoedu.common.Person">
<property name="name" value="lokesh" />
<property name="address" value="India" />
<property name="age" value="34" /> </bean>
</property>
</bean>
5.请举例解释@Required Annotation?
在产品级别的应用中,IOC 容器可能声明了数十万了bean,bean与bean之间有着复杂的依赖关系。设值注解方法的短板之一就是验证所有的属性是否被注解是一项十分困难的操作。可以通过在中设置“dependency-check”来解决这个问题。
在应用程序的生命周期中,你可能不大愿意花时间在验证所有bean的属性是否按照上下文文件正确配置。或者你宁可验证某个 bean的特定属性是否被正确的设置。即使是用“dependency-check”属性也不能很好的解决这个问题,在这种情况下,你需要使用@Required 注解。需要用如下的方式使用来标明bean的设值方法。
public class EmployeeFactoryBean extends AbstractFactoryBean<Object> {
private String designation; public String getDesignation() {
return designation;
}
@Required
public void setDesignation(String designation) {
this.designation = designation;
}
}
RequiredAnnotationBeanPostProcessor 是 Spring 中的后置处理用来验证被@Required 注解的bean属性是否被正确的设置了。在使用RequiredAnnotationBeanPostProcesso 来验证bean属性之前,首先要在IOC 容器中对其进行注册:
<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor" />
但是如果没有属性被用 @Required 注解过的话,后置处理器会抛出一个BeanInitializationException异常。
6.请举例说明@Qualifier注解?
@Qualifier注解意味着可以在被标注bean的字段上可以自动装配。
Qualifier注解可以用来取消Spring不能取消的bean应用。
7.FileSystemResource和ClassPathResource有何区别?
在FileSystemResource 中需要给出 spring-config.xml文件在你项目中的相对路径或者绝对路径。在ClassPathResource中spring会在 ClassPath中自动搜寻配置文件,所以要把 ClassPathResource 文件放在ClassPath下。
如果将 spring-config.xml 保存在了 src 文件夹下的话,只需给出配置文件的名称即可,因为 src文件夹是默认。
简而言之,ClassPathResource在环境变量中读取配置文件,FileSystemResource在配置文件中读取配置文件。
8.Spring可以注入null或空字符串?
完全可以。






还没有评论,来说两句吧...