首页 > 其他 > 详细

Unit02: 参数值注入 、 基于注解的组件扫描

时间:2017-03-02 00:17:49      阅读:448      评论:0      收藏:0      [点我收藏+]

    Unit02: 参数值注入 、 基于注解的组件扫描    

(4)IOC (Inversion Of Controll 控制反转)

什么是IOC?

对象之间的依赖关系由容器来建立。

什么是DI? (Dependency Injection 依赖注入)

容器通过调用set方法或者构造器来建立对象之间的依赖关系。
注: IOC是目标,而DI是手段。

 

技术分享

依赖注入的两种方式

方式一 set方法注入。

step1. 添加set方法。

技术分享

step2. 配置set方法注入。

技术分享

方式二 构造器注入。

step1. 添加构造器。

技术分享

step2. 配置构造器注入。

技术分享

自动装配 (了解)

什么是自动装配?

容器依据某些规则,自动建立对象之间的依赖关系。
注:默认情况下,容器禁止自动装配。

技术分享

注入基本类型的值

使用value属性来注入。

注入集合类型的值

List,Set,Map,Properties

引用的方式注入集合类型的值

技术分享

读取properties文件的内容

技术分享

使用Spring表达式

技术分享

案例:

 

技术分享

auto

技术分享
package auto;

public class Restaurant {
    private Waiter wt;
    
    public void setWt(Waiter wt) {
        System.out.println("setWt()");
        this.wt = wt;
    }

    public Restaurant() {
        System.out.println("Restaurant");
    }

    @Override
    public String toString() {
        return "Restaurant [wt=" + wt + "]";
    }

}
Restaurant.java
技术分享
package auto;

public class Waiter {

    public Waiter() {
        System.out.println("Waiter()");
    }
}
Waiter.java

ioc

技术分享
package ioc;

public class A {
    private B b;
    
    public A() {
        System.out.println("A()");
    }
    
    public A(B b) {
        System.out.println("A(B)");
        this.b = b;
    }

    public void service(){
        System.out.println("A‘s service()");
        b.f1();
    }
    
}
A.java
技术分享
package ioc;

public class B {

    public B() {
        System.out.println("B()");
    }
    
    public void f1(){
        System.out.println("B‘s f1()");
    }
    
}
B.java

value

技术分享
package value;

public class SpelBean {
    private String name;
    private String interest;
    private double score;
    private int pageSize;
    
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public SpelBean() {
        System.out.println("SpelBean");
    }

    public void setScore(double score) {
        this.score = score;
    }

    public void setInterest(String interest) {
        this.interest = interest;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "SpelBean [name=" + name + ", interest=" + interest + ", score=" + score + ", pageSize=" + pageSize
                + "]";
    }
    
    
    
}
SpelBean.java
技术分享
package value;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class ValueBean {
    private String name;
    private int age;
    private List<String> interest;
    private Set<String> city;
    private Map<String,Double> score;
    private Properties db;
    
    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }
    public List<String> getInterest() {
        return interest;
    }
    public Set<String> getCity() {
        return city;
    }
    public Map<String, Double> getScore() {
        return score;
    }
    public Properties getDb() {
        return db;
    }
    public void setInterest(List<String> interest) {
        this.interest = interest;
    }
    public void setDb(Properties db) {
        this.db = db;
    }
    public void setCity(Set<String> city) {
        this.city = city;
    }
    public void setScore(Map<String, Double> score) {
        this.score = score;
    }
    public ValueBean() {
        System.out.println("ValueBean()");
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "ValueBean [name=" + name + ", age=" + age + ", interest=" + interest + ", city=" + city + ", score="
                + score + ", db=" + db + "]";
    }
    
    
}
ValueBean.java

src/main/resource

技术分享
<?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" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    <bean id="wt2" class="auto.Waiter"/>
    <!-- 
        autowire指定自动装配的规则,有这样三个值:
        byName: 容器依据属性名查找对应的bean(即
        bean的id等于属性名),找到之后,调用对应的
        set方法来完成注入。
            注:如果找不到,会注入null值。
        byType:容器依据属性类型查找对应的bean(即
        bean的类型与属性类型一致),找到之后,调用对
        应的set方法来完成注入。
            注:如果找不到,会注入null值。
                如果找到多个,会出错。
        constructor:类似byType,只不过会调用构造器
        来完成注入。
     -->
    <bean id="rest" class="auto.Restaurant" 
    autowire="byType"/>
</beans>
auto.xml
技术分享
pagesize=10
config.properties
技术分享
<?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" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    <bean id="b1" class="ioc.B"/>
    <!--
        构造器方式注入。
        index属性:指定参数的下标(从0开始) 
     -->
    <bean id="a1" class="ioc.A">
        <constructor-arg index="0" ref="b1"/>
    </bean>
    
</beans>
ioc.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" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    <!-- 
        使用Spring表达式读取其它bean的属性值。
     -->
    <bean id="sb1" class="value.SpelBean">
        <property name="name" 
        value="#{vb1.name}"/>
        <property name="interest"
        value="#{vb1.interest[0]}"/>
        <property name="score"
        value="#{vb1.score[‘英语‘]}"/>
        <property name="pageSize"
        value="#{config.pagesize}"/>
    </bean>
    
    <bean id="vb1" class="value.ValueBean">
        <property name="name" value="小月"/>
        <property name="age" value="22"/>
        <property name="interest">
            <list>
                <value>钓鱼</value>
                <value>旅游</value>
                <value>看电视</value>
                <value>看电视</value>
            </list>
        </property>
        <property name="city">
            <set>
                <value>北京</value>
                <value>长沙</value> 
                <value>南京</value>
            </set>
        </property>
        <property name="score">
            <map>
                <entry key="英语" value="60"/>
                <entry key="math" value="80"/>
            </map>
        </property>
        <property name="db">
            <props>
                <prop key="username">King</prop>
                <prop key="password">1234</prop>
            </props>
        </property>
    </bean>
    
    <!-- 引用的方式注入集合类型的值 -->
    <util:list id="interestBean">
        <value>钓鱼</value>
        <value>旅游</value>
        <value>上网</value>
    </util:list>
    <util:set id="cityBean">
        <value>北京</value>
        <value>上海</value>
        <value>武汉</value>
    </util:set>
    <util:map id="scoreBean">
        <entry key="english" value="80"/>
        <entry key="math" value="90"/>
    </util:map>
    <util:properties id="dbBean">
        <prop key="username">King</prop>
        <prop key="password">1234</prop>
    </util:properties>
    <bean id="vb2" class="value.ValueBean">
        <property name="interest" 
        ref="interestBean"/>
        <property name="city"
        ref="cityBean"/>
        <property name="score"
        ref="scoreBean"/>
        <property name="db"
        ref="dbBean"/>
    </bean>
    
    <!-- 读取properties文件的内容 -->
    <!-- 
        location属性:指定要 读取的文件的位置,
        其中,classpath:表示依据类路径去查找。
     -->
    <util:properties id="config" 
    location="classpath:config.properties"/>
    
    
    
</beans>
value.xml

src/test/java

test

技术分享
package test;

import java.util.Properties;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import auto.Restaurant;
import ioc.A;
import value.SpelBean;
import value.ValueBean;

public class TestCase {
    @Test
    // 测试 构造器方式的注入
    public void test1() {
        // 启动Spring容器
        String config = "ioc.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(config);
        // 获得对象
        A a1 = ac.getBean("a1", A.class);
        a1.service();
    }

    @Test
    // 测试 自动装配
    public void test2() {
        // 启动Spring容器
        String config = "auto.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(config);
        // 获得对象
        Restaurant rest = ac.getBean("rest", Restaurant.class);
        System.out.println(rest);
    }

    @Test
    // 测试 注入基本类型的值
    public void test3() {
        // 启动Spring容器
        String config = "value.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(config);
        ValueBean vb1 = ac.getBean("vb2", ValueBean.class);
        System.out.println(vb1);
    }

    @Test
    // 读取properties文件
    public void test4() {
        // 启动Spring容器
        String config = "value.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(config);
        Properties props = ac.getBean("config", Properties.class);
        System.out.println(props);
    }

    @Test
    // 测试 Spring表达式
    public void test5() {
        // 启动Spring容器
        String config = "value.xml";
        ApplicationContext ac =
            new ClassPathXmlApplicationContext(
                    config);
        SpelBean sb1 = 
                ac.getBean("sb1",SpelBean.class);
        System.out.println(sb1);
    }
}
TestCase.java

 

Unit02: 参数值注入 、 基于注解的组件扫描

原文:http://www.cnblogs.com/tangshengwei/p/6487081.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!