初识Java_Spring 之要点记忆


Spring 是最受欢迎的企业级 Java 应用程序开发框架,数以百万的来自世界各地的开发人员使用 Spring 框架来创建性能好、易于测试、可重用的代码。

依赖注入

  • Spring 最认同的技术是控制反转的依赖注入(DI)模式。
  • 控制反转(IoC)是一个通用的概念,它可以用许多不同的方式去表达,依赖注入仅仅是控制反转的一个具体的例子。
  • IOC 容器具有依赖注入功能的容器,它可以创建对象,IOC 容器负责实例化、定位、配置应用程序中的对象及建立这些对象间的依赖。通常new一个实例,控制权由程序员控制,而”控制反转”是指new实例工作不由程序员来做而是交给Spring容器来做。
  • Spring 提供了以下两种不同类型的容器
    1. Spring BeanFactory 容器(org.springframework.beans.factory.BeanFactory)
    2. Spring ApplicationContext 容器 (org.springframework.context.ApplicationContext)

spring 框架:

spring
(注:JDBC=Java Data Base Connectivity,ORM=Object Relational Mapping,OXM=Object XML Mapping,JMS=Java Message Service)

Spring Bean

  • 如何创建一个 bean
  • bean 的生命周期的详细信息
  • bean 的依赖关系
  1. 定义: bean 是一个被实例化,组装,并通过 Spring IoC 容器所管理的对象(定义一个类,在配置好元数据,然后交给容器去实例这就是bean的定义过程)
    Bean 与 Spring 容器的关系
    bean
  2. 作用域:singleton、prototype、request、session和global session
  3. 生命周期
    Bean的生命周期可以表达为:

Bean的定义——Bean的初始化——Bean的使用——Bean的销毁

在基于 XML 的配置元数据的情况下,你可以使用 init-method 属性来指定带有 void 无参数方法的名称。例如:

1
<bean id="exampleBean" class="examples.ExampleBean" init-method="init" destroy-method="destroy"/>

下面是类的定义:

1
2
3
4
5
6
7
8
9
public class ExampleBean {
public void init() {
// do some initialization work
}
}public class ExampleBean {
public void destroy() {
// do some destruction work
}
}

  1. 后置处理器
    Bean 后置处理器允许在调用初始化方法前后对 Bean 进行额外的处理。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    package com.wind;

    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.BeanPostProcessor;

    public class InitHelloWorld implements BeanPostProcessor {
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    System.out.println("BeforeInitialization : " + beanName);
    return bean; // you can return any other object as well
    }
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    System.out.println("AfterInitialization : " + beanName);
    return bean; // you can return any other object as well
    }
    }
  2. 定义继承

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="helloWorld" class="com.wind.HelloWorld">
    <property name="message1" value="Hello World!"/>
    <property name="message2" value="Hello Second World!"/>
    </bean>
    <bean id="helloIndia" class="com.wind.HelloIndia" parent="helloWorld">
    <property name="message1" value="Hello India!"/>
    <property name="message3" value="Namaste India!"/>
    </bean>
    <bean class="com.wind.InitHelloWorld" />
    </beans>

Spring 依赖注入

  1. 通过构造函数注入
    假设你有一个包含文本编辑器组件的应用程序,并且你想要提供拼写检查。标准代码看起来是这样的:
    1
    2
    3
    4
    5
    6
    public class TextEditor {
    private SpellChecker spellChecker;
    public TextEditor() {
    spellChecker = new SpellChecker(); // 相当于写死啦
    }
    }

在控制反转的场景中,我们反而会做这样的事情:

1
2
3
4
5
6
public class TextEditor {
private SpellChecker spellChecker;
public TextEditor(SpellChecker spellChecker) {
this.spellChecker = spellChecker;
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!-- Definition for textEditor bean -->
<bean id="textEditor" class="com.tutorialspoint.TextEditor">
<property name="spellChecker" ref="spellChecker"/>
</bean>

<!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
</bean>

</beans>
  1. 通过设置函数注入
    1
    2
    3
    4
    5
    6
    7
    ```java
    public class TextEditor {
    private SpellChecker spellChecker;
    public void setSpellChecker(SpellChecker spellChecker) {
    this.spellChecker = spellChecker;
    }
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!-- Definition for textEditor bean -->
<bean id="textEditor" class="com.tutorialspoint.TextEditor">
<property name="spellChecker" ref="spellChecker"/>
</bean>

<!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
</bean>

</beans>

两者唯一区别:就是在基于构造函数注入中,我们使用的是〈bean〉标签中的〈constructor-arg〉元素,而在基于设值函数的注入中,我们使用的是〈bean〉标签中的〈property〉元素。