Spring 中的 org.springframework.beans 包和 org.springframework.context 包 构成了 Spring 框架 IoC 容器的基础。
BeanFactory 接口提供了一个先进的配置机制,使得任何类型的对象的配置成为可能。 ApplicationContex 接口对 BeanFactory(是一个子接口)进行了扩展,在 BeanFactory 的基础上添加了其他功能,比如与 Spring 的 AOP 更容易集成,也提供了处理 message resource 的机制(用于国际化)、事件传播以及应用层的特别配置,比如针对 Web 应用的 WebApplicationContext。
org.springframework.beans.factory.BeanFactory 是 Spring IoC 容器的具体实现, 用来包装和管理前面提到的各种 bean。 BeanFactory 接口是 Spring IoC 容器的核心接口。 IOC:把对象的创建、初始化、销毁交给 spring 来管理,而不是由开发者控制,实现控制反转。
3、 XmlWebApplicationContext:由 Web 应用的 XML 文件读取上下文。 4.AnnotationConfigApplicationContext(基于 Java 配置启动容器)
6、 Spring 有几种配置方式?将 Spring 配置到应用开发中有以下三种方式:
基于 XML 的配置
基于注解的配置
基于 Java 的配置
7、如何用基于 XML 配置的方式配置 Spring?
在 Spring 框架中,依赖和服务需要在专门的配置文件来实现,我常用的 XML 格式的配置文件。这 些配置文件的格式通常用开头,然后一系列的 bean 定义和专门的应用配置 选项组成。 SpringXML 配置的主要目的时候是使所有的 Spring 组件都可以用 xml 文件的形式来进行配置。这 意味着不会出现其他的 Spring 配置类型(比如声明的方式或基于 Java Class 的配置方式) Spring 的 XML 配置方式是使用被 Spring 命名空间的所支持的一系列的 XML 标签来实现的。 Spring 有以下主要的命名空间: context、 beans、 jdbc、 tx、 aop、 mvc 和 aso。 如:
<beans><!-- JSON Support --><bean name="viewResolver"class="org.springframework.web.servlet.view.BeanNameViewResolver"/><bean name="jsonTemplate"class="org.springframework.web.servlet.view.json.MappingJackson2JsonV
iew"/><bean id="restTemplate"class="org.springframework.web.client.RestTemplate"/></beans>
下面这个 web.xml 仅仅配置了 DispatcherServlet,这件最简单的配置便能满足应用程序配置运
行时组件的需求。
<web-app><display-name>Archetype Created Web Application</display-name><servlet><servlet-name>spring</servlet-name><servletclass>org.springframework.web.servlet.DispatcherServlet</servletclass><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>spring</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>
8、如何用基于 Java 配置的方式配置 Spring?
Spring 对 Java 配置的支持是由@Configuration 注解和@Bean 注解来实现的。由@Bean 注解 的方法将会实例化、配置和初始化一个 新对象,这个对象将由 Spring 的 IoC 容器来管理。 @Bean 声明所起到的作用与 元素类似。被 @Configuration 所注解的类则表示这个类 的主要目的是作为 bean 定义的资源。被@Configuration 声明的类可以通过在同一个类的 内部调 用@bean 方法来设置嵌入 bean 的依赖关系。 最简单的@Configuration 声明类请参考下面的代码:
在上面的例子中, com.acme 包首先会被扫到,然后再容器内查找被@Component 声明的类,找 到后将这些类按照 Sring bean 定义进行注册。 如果你要在你的 web 应用开发中选用上述的配置的方式的话,需要用 AnnotationConfigWebApplicationContext 类来读 取配置文件,可以用来配置 Spring 的 Servlet 监听器 ContextLoaderListener 或者 Spring MVC 的 DispatcherServlet。
<web-app><!-- Configure ContextLoaderListener to use
AnnotationConfigWebApplicationContext
instead of the default XmlWebApplicationContext --><context-param><param-name>contextClass</param-name><param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicatio
nContext
</param-value></context-param><!-- Configuration locations must consist of one or more comma- or
space-delimited
fully-qualified @Configuration classes. Fully-qualified
packages may also be
specified for component-scanning --><context-param><param-name>contextConfigLocation</param-name><param-value>com.howtodoinjava.AppConfig</param-value></context-param><!-- Bootstrap the root application context as usual using
ContextLoaderListener --><listener><listenerclass>org.springframework.web.context.ContextLoaderListener</listener
-class></listener><!-- Declare a Spring MVC DispatcherServlet as usual --><servlet><servlet-name>dispatcher</servlet-name><servletclass>org.springframework.web.servlet.DispatcherServlet</servletclass><!-- Configure DispatcherServlet to use
AnnotationConfigWebApplicationContext
instead of the default XmlWebApplicationContext --><init-param><param-name>contextClass</param-name><param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicatio
nContext
</param-value></init-param><!-- Again, config locations must consist of one or more commaor space-delimited
and fully-qualified @Configuration classes --><init-param><param-name>contextConfigLocation</param-name><param-value>com.howtodoinjava.web.MvcConfig</paramvalue></init-param></servlet><!-- map all requests for/app/* to the dispatcher servlet -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
</web-app>
9、怎样用注解的方式配置 Spring?
Spring 在 2.5 版本以后开始支持用注解的方式来配置依赖注入。可以用注解的方式来替代 XML 方 式的 bean 描述,可以将 bean 描述转移到组件类的 内部,只需要在相关类上、方法上或者字段声 明上使用注解即可。注解注入将会被容器在 XML 注入之前被处理,所以后者会覆盖掉前者对于同一 个属性的处理结 果。 注解装配在 Spring 中是默认关闭的。所以需要在 Spring 文件中配置一下才能使用基于注解的装配 模式。如果你想要在你的应用程序中使用关于注解的方法的话,请参考如下的配置。
context:annotation-config/
在 标签配置完成以后,就可以用注解的方式在 Spring 中向属 性、方法和构造方法中自动装配变量。 下面是几种比较重要的注解类型: 1. @Required:该注解应用于设值方法。 2. @Autowired:该注解应用于有值设值方法、非设值方法、构造方法和变量。 3. @Qualifier:该注解和@Autowired 注解搭配使用,用于消除特定 bean 自动装配的歧义。 4. JSR-250 Annotations: Spring 支持基于 JSR-250 注解的以下注解, @Resource、 @PostConstruct 和 @PreDestroy。 ## 10、请解释 Spring Bean 的生命周期? Spring Bean 的生命周期简单易懂。在一个 bean 实例被初始化时,需要执行一系列的初始化操作 以达到可用的状态。同样的,当一个 bean 不在被调用时需要进行相关的析构操作,并从 bean 容 器中移除。 Spring bean factory 负责管理在 spring 容器中被创建的 bean 的生命周期。 Bean 的生命周期 由两组回调(call back)方法组成。 1. 初始化之后调用的回调方法。 2. 销毁之前调用的回调方法。 Spring 框架提供了以下四种方式来管理 bean 的生命周期事件: InitializingBean 和 DisposableBean 回调接口 针对特殊行为的其他 Aware 接口 Bean 配置文件中的 Custom init()方法和 destroy()方法 @PostConstruct 和@PreDestroy 注解方式 使用 customInit()和 customDestroy()方法管理 bean 生命周期的代码样例如 下: ## 11、 Spring Bean 的作用域之间有什么区别? Spring 容器中的 bean 可以分为 5 个范围。所有范围的名称都是自说明的,但是为了避免混淆,还 是让我们来解释一下: 1. singleton:这种 bean 范围是默认的,这种范围确保不管接受到多少个请求,每个容器中只有一个 bean 的实例,单例的模式由 bean factory 自身来维护。 2. prototype:原形范围与单例范围相反,为每一个 bean 请求提供一个实例。 3. request:在请求 bean 范围内会每一个来自客户端的网络请求创建一个实例,在请求完成以后, bean 会失效并被垃圾回收器回收。 4. Session:与请求范围类似,确保每个 session 中有一个 bean 的实例,在 session 过期后, bean 会随之失效。 5. global- session: global-session 和 Portlet 应用相关。当你的应用部署在 Portlet 容器中工作 时,它包含很多 portlet。如果 你想要声明让所有的 portlet 共用全局的存储变量的话,那么这全 局变量需要存储在 global-session 中。 全局作用域与 Servlet 中的 session 作用域效果相同。 ## 12、什么是 Spring inner beans? 在 Spring 框架中,无论何时 bean 被使用时,当仅被调用了一个属性。一个明智的做法是将这个 bean 声明为内部 bean。内部 bean 可以用 setter 注入“属性”和构造方法注入“构造参数”的方式来 实现。 比如,在我们的应用程序中,一个 Customer 类引用了一个 Person 类,我们的要做的是创建一个 Person 的实例,然后在 Customer 内部使用。 public class Customer{ private Person person; //Setters and Getters} public class Person{ private String name; private String address; private int age; //Setters and Getters } 内部 bean 的声明方式如下:
## 13、 Spring 框架中的单例 Beans 是线程安全的么? Spring 框架并没有对单例 bean 进行任何多线程的封装处理。关于单例 bean 的线程安全和并发问 题需要开发者自行去搞定。但实际上,大部分的 Spring bean 并没有可变的状态(比如 Serview 类 和 DAO 类),所以在某种程度上说 Spring 的单例 bean 是线程安全的。如果你的 bean 有多种状 态的话(比如 View Model 对象),就需要自行保证线程安全。 最浅显的解决办法就是将多态 bean 的作用域由“singleton”变更为“prototype”。14、请举例说明如何在 Spring 中注入一个 Java Collection? Spring 提供了以下四种集合类的配置元素: : 该标签用来装配可重复的 list 值。 : 该标签用来装配没有重复的 set 值。
以下为 spring 常见面试问题:1、什么是 Spring 框架? Spring 框架有哪些主要模块?Spring 框架是一个为 Java 应用程序的开发提供了综合、广泛的基础性支持的 Java 平台。Spring 帮助开发者解决了开发中基础性的问题,使得开发人员可以专注于应用程序的开发。Spring框架本身亦是按照设计模式精心打造,这使得我们可以在开发环境中安心的集成 Spring 框 架,不必担心 Spring是如何在后台进行工作的。Spring 框架至今已集成了 20 多个