[关闭]
@mritd 2016-01-02T05:49:24.000000Z 字数 6985 阅读 1618

Spring4 学习笔记

Spring


Spring-logo

一、Spring Hello World

  1. <dependency>
  2. <groupId>org.springframework</groupId>
  3. <artifactId>spring-context</artifactId>
  4. <version>4.2.4.RELEASE</version>
  5. </dependency>
  1. package me.mritd.beans;
  2. public class Persion {
  3. private String name;
  4. private int age;
  5. private String address;
  6. public String getName() {
  7. return name;
  8. }
  9. public void setName(String name) {
  10. this.name = name;
  11. }
  12. public int getAge() {
  13. return age;
  14. }
  15. public void setAge(int age) {
  16. this.age = age;
  17. }
  18. public String getAddress() {
  19. return address;
  20. }
  21. public void setAddress(String address) {
  22. this.address = address;
  23. }
  24. @Override
  25. public String toString() {
  26. return "Persion [name=" + name + ", age=" + age + ", address=" + address + "]";
  27. }
  28. public Persion() {
  29. super();
  30. // TODO Auto-generated constructor stub
  31. }
  32. public Persion(String name, int age, String address) {
  33. super();
  34. this.name = name;
  35. this.age = age;
  36. this.address = address;
  37. }
  38. }
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <bean id="persion" name="persion" class="me.mritd.beans.Persion">
  6. <property name="name" value="Hello World"></property>
  7. <property name="age" value="20"></property>
  8. <property name="address" value="伊拉克"></property>
  9. </bean>
  10. </beans>
  1. package me.mritd.test;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. import me.mritd.beans.Persion;
  6. /**
  7. *
  8. * Copyright © 2016 Mritd. All rights reserved.
  9. *
  10. * @ClassName: Test1
  11. * @Description: TODO
  12. * @author: 漠然
  13. * @date: 2016年1月1日 下午8:59:14
  14. */
  15. public class Test1 {
  16. @Test
  17. public void testHelloWorld(){
  18. // 创建ApplicationContext
  19. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
  20. // 从上下文获取 persion Bean
  21. Persion persion = (Persion) applicationContext.getBean("persion");
  22. // 查看获取的Persion
  23. System.out.println(persion);
  24. }
  25. }

二、Spring Bean配置

IOC(Inversion of Control):其思想是反转资源获取的方向.传统的资源查找方式要求组件向容器发起请求查找资源. 作为回应, 容器适时的返回资源. 而应用了 IOC 之后,则是容器主动地将资源推送给它所管理的组件,组件所要做的仅是选择一种合适的方式来接受资源. 这种行为也被称为查找的被动形式 DI(Dependency Injection) - IOC 的另一种表述方式:即组件以一些预先定义好的方式(例如: setter 方法)接受来自如容器的资源注入. 相对于 IOC 而言,这种表述更直接。

Spring通过在xml配置文件中的 <bean> 节点来配置放入IOC容器中的bean;

  1. <bean id="persion" name="persion" class="me.mritd.beans.Persion">
  2. <property name="name" value="Hello World"></property>
  3. <property name="age" value="20"></property>
  4. <property name="address" value="伊拉克"></property>
  5. </bean>
  • id: IOC容器中Bean的ID
    • 在 IOC 容器中必须是唯一的
    • 若 id 没有指定,Spring 自动 将全限定性类名 作为 Bean 的名字
    • id 可以指定多个名字,名字之间可用逗号、分号、或空格分隔

在 Spring IOC 容器读取 Bean 配置创建 Bean 实例之前,必须对它进行实例化. 只有在容器实例化后, 才可以从 IOC 容器里获取 Bean 实例并使用;Spring 提供了 两种类型 的 IOC 容器实现:

  • BeanFactory: IOC 容器的基本实现.
  • ApplicationContext: 提供了更多的高级特性. 是 BeanFactory 的子接口.

BeanFactory 是 Spring 框架的基础设施,面向 Spring 本身;ApplicationContext 面向使用 Spring 框架的开发者,几乎所有的应用场合都直接使用 ApplicationContext 而非底层的 BeanFactory;无论使用何种方式,配置文件时相同的. ApplicationContext UML 图示例如下
Spring-applicationContext-UML

  • ApplicationContext 的主要实现类:

    • ClassPathXmlApplicationContext:从 类路径下加载配置文件
    • FileSystemXmlApplicationContext: 从文件系统中加载配置文件
  • ConfigurableApplicationContext 扩展于 ApplicationContext,新增加两个主要方法:refresh()close(), 让 ApplicationContext 具有启动、刷新和关闭上下文的能力

  • ApplicationContext 在初始化上下文时就实例化所有单例的 Bean

  • WebApplicationContext 是专门为 WEB 应用而准备的,它允许从相对于 WEB 根目录的路径中完成初始化工作

首先定义两个 Java Bean

  1. package me.mritd.beans;
  2. /**
  3. *
  4. * Copyright © 2016 Mritd. All rights reserved.
  5. *
  6. * @ClassName: Car
  7. * @Description: TODO
  8. * @author: 漠然
  9. * @date: 2016年1月2日 上午1:09:46
  10. */
  11. public class Car {
  12. private String carName;
  13. public String getCarName() {
  14. return carName;
  15. }
  16. public void setCarName(String carName) {
  17. this.carName = carName;
  18. }
  19. @Override
  20. public String toString() {
  21. return "Car [carName=" + carName + "]";
  22. }
  23. public Car() {
  24. super();
  25. // TODO Auto-generated constructor stub
  26. }
  27. public Car(String carName) {
  28. super();
  29. this.carName = carName;
  30. }
  31. }
  1. package me.mritd.beans;
  2. /**
  3. *
  4. * Copyright © 2016 Mritd. All rights reserved.
  5. *
  6. * @ClassName: Persion
  7. * @Description: TODO
  8. * @author: 漠然
  9. * @date: 2016年1月2日 上午1:11:21
  10. */
  11. public class Persion {
  12. private String name;
  13. private int age;
  14. private String address;
  15. private Car car;
  16. @Override
  17. public String toString() {
  18. return "Persion [name=" + name + ", age=" + age + ", address=" + address + ", car=" + car + "]";
  19. }
  20. public Persion() {
  21. super();
  22. // TODO Auto-generated constructor stub
  23. }
  24. public Persion(String name, int age, String address, Car car) {
  25. super();
  26. this.name = name;
  27. this.age = age;
  28. this.address = address;
  29. this.car = car;
  30. }
  31. public String getName() {
  32. return name;
  33. }
  34. public void setName(String name) {
  35. this.name = name;
  36. }
  37. public int getAge() {
  38. return age;
  39. }
  40. public void setAge(int age) {
  41. this.age = age;
  42. }
  43. public String getAddress() {
  44. return address;
  45. }
  46. public void setAddress(String address) {
  47. this.address = address;
  48. }
  49. public Car getCar() {
  50. return car;
  51. }
  52. public void setCar(Car car) {
  53. this.car = car;
  54. }
  55. }

配置 Bean 引用

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <!-- 首先定义没有引用的 Bean -->
  6. <bean id="car" class="me.mritd.beans.Car">
  7. <property name="carName" value="四个圈"></property>
  8. </bean>
  9. <bean id="persion" name="persion" class="me.mritd.beans.Persion">
  10. <!-- 采用构造器注入(索引位置)的方式 -->
  11. <constructor-arg index="0" value="zhangsan"></constructor-arg>
  12. <constructor-arg index="1" value="10"></constructor-arg>
  13. <!-- 显示指定 某个属性值为null 使用 <null/>标签 -->
  14. <constructor-arg index="2"><null/></constructor-arg>
  15. <!-- 使用 ref 属性指定 引用 Bean -->
  16. <constructor-arg index="3" ref="car"></constructor-arg>
  17. </bean>
  18. <bean id="persion1" name="persion1" class="me.mritd.beans.Persion">
  19. <!-- 采用属性注入 -->
  20. <property name="name" value="zhangsan1"></property>
  21. <property name="age" value="11"></property>
  22. <property name="address" value="伊拉克"></property>
  23. <property name="car" ref="car"></property>
  24. </bean>
  25. </beans>

测试获取两个 Persion Bean,打印里面的 Car

  1. package me.mritd.test;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. import me.mritd.beans.Persion;
  6. /**
  7. *
  8. * Copyright © 2016 Mritd. All rights reserved.
  9. *
  10. * @ClassName: Test2
  11. * @Description: TODO
  12. * @author: 漠然
  13. * @date: 2016年1月2日 上午1:20:49
  14. */
  15. public class Test2 {
  16. @Test
  17. public void test2(){
  18. // 创建 ApplicationContext
  19. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
  20. // 获取Bean 测试
  21. Persion persion = (Persion) applicationContext.getBean("persion");
  22. Persion persion1 = (Persion) applicationContext.getBean("persion1");
  23. System.out.println(persion);
  24. System.out.println(persion1);
  25. }
  26. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注