1. 程式人生 > >Spring(23) 名稱空間

Spring(23) 名稱空間

  1. 為了精簡配置檔案,可以使用名稱空間~~
  2. p,也就是property,可以簡化bean的peoperty配置。具體看程式碼
    <?xml version="1.0" encoding="GBK"?>
    <!--需要引入p名稱空間-->
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           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.0.xsd">
    
        <bean id="test" class="MainPackage.MainClass" p:integerVariable="12"  p:classA-ref="referenceA" p:classB-ref="referenceB"/>
            <!--然後可以在bean中優雅地配置peoperty啦  classA-ref是引用其他bean  integerVariable是指定基本型別變數-->
    
    
        <bean id="referenceA" class="ReferenceClass.ReferenceClassA">
            <property name="thisIsA" value="this is a"/>
        </bean>
    
        <bean id="referenceB" class="ReferenceClass.ReferenceClassB">
            <property name="thisIsB" value="this is b"/>
        </bean>
    
    </beans>
    package MainPackage;
    
    import ReferenceClass.ReferenceClassA;
    import ReferenceClass.ReferenceClassB;
    
    public class MainClass {
        private ReferenceClassA classA;
        private ReferenceClassB classB;
        private int IntegerVariable;
    
        public int getIntegerVariable() {
            return IntegerVariable;
        }
    
        public void setIntegerVariable(int integerVariable) {
            IntegerVariable = integerVariable;
        }
    
        public ReferenceClassA getClassA() {
            return classA;
        }
    
        public void setClassA(ReferenceClassA classA) {
            this.classA = classA;
        }
    
        public ReferenceClassB getClassB() {
            return classB;
        }
    
        public void setClassB(ReferenceClassB classB) {
            this.classB = classB;
        }
    
        public void test(){
            System.out.println("thisIsA" + classA.getThisIsA());
            System.out.println("thisIsB" + classB.getThisIsB());
        }
    }
    
    package TestPackage;
    
    import MainPackage.MainClass;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class SpringTest {
        public static void main(String [] args){
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
            MainClass mainClass = applicationContext.getBean("test", MainClass.class);
            System.out.println(mainClass.getClassA().getThisIsA());
            System.out.println(mainClass.getClassB().getThisIsB());
        }
    }
    
    package ReferenceClass;
    
    public class ReferenceClassA {
        private String thisIsA;
    
        public String getThisIsA() {
            return thisIsA;
        }
    
        public void setThisIsA(String thisIsA) {
            this.thisIsA = thisIsA;
        }
    }
    
    package ReferenceClass;
    
    public class ReferenceClassB {
        private String thisIsB;
    
        public String getThisIsB() {
            return thisIsB;
        }
    
        public void setThisIsB(String thisIsB) {
            this.thisIsB = thisIsB;
        }
    }
    

     

  3. 名稱空間,為了精簡構造方法配置的,用法和p差不多,這是部分例子,其他的和上面是一樣的

    package MainPackage;
    
    import ReferenceClass.ReferenceClassA;
    import ReferenceClass.ReferenceClassB;
    
    public class MainClass {
        private ReferenceClassA classA;
        private ReferenceClassB classB;
        private int IntegerVariable;
    
    //    在這裡寫了構造方法,然後呢在配置檔案裡用c進行具體配置
        public MainClass(ReferenceClassA classA, ReferenceClassB classB, int IntegerVariable){
            this.classA = classA;
            this.classB = classB;
            this.IntegerVariable = IntegerVariable;
        }
    
        public int getIntegerVariable() {
            return IntegerVariable;
        }
    
        public void setIntegerVariable(int integerVariable) {
            IntegerVariable = integerVariable;
        }
    
        public ReferenceClassA getClassA() {
            return classA;
        }
    
        public void setClassA(ReferenceClassA classA) {
            this.classA = classA;
        }
    
        public ReferenceClassB getClassB() {
            return classB;
        }
    
        public void setClassB(ReferenceClassB classB) {
            this.classB = classB;
        }
    
        public void test(){
            System.out.println("thisIsA" + classA.getThisIsA());
            System.out.println("thisIsB" + classB.getThisIsB());
            System.out.println("this is it's int " + IntegerVariable);
        }
    }
    
    <?xml version="1.0" encoding="GBK"?>
    <!--需要引入名稱空間-->
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:c="http://www.springframework.org/schema/c"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
    	http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
    
        <bean id="test" class="MainPackage.MainClass" c:classA-ref="referenceA" c:classB-ref="referenceB" c:IntegerVariable="5"/>
            <!--然後可以在bean中優雅地配置peoperty啦  classA-ref是引用其他bean  integerVariable是指定基本型別變數-->
    
    
        <bean id="referenceA" class="ReferenceClass.ReferenceClassA">
            <property name="thisIsA" value="this is a"/>
        </bean>
    
        <bean id="referenceB" class="ReferenceClass.ReferenceClassB">
            <property name="thisIsB" value="this is b"/>
        </bean>
    
    </beans>

    c標籤還可以根據引數的位置進行賦值配置

    <bean id="test" class="MainPackage.MainClass" c:_0-ref="referenceA" c:_1-ref="referenceB" c:_2="5"/>

     

  4. 還有呢,可以通過util標籤來配置集合元素~   例子裡beans的結構是:在beans下配置好util,然後在一個bean中通過c標籤使用這些util

    <?xml version="1.0" encoding="GBK"?>
    <!-- 指定Spring配置檔案的根元素和Schema
    	匯入p:名稱空間和util:名稱空間的元素 -->
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:util="http://www.springframework.org/schema/util" xmlns:c="http://www.springframework.org/schema/c"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
    	http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    	http://www.springframework.org/schema/util
    	http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    
        <bean id="test" class="MainPackage.MainClass"
        c:classA-ref="referenceA" c:classB-ref="referenceB" c:IntegerVariable="5" c:testList-ref="test.testList"
              c:testMap-ref="test.testMap" c:testSet-ref="test.testSet"/>
            <!--然後可以在bean中優雅地配置peoperty啦  classA-ref是引用其他bean  integerVariable是指定基本型別變數-->
    
        <util:constant id="test.IntegerVariable" static-field="java.sql.Connection.TRANSACTION_NONE"/>
    
        
        <!--這就是util的配置啦,指定id,指定實現類,再指定它具體的值-->
        <util:list id="test.testList" list-class="java.util.LinkedList">
            <value>1</value>
            <value>2</value>
        </util:list>
    
    
        <util:map id="test.testMap" map-class="java.util.TreeMap">
            <entry key="a" value="A"/>
            <entry key="b" value="B"/>
        </util:map>
    
        <util:set id="test.testSet" set-class="java.util.HashSet">
            <value>aaa</value>
            <bean   class="java.util.Date"/>
            <ref bean="referenceB"/>
        </util:set>
    
        <bean id="referenceA" class="ReferenceClass.ReferenceClassA">
            <property name="thisIsA" value="this is a"/>
        </bean>
    
        <bean id="referenceB" class="ReferenceClass.ReferenceClassB">
            <property name="thisIsB" value="this is b"/>
        </bean>
    
    </beans>

     

  5. 這是我看李剛編著的《輕量級javaEE企業應用實戰(第五版)-Struts2+Spring5+Hibernate5/JAP2》後總結出來的。