[关闭]
@yudesong 2018-02-16T13:02:49.000000Z 字数 6949 阅读 529

Spring框架简介

Spring


spring是J2EE应用程序框架,是轻量级的IoC和AOP的容器框架,主要是针对javaBean的生命周期进行管理的轻量级容器,可以单独使用,也可以和Struts框架,ibatis框架等组合使用。

Spring 体系结构:

Hello World 实例

javaBean

  1. public class HelloWorld {
  2. private String message;
  3. public void setMessage(String message){
  4. this.message = message;
  5. }
  6. public void getMessage(){
  7. System.out.println("Your Message : " + message);
  8. }
  9. }

XML配置文件

  1. <bean id="helloWorld" class="com.tutorialspoint.HelloWorld">
  2. <property name="message" value="Hello World!"/>
  3. </bean>

测试类

  1. public class MainApp {
  2. public static void main(String[] args) {
  3. ApplicationContext context =
  4. new ClassPathXmlApplicationContext("Beans.xml");
  5. HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
  6. obj.getMessage();
  7. }
  8. }

IOC 容器

Spring 容器是 Spring 框架的核心。容器将创建对象,把它们连接在一起,配置它们,并管理他们的整个生命周期从创建到销毁。Spring 容器使用依赖注入(DI)来管理组成一个应用程序的组件。这些对象被称为 Spring Beans

Spring 提供了以下两种不同类型的容器。

序号 容器 & 描述
Spring BeanFactory 容器 它是最简单的容器,给 DI 提供了基本的支持,它用 org.springframework.beans.factory.BeanFactory 接口来定义。BeanFactory 或者相关的接口,如 BeanFactoryAware,InitializingBean,DisposableBean,在 Spring 中仍然存在具有大量的与 Spring 整合的第三方框架的反向兼容性的目的
Spring ApplicationContext 容器 该容器添加了更多的企业特定的功能,例如从一个属性文件中解析文本信息的能力,发布应用程序事件给感兴趣的事件监听器的能力。该容器是由 org.springframework.context.ApplicationContext 接口定义

实例代码

  1. XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("Beans.xml"));
  2. HelloWorld obj = (HelloWorld) factory.getBean("helloWorld");
  3. obj.getMessage();

最常被使用的 ApplicationContext 接口实现:

Bean 的作用域

Bean 对象创建的时机
默认是随着容器创建,可以使用 lazy-init=true(在调用 getBean 创建)延迟创建也可以用

  1. <beans default-lazy-init="true"/>

批量延迟创建

Spring 框架支持以下五个作用域,如果你使用 web-aware ApplicationContext 时,其中三个是可用的。

作用域 描述
singleton 该作用域将 bean 的定义的限制在每一个 Spring IoC 容器中的一个单一实例(默认)
prototype 该作用域将单一 bean 的定义限制在任意数量的对象实例。
request 该作用域将 bean 的定义限制为 HTTP 请求。只在 web-aware Spring ApplicationContext 的上下文中有效
session 该作用域将 bean 的定义限制为 HTTP 会话。 只在web-aware Spring ApplicationContext的上下文中有效
global-session 该作用域将 bean 的定义限制为全局 HTTP 会话。只在 web-aware Spring ApplicationContext 的上下文中有效

Bean 的生命周期

为了定义安装和拆卸一个 bean,我们只要声明带有 init-method 和/或 destroy-method 参数(,仅适用于 singleton 模式
) 。init-method 属性指定一个方法,在实例化 bean 时,立即调用该方法。同样,destroy-method 指定一个方法,只有从容器中移除 bean 之后,才能调用该方法。

  1. <bean id="helloWorld"
  2. class="com.tutorialspoint.HelloWorld"
  3. init-method="init" destroy-method="destroy">
  4. <property name="message" value="Hello World!"/>
  5. </bean>

Bean 后置处理器

BeanPostProcessor 接口定义回调方法,你可以实现该方法来提供自己的实例化逻辑,依赖解析逻辑等。你也可以在 Spring 容器通过插入一个或多个 BeanPostProcessor 的实现来完成实例化,配置和初始化一个bean之后实现一些自定义逻辑回调方法。
HelloWorld.java

  1. public class HelloWorld {
  2. private String message;
  3. public void setMessage(String message){
  4. this.message = message;
  5. }
  6. public void getMessage(){
  7. System.out.println("Your Message : " + message);
  8. }
  9. public void init(){
  10. System.out.println("Bean is going through init.");
  11. }
  12. public void destroy(){
  13. System.out.println("Bean will destroy now.");
  14. }
  15. }

InitHelloWorld.java

  1. public class InitHelloWorld implements BeanPostProcessor {
  2. public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
  3. System.out.println("BeforeInitialization : " + beanName);
  4. return bean; // you can return any other object as well
  5. }
  6. public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
  7. System.out.println("AfterInitialization : " + beanName);
  8. return bean; // you can return any other object as well
  9. }
  10. }

MainApp.java 文件的内容。在这里,你需要注册一个在 AbstractApplicationContext 类中声明的关闭 hook 的 registerShutdownHook() 方法。它将确保正常关闭,并且调用相关的 destroy 方法。

  1. public class MainApp {
  2. public static void main(String[] args) {
  3. AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
  4. HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
  5. obj.getMessage();
  6. context.registerShutdownHook();
  7. }
  8. }

Beans.xml

  1. <bean id="helloWorld" class="com.tutorialspoint.HelloWorld"
  2. init-method="init" destroy-method="destroy">
  3. <property name="message" value="Hello World!"/>
  4. </bean>
  5. <bean class="com.tutorialspoint.InitHelloWorld" />

Bean 定义继承

Beans.xml

  1. <bean id="helloWorld" class="com.tutorialspoint.HelloWorld">
  2. <property name="message1" value="Hello World!"/>
  3. <property name="message2" value="Hello Second World!"/>
  4. </bean>
  5. <bean id="helloIndia" class="com.tutorialspoint.HelloIndia" parent="helloWorld">
  6. <property name="message1" value="Hello India!"/>
  7. <property name="message3" value="Namaste India!"/>
  8. </bean>

HelloWorld.java

  1. public class HelloWorld {
  2. private String message1;
  3. private String message2;
  4. public void setMessage1(String message){
  5. this.message1 = message;
  6. }
  7. public void setMessage2(String message){
  8. this.message2 = message;
  9. }
  10. public void getMessage1(){
  11. System.out.println("World Message1 : " + message1);
  12. }
  13. public void getMessage2(){
  14. System.out.println("World Message2 : " + message2);
  15. }
  16. }

HelloIndia.java

  1. public class HelloIndia {
  2. private String message1;
  3. private String message2;
  4. private String message3;
  5. public void setMessage1(String message){
  6. this.message1 = message;
  7. }
  8. public void setMessage2(String message){
  9. this.message2 = message;
  10. }
  11. public void setMessage3(String message){
  12. this.message3 = message;
  13. }
  14. public void getMessage1(){
  15. System.out.println("India Message1 : " + message1);
  16. }
  17. public void getMessage2(){
  18. System.out.println("India Message2 : " + message2);
  19. }
  20. public void getMessage3(){
  21. System.out.println("India Message3 : " + message3);
  22. }
  23. }

MainApp.java

  1. public class MainApp {
  2. public static void main(String[] args) {
  3. ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
  4. HelloWorld objA = (HelloWorld) context.getBean("helloWorld");
  5. objA.getMessage1();
  6. objA.getMessage2();
  7. HelloIndia objB = (HelloIndia) context.getBean("helloIndia");
  8. objB.getMessage1();
  9. objB.getMessage2();
  10. objB.getMessage3();
  11. }
  12. }

Bean 定义模板

你可以创建一个 Bean 定义模板,不需要花太多功夫它就可以被其他子 bean 定义使用。在定义一个 Bean 定义模板时,你不应该指定类的属性,而应该指定带 true 值的抽象属性

  1. <bean id="beanTeamplate" abstract="true">
  2. <property name="message1" value="Hello World!"/>
  3. <property name="message2" value="Hello Second World!"/>
  4. <property name="message3" value="Namaste India!"/>
  5. </bean>
  6. <bean id="helloIndia" class="com.tutorialspoint.HelloIndia" parent="beanTeamplate">
  7. <property name="message1" value="Hello India!"/>
  8. <property name="message3" value="Namaste India!"/>
  9. </bean>
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注