1. 程式人生 > >使用Spring框架入門二:基於註解+XML配置的IOC/DI的使用

使用Spring框架入門二:基於註解+XML配置的IOC/DI的使用

bsp hot sources hierarchy osi pan ioc clas operate

一、簡述

本文主要講使用註解+xml配合使用的幾種使用方式。基礎課程請看前一節。

二、步驟

1、為Pom.xml中引入依賴:本例中使用的是spring-context包,引入此包時系統會自動導入它的依賴包spring-beans\spring-core\spring-expression\spring-context.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>sindrol</groupId> <artifactId>SpringDemo</artifactId> <version>1.0-SNAPSHOT</version> <
dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.5.RELEASE</version> </dependency> <dependency> <
groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.0.5.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.20</version> </dependency> </dependencies> </project>

2、在/src/java/test2下建立下列類:

package test2;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

@Data
@Component(value = "product1")
//@Controller("product1") //Controller也是一個Component,一般用於WEB中的Action
//@Service("product1")//Service也是一個Component,一般用於業務邏輯類
//@Repository("product1")//Repository也是一個Component,一般用於持久層類
public class Product {
    @Value("Sindrol")
    private String name;
    @Value("1.5")
    private float price;
}
package test2;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("productOperate")
public class ProductOperate {
    //使用Resource也可以實現值的註入,使用Resource和Autowired的區別在於,Resource可以指定的名稱必須和要註入Comment名稱相同的對象,名稱不一致時出錯。
    @Resource(name = "product1")
    private Product product;
    @Autowired
    private Product product2;

    @Override
    public String toString() {
        return "ProductOperate(product=" + product + " ,product2=" + product2 + ")";
    }
}
package test2;

import jdk.nashorn.internal.objects.annotations.Constructor;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.PrimitiveIterator;

@Data
@Component("paramProduct1")
//@Scope("prototype")
@Scope("singleton")
public class ParamProduct {
    @Value("JAVA書集")
    private String name;
    private float price;
    private Product product;

    public ParamProduct(@Autowired Product product, @Value("Sindrol") String name, @Value("28") float price) {
        this.product = product;
        this.name = name;
        this.price = price;
    }

}
package test2;

import java.util.List;

public class ConstructProduct {
    private List<String> list;

    public ConstructProduct(List<String> list) {
        this.list = list;
    }

    @Override
    public String toString() {
        return "ConstructProduct(list=" + list + ")";
    }
}
package test2;

import org.springframework.context.annotation.ComponentScan;

//使用ComponentScan定義類所在包開頭名稱。
@ComponentScan(basePackages = "test2")
public class SamePackage {
}

3、在/src/test/resources(註意:標記此resources為測試資源)下建立applicationContextTest2.xml,內容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    <!--開啟註解掃描(掃描test2開頭的包)-->
    <context:component-scan base-package="test2"/>

    <!--引入配置文件,引入後值中可使用變量。引入方法一:-->
    <!--<context:property-placeholder location="config.properties" file-encoding="UTF-8"/>-->
    <!--引入配置文件,引入後值中可使用變量。引入方法二:-->
    <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="config.properties" />
        <property name="fileEncoding" value="UTF-8"/>
    </bean>

    <!--掃描屬性上面的註解-->
    <!--<context:annotation-config></context:annotation-config>-->
    <bean id="constructProduct" class="test2.ConstructProduct">
        <constructor-arg>
            <array>
                <!--這裏可以使用引入的properties文件的變量 -->
                <value>${configKey1}</value>
                <value>${configKey2}</value>
                <value>list_item3</value>
            </array>
        </constructor-arg>
    </bean>
</beans>

4、在/src/test/resources下建立一個名為config.properties的文件,內容如下:(這裏一定要註意文件的編碼,IDEA在Windows下默認新建的properties是ASCII編碼,所以要轉成UTF-8編碼後測試。)

#這是Key:Value結構。
configKey1:this is my value1.
configKey2:柱柱

5、在/src/test/java/BeanTest下添加Junit測試方法:

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import test1.Student;
import test1.User;
import test2.*;
import test3.Bird;

public class BeanTest {
  
    @Test
    public void test2() {
        //ApplicationContext context = new AnnotationConfigApplicationContext(SamePackage.class);
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContextTest2.xml");
        Product product1 = (Product) context.getBean("product1");
        System.out.println(product1);
    }

    @Test
    public void test3() {
        ApplicationContext context = new AnnotationConfigApplicationContext(SamePackage.class);
        ParamProduct paramProduct1 = (ParamProduct) context.getBean(ParamProduct.class, new Object[]{ //構造函數值
                context.getBean(Product.class),
                "Product初始名字",
                23
        });
        System.out.println(paramProduct1);
        ParamProduct paramProduct2 = (ParamProduct) context.getBean(ParamProduct.class, new Object[]{ //構造函數值
                context.getBean(Product.class),
                "Product初始名字2",
                333
        });
        System.out.println(paramProduct2); //註意,paramProduct2並未進行真實的構造,而是直接取了單例對象。
        System.out.println("是否是同一實例:" + (paramProduct1 == paramProduct1));
    }

    @Test
    public void test4() {
        ApplicationContext context = new AnnotationConfigApplicationContext(SamePackage.class);
        ProductOperate productOperate = context.getBean(ProductOperate.class);
        System.out.println(productOperate);
    }

    @Test
    public void test5() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContextTest2.xml");
        ConstructProduct constructProduct = context.getBean(ConstructProduct.class);
        System.out.println(constructProduct);
    }
   
}

6、執行測試,查看測試結果:

test2測試結果:

Product(name=Sindrol, price=1.5)

test3測試結果:

五月 06, 2018 12:40:40 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@10e92f8f: startup date [Sun May 06 12:40:40 CST 2018]; root of context hierarchy
ParamProduct(name=JAVA書集, price=28.0, product=Product(name=Sindrol, price=1.5))
ParamProduct(name=JAVA書集, price=28.0, product=Product(name=Sindrol, price=1.5))
是否是同一實例:true

test4測試結果:

五月 06, 2018 12:40:40 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@67a20f67: startup date [Sun May 06 12:40:40 CST 2018]; root of context hierarchy
五月 06, 2018 12:40:40 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@31368b99: startup date [Sun May 06 12:40:40 CST 2018]; root of context hierarchy
五月 06, 2018 12:40:40 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContextTest2.xml]
ProductOperate(product=Product(name=Sindrol, price=1.5) ,product2=Product(name=Sindrol, price=1.5))

test5測試結果:

ConstructProduct(list=[this is my value1., 柱柱, list_item3])

三、測試屬性文件以Map的形式載入

1、在/src/test/java/test3下建立一個Bird類

package test3;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class Bird {
    @Value("#{configProperties[‘configKey2‘]}")
    private String configKey2;
    @Value("#{configProperties}")
    private Map<String, String> properties;

    @Override
    public String toString() {
        return "Bird(properties=" + properties + ",configKey2=" + configKey2 + ")";
    }
}

2、在/src/test/resources下建立一個applicationContextTest3.xml,內容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="test3"/>
    <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>*.properties</value>
            </list>
        </property>
        <property name="fileEncoding" value="UTF-8"/>
    </bean>
</beans>

3、在/src/test/java/BeanTest中添加test6測試方法:

    @Test
    public void test6() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContextTest3.xml");
        Bird bird = context.getBean(Bird.class);
        System.out.println(bird);
    }

4、運行查看結果:

五月 06, 2018 12:40:41 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@795cd85e: startup date [Sun May 06 12:40:41 CST 2018]; root of context hierarchy
五月 06, 2018 12:40:41 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContextTest3.xml]
Bird(properties={configKey1=this is my value1., #這是Key=Value結構。, configKey2=柱柱},configKey2=柱柱)

技術分享圖片

使用Spring框架入門二:基於註解+XML配置的IOC/DI的使用