spring-配置详解

Spring

Spring 配置详解!

一、Bean元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<!-- 将ComboList对象交给spring容器管理 -->
<!-- Bean元素:使用该元素描述需要spring容器管理的对象
class属性:被管理对象的完整类名.
name属性:给被管理的对象起个名字.获得对象时根据该名称获得对象.
可以重复.可以使用特殊字符.
id属性: 与name属性一模一样.
名称不可重复.不能使用特殊字符.
结论: 尽量使用name属性.
-->
<bean name="comboList" class="com.ccblogs.spring.ComboList" ></bean>
</beans>

二、创建对象方式

  • ComboList.java类:

    1
    2
    3
    4
    5
    public class ComboList {
    public ComboList() {
    System.out.println("无参构造方法创建对象");
    }
    }
  • ComboListFactory.java类:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    public class ComboListFactory {
    public ComboListFactory() {
    // TODO Auto-generated constructor stub
    }
    public static ComboList createComboList() {
    System.out.println("静态工厂创建");
    return new ComboList();
    }
    public ComboList CreateComboList2() {
    System.out.println("实例工厂创建");
    return new ComboList();
    }
    }
  • applicationContext.xml配置文件:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd ">
    <!-- 无参构造函数创建对象 -->
    <bean name="comboList" class="com.ccblogs.spring.ComboList"></bean>
    <!-- 静态工厂创建对象 -->
    <bean name="comboList2" class="com.ccblogs.spring.ComboListFactory"
    factory-method="createComboList"></bean>
    <!-- 实例工厂创建对象 -->
    <bean name="comboList3" factory-bean="createComboList2"
    factory-method="CreateComboList2"></bean>
    <bean name="createComboList2" class="com.ccblogs.spring.ComboListFactory"></bean>
    </beans>
  • ComboListTest.java类:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class ComboListTest {
    @SuppressWarnings("resource")
    @Test
    public static void main(String[] args) {
    ApplicationContext cont = new ClassPathXmlApplicationContext("applicationContext.xml");
    Object obj = cont.getBean("comboList");
    System.out.println(obj);
    Object obj2 = cont.getBean("comboList2");
    System.out.println(obj2);
    Object obj3 = cont.getBean("comboList3");
    System.out.println(obj3);
    }
    }

三、scope属性

  • singleton,默认值,单例对象。在spring容器中只会存在一个实例。使用方式:

    1
    <bean name="comboList" class="com.ccblogs.spring.ComboList" scope="singleton"></bean>
  • prototype,多例的对象。每次再获得都会创建新的对象,spring管理Struts2时,ActionBean必须配置为多例的。

    1
    <bean name="comboList" class="com.ccblogs.spring.ComboList" scope="prototype"></bean>
  • request,web环境下,对象与request生命周期一致。

  • session,web环境下,对象与session生命周期一致。

四、初始化和销毁方法

有错误!(cvc-complex-type.3.2.2: Attribute ‘destory-method’ is not allowed to appear in element ‘bean’.)

  • ComboList.java:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public class ComboList {
    public ComboList() {
    System.out.println("无参构造方法创建对象");
    }
    public void getInit() {
    System.out.println("初始化方法");
    }
    public void getDestory() {
    System.out.println("销毁方法");
    }
    }
  • 使用init-method属性标注初始化方法,使用destory-method属性标注销毁方法,applicationContext.xml:

    1
    2
    3
    4
    5
    6
    7
    8
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd ">
    <bean name="comboList" class="com.ccblogs.spring.ComboList"
    init-method="getInit" destory-method="getDestory"></bean>
    </beans>
  • ComboListTest.java:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    import org.junit.Test;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class ComboListTest {
    @Test
    public static void main(String[] args) {
    ClassPathXmlApplicationContext cont = new ClassPathXmlApplicationContext("applicationContext.xml");
    Object obj = cont.getBean("comboList");
    System.out.println(obj);
    cont.close();
    }
    }

五、模块化配置

  • ComboList.java:

    1
    2
    3
    4
    5
    6
    7
    8
    public class ComboList {
    public ComboList() {
    System.out.println("无参构造方法创建对象");
    }
    public void getInit() {
    System.out.println("初始化方法");
    }
    }
  • applicationContext.xml:

    1
    2
    3
    4
    5
    6
    7
    8
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd ">
    <bean name="comboList" class="com.ccblogs.spring.ComboList"
    init-method="getInit"></bean>
    </beans>
  • 使用import标签导入其他配置文件,applicationContext1.xml:

    1
    2
    3
    4
    5
    6
    7
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd ">
    <import resource="applicationContext.xml"></import>
    </beans>
  • ComboListTest.java:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    import org.junit.Test;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class ComboListTest {
    @Test
    public static void main(String[] args) {
    ClassPathXmlApplicationContext cont = new ClassPathXmlApplicationContext("applicationContext1.xml");
    Object obj = cont.getBean("comboList");
    System.out.println(obj);
    cont.close();
    }
    }

六、属性注入(set方式)

  • ComboList.java:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    public class ComboList {
    private String name;
    private int age;
    private Like like;
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public int getAge() {
    return age;
    }
    public void setAge(int age) {
    this.age = age;
    }
    public Like getLike() {
    return like;
    }
    public void setLike(Like like) {
    this.like = like;
    }
    @Override
    public String toString() {
    return "ComboList [name=" + name + ", age=" + age + ", like=" + like + "]";
    }
    }
  • Like.java:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    public class Like {
    private String like1;
    private String like2;
    public Like() {
    // TODO Auto-generated constructor stub
    }
    public String getLike1() {
    return like1;
    }
    public void setLike1(String like1) {
    this.like1 = like1;
    }
    public String getLike2() {
    return like2;
    }
    public void setLike2(String like2) {
    this.like2 = like2;
    }
    @Override
    public String toString() {
    return "Like [like1=" + like1 + ", like2=" + like2 + "]";
    }
    }
  • applicationContext.xml:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd ">
    <bean name="comboList" class="com.ccblogs.spring.ComboList">
    <property name="name" value="ccblogs"></property>
    <property name="age" value="18"></property>
    <property name="like" ref="like"></property>
    </bean>
    <bean name="like" class="com.ccblogs.spring.Like">
    <property name="like1" value="like11"></property>
    <property name="like2" value="like22"></property>
    </bean>
    </beans>
  • ComboListTest.java:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class ComboListTest {
    @SuppressWarnings("resource")
    @Test
    public static void main(String[] args) {
    ApplicationContext cont = new ClassPathXmlApplicationContext("applicationContext.xml");
    Object obj = cont.getBean("comboList");
    System.out.println(obj);
    }
    }

七、属性注入(构造函数)

  • ComboList.java:

    1
    2
    3
    4
    5
    6
    7
    8
    public class ComboList {
    public ComboList(String name, Integer age) {
    System.out.println(String.format("name=%s,age=%d", name, age));
    }
    public ComboList(Integer name, String age) {
    System.out.println(String.format("name=%d,age=%s", name, age));
    }
    }
  • applicationContext.xml:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd ">
    <bean name="comboList" class="com.ccblogs.spring.ComboList">
    <constructor-arg name="name" value="18" index="0" type="java.lang.Integer"></constructor-arg>
    <constructor-arg name="age" value="ccblogs" index="1" type="java.lang.String"></constructor-arg>
    </bean>
    </beans>
  • ComboListTest.java:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class ComboListTest {
    @SuppressWarnings("resource")
    @Test
    public static void main(String[] args) {
    ApplicationContext cont = new ClassPathXmlApplicationContext("applicationContext.xml");
    Object obj = cont.getBean("comboList");
    System.out.println(obj);
    }
    }

八、属性注入(p名称空间&SPEL表达式)

  1. p名称空间:

    • ComboList.java:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      public class ComboList {
      private String name;
      private int age;
      private Like like;
      public String getName() {
      return name;
      }
      public void setName(String name) {
      this.name = name;
      }
      public int getAge() {
      return age;
      }
      public void setAge(int age) {
      this.age = age;
      }
      public Like getLike() {
      return like;
      }
      public void setLike(Like like) {
      this.like = like;
      }
      @Override
      public String toString() {
      return "ComboList [name=" + name + ", age=" + age + ", like=" + like + "]";
      }
      }
    • Like.java:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      public class Like {
      private String like1;
      private String like2;
      public Like() {
      // TODO Auto-generated constructor stub
      }
      public String getLike1() {
      return like1;
      }
      public void setLike1(String like1) {
      this.like1 = like1;
      }
      public String getLike2() {
      return like2;
      }
      public void setLike2(String like2) {
      this.like2 = like2;
      }
      @Override
      public String toString() {
      return "Like [like1=" + like1 + ", like2=" + like2 + "]";
      }
      }
    • applicationContext.xml:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd ">
      <bean name="comboList" class="com.ccblogs.spring.ComboList"
      p:name="ccblogs" p:age="18" p:like-ref="like"></bean>
      <bean name="like" class="com.ccblogs.spring.Like">
      <property name="like1" value="like11"></property>
      <property name="like2" value="like22"></property>
      </bean>
      </beans>
    • ComboListTest.java:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      import org.junit.Test;
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.support.ClassPathXmlApplicationContext;
      public class ComboListTest {
      @SuppressWarnings("resource")
      @Test
      public static void main(String[] args) {
      ApplicationContext cont = new ClassPathXmlApplicationContext("applicationContext.xml");
      Object obj = cont.getBean("comboList");
      System.out.println(obj);
      }
      }
  2. SPEL表达式:

    • ComboList.java:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      public class ComboList {
      private String name;
      private int age;
      private Like like;
      public String getName() {
      return name;
      }
      public void setName(String name) {
      this.name = name;
      }
      public int getAge() {
      return age;
      }
      public void setAge(int age) {
      this.age = age;
      }
      public Like getLike() {
      return like;
      }
      public void setLike(Like like) {
      this.like = like;
      }
      @Override
      public String toString() {
      return "ComboList [name=" + name + ", age=" + age + ", like=" + like + "]";
      }
      }
    • Like.java:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      public class Like {
      private String like1;
      private String like2;
      public Like() {
      // TODO Auto-generated constructor stub
      }
      public String getLike1() {
      return like1;
      }
      public void setLike1(String like1) {
      this.like1 = like1;
      }
      public String getLike2() {
      return like2;
      }
      public void setLike2(String like2) {
      this.like2 = like2;
      }
      @Override
      public String toString() {
      return "Like [like1=" + like1 + ", like2=" + like2 + "]";
      }
      }
    • applicationContext.xml:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd ">
      <bean name="comboList" class="com.ccblogs.spring.ComboList">
      <property name="name" value="#{comboList1.name}"></property>
      <property name="age" value="#{comboList2.age}"></property>
      <property name="like" ref="like"></property>
      </bean>
      <bean name="comboList1" class="com.ccblogs.spring.ComboList"
      p:name="ccblogs1" p:age="181" p:like-ref="like">
      </bean>
      <bean name="comboList2" class="com.ccblogs.spring.ComboList">
      <property name="name" value="ccblogs2"></property>
      <property name="age" value="182"></property>
      <property name="like" ref="like"></property>
      </bean>
      <bean name="like" class="com.ccblogs.spring.Like">
      <property name="like1" value="like11"></property>
      <property name="like2" value="like22"></property>
      </bean>
      </beans>
    • ComboListTest.java:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      import org.junit.Test;
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.support.ClassPathXmlApplicationContext;
      public class ComboListTest {
      @SuppressWarnings("resource")
      @Test
      public static void main(String[] args) {
      ApplicationContext cont = new ClassPathXmlApplicationContext("applicationContext.xml");
      Object obj = cont.getBean("comboList");
      System.out.println(obj);
      }
      }

九、复杂类型注入

  • ComboList.java:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    import java.util.Arrays;
    import java.util.List;
    import java.util.Map;
    import java.util.Properties;
    public class ComboList {
    private Object[] strs;
    private List lst;
    private Map map;
    private Properties pro;
    public Object[] getStrs() {
    return strs;
    }
    public void setStrs(Object[] strs) {
    this.strs = strs;
    }
    public List getLst() {
    return lst;
    }
    public void setLst(List lst) {
    this.lst = lst;
    }
    public Map getMap() {
    return map;
    }
    public void setMap(Map map) {
    this.map = map;
    }
    public Properties getPro() {
    return pro;
    }
    public void setPro(Properties pro) {
    this.pro = pro;
    }
    @Override
    public String toString() {
    return "ComboList [strs=" + Arrays.toString(strs) + ", lst=" + lst + ", map=" + map + ", pro=" + pro + "]";
    }
    }
  • Like.java:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    public class Like {
    private String like1;
    private String like2;
    public Like() {
    // TODO Auto-generated constructor stub
    }
    public String getLike1() {
    return like1;
    }
    public void setLike1(String like1) {
    this.like1 = like1;
    }
    public String getLike2() {
    return like2;
    }
    public void setLike2(String like2) {
    this.like2 = like2;
    }
    @Override
    public String toString() {
    return "Like [like1=" + like1 + ", like2=" + like2 + "]";
    }
    }
  • applicationContext.xml:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd ">
    <bean name="comboList" class="com.ccblogs.spring.ComboList">
    <property name="strs">
    <array>
    <value>ccblogs</value>
    <value>ccblogs1</value>
    <ref bean="like" />
    </array>
    </property>
    <property name="lst">
    <list>
    <value>ccblogs</value>
    <value>ccblogs1</value>
    <ref bean="like" />
    </list>
    </property>
    <property name="map">
    <map>
    <entry key="key1" value="ccblogs1">
    </entry>
    <entry key="key1" value="ccblogs2">
    </entry>
    <entry key-ref="like" value-ref="like">
    </entry>
    </map>
    </property>
    <property name="pro">
    <props>
    <prop key="key1">ccblogs1</prop>
    <prop key="key2">ccblogs2</prop>
    </props>
    </property>
    </bean>
    <bean name="like" class="com.ccblogs.spring.Like">
    <property name="like1" value="like11"></property>
    <property name="like2" value="like22"></property>
    </bean>
    </beans>
  • ComboListTest.java:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class ComboListTest {
    @SuppressWarnings("resource")
    @Test
    public static void main(String[] args) {
    ApplicationContext cont = new ClassPathXmlApplicationContext("applicationContext.xml");
    Object obj = cont.getBean("comboList");
    System.out.println(obj);
    }
    }