zoukankan      html  css  js  c++  java
  • 【Spring】Spring之依赖注入(DI)传递参数的方式

    DI依赖注入传入参数的方式,这里介绍了基本数据类型,集合,符合数据类型的传递(String类型比较特殊,其传递值和基本数据类型的方法一致)

    注入基本数据类型和String类型

    通过setter方法注入基本数据类型与String

    案例:

    <bean id="book" class="cn.xdl.bean.Book">
    <!--
        对于基本数据类型 与 String的注入操作
    
        通过set方法 ,完成注入
        name: 属性的名称
        value: 属性要赋的值
        <null/>: 节点表示null。
     -->
        <property name="bookName" value="西游记"></property>
        <property name="bookId" value="10001"></property>
        <!-- null值 -->
        <property name="x">
            <null/>
        </property>
    
    </bean>

    通过构造方法 ,注入基本数据类型与String

        方式1:

        <bean id="book" class="cn.xdl.bean.Book">
               <!-- 通过构造器 传入形参名称 与 value值, 进行参数的注入-->
            <constructor-arg name="bn" value="红楼梦"></constructor-arg>
            <constructor-arg name="bi" value="10002"></constructor-arg>
        </bean>
     方式2:
      <bean id="book" class="cn.xdl.bean.Book">
               <!-- 通过构造器 传入形参列表的索引 ,与value进行参数的注入-->
             <constructor-arg index="0">
                <null/>
             </constructor-arg>
             <constructor-arg index="1" value="10003"></constructor-arg>    
        </bean>

    注入集合类型的数据

    注入List集合

                <property name="v">
                    <!--向List集合 注入值-->
                    <list>
                         <!--每一个value节点, 表示集合中的一个元素-->
                        <value>A</value>
                        <value>B</value>
                        <value>C</value>
                        <value>D</value>
                        <value>E</value>
                        <value>F</value>
                    </list>     
                </property>

    注入Set集合

                <property name="v">
                   <!--向set集合 注入值-->
                    <set>
                         <!--每一个value节点, 表示集合中的一个元素-->
                        <value>A</value>
                        <value>B</value>
                        <value>C</value>
                        <value>D</value>
                        <value>E</value>
                        <value>F</value>
                    </set>     
                </property>     

    注入Map集合

                <property name="v">
                    <map>
                        <!--每一个enrty表示集合中的一个键值对-->
                        <entry key="jiyou1" value="map1"></entry>
                        <entry key="jiyou2" value="map2"></entry>
                    </map>
                </property>

    注入Properties集合

                <property name="v">
                    <!--对于properties的注入 ,value是编写在prop节点的文本内容区域的
                    每一个prop都表示proerties存储的一个键值对的数据-->
                    <props>
                        <prop key="v1">A</prop>
                        <prop key="v2">B</prop>
                        <prop key="v3">C</prop>
                    </props>
                </property>

    通过引入的方式, 实现集合的重复利用

    原理:

        1.  配置一个集合对象到容器中, 并指定ID
        2.  在其他对象使用时, 直接通过对象注入的方式, 将其注入即可

    案例:

        向容器中添加集合对象:

            <util:set id="list">
                <value>张三</value>
                <value>李四</value>
                <value>王二</value>
                <value>麻子</value>
            </util:set>

        将对象注入其他对象中:

            <bean id="person" class="cn.xdl.bean.Person">
                <property name="refriendship" ref="list"></property>
            </bean>

    对象的注入

        通过Setter方法设置属性   

        property节点:  通过set方法, 设置一个属性的值
        name属性: 表示的是 当前类中的属性的名称
        ref: 表示的是, 本次要注入的对象在当前容器中的id值

        案例:

        <bean id="bookinstance" class="BookClass">
        <property name="book" ref="bookinstance"></property>
        </bean>

        通过构造器设置属性

        constructor节点: 通过构造器  , 注入属性的值
        name属性:  这是一个容易混淆的地方 , 它指的其实是构造方法中形式参数列表中参数的名称
        ref: 表示的是, 本次要注入的对象在当前容器中的id值

        案例:

        <bean id="bookinstance" class="BookClass">
        <constructor-arg name="book" ref="bookinstance"></constructor-arg>
        </bean>

        对象的自动装配

        我们可以在bean节点中, 添加autowire属性来完成自动装配的操作

        属性取值范围:

         1,no : 默认设置 , 表示关闭自动装配
         2,byName : 通过名称完成自动装配:
                例如: 当前我们容器中存在一个对象, id为book;
                    而刚好当前的这个person类中有一个名称为book的属性   
                    那么这个时候, 我们的容器, 会自动完成两个对象的关联
                    byName 不会判断传入的参数类型是否匹配, 只要名称相同, 就会强行建立依赖关系 , 导致报错!      

         3, byType : 通过类型完成自动装配

                例如: 当前我们容器中存在一个Book类的对象
                    而刚好我们当前的Person有一个Book类型的属性
                    我们的容器, 就会自动完成两个对象的关联


         4,constructor 与byType的方式类似,不同之处在于它应用于构造器参数
         5,autodetect  通过bean类来决定是使用constructor还是byType方式进行自动装配。如果发现默认的构造器,那么将使用byType方式
    案例:

    Person类:

    package cn.xdl.bean;
    
    public class Person {
    
        private String name;
        private int age;
        private Book book;
        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 Book getBook() {
            return book;
        }
        public void setBook(Book book) {
            this.book = book;
        }
        @Override
        public String toString() {
            return "这个人叫做:" + name + ", 今年" + age + "岁了, 他有一本书:" + book ;
        }
        public Person(String name, int age, Book book) {
            super();
            this.name = name;
            this.age = age;
            this.book = book;
        }
        public Person() {
            super();
            // TODO Auto-generated constructor stub
        }
        public Person(Book book) {
            super();
            this.book = book;
        }
        
        
    }
    Person.java

    applicationContext文件:

    <?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-4.1.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
            http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.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-4.1.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
        
        <bean id="book" class="cn.xdl.bean.Book">
            <property name="bookName" value="水浒传"></property>
            <property name="bookId" value="10001"></property>
        </bean>
        <!-- 
            <bean id="person" class="cn.xdl.bean.Person">
                通过ref  关联对象之间的关系
                <constructor-arg name="book">
                    <null/>
                </constructor-arg>
                <constructor-arg name="name" value="张三"></constructor-arg>
                <constructor-arg name="age" value="18"></constructor-arg>
                <property name="book" ref="book"></property>
            </bean>     
        -->
        <!-- 自动装配 byName 通过属性名称 与 容器中对象的id 匹配 -->
         <bean id="person" class="cn.xdl.bean.Person" autowire="byName">
             <property name="name" value="小李"></property>
             <property name="age" value="18"></property>
        </bean>     
    </beans>
    applicationContext.xml

    Test测试类:

    package cn.xdl.test;
    
    import org.junit.Test;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import cn.xdl.bean.Person;
    import cn.xdl.test.util.ContextUtil;
    
    public class Test {
        
        @Test
        public void test() throws Exception {
            ClassPathXmlApplicationContext context = ContextUtil.getC("bean6.xml");
            Person p= context.getBean("person", Person.class);
            System.out.println(p);//这个人叫做:小李, 今年23岁了, 他有一本书:书名=水浒传, 编号=10001
            
        }
    }
    Test.java

    扫描组件:

    扫描组件后,默认id值为类名首字母小写,也可以自定义id。

    下面是一些常用的@式,@后面的式可以写值,也可以不写,还有就是@Value(value="abc"),可以简写为@Value("abc")。

    @Compenent 通用注解

    @Repository 持久层组件注解

    @Service 业务层组件注解

    @Controller 控制层组件处理

    @Value 可以注入指定变量的值

    @Scope 可以指定对象的作用域singleton(单例模式,默认)、prototype(多例模式)、request、session、global Session,

    @Transactional 表示事务

    Spring表达式

    这种表达式在语法上和EL非常像,可以读取一个bean对象/集合中的数据

    例如:

    <!-- 进行扫描cn包 -->
    <context:component-scan base-package="cn"></context:component-scan>
    
    <!--从db.propertise文件中读取配置信息-->
    <util:properties id="db" location="classpath:db.properties"/>
    
    <bean id="demo"class="com.xdl.bean.OracleDataSource">
    <!--读取name的值-->
    <property name=“username" value="#{db.name}"/>
    <!--读取password-->
    <property name=“password"value="#{db.password}"/>
    <!--读取url的值-->
    <property name=“url" value="#{db.url}"/>
    <bean>
     
  • 相关阅读:
    activity 之间传递参数
    手动创建一个Activity,完成页面跳转(intent 无参数)
    C++中汉字字符串的截取
    android基础知识清单。
    更改远程仓库
    设计模式六大原则
    事件订阅代码
    Python Mac ssl.SSLError certificate verify failed (_ssl.c:833)
    Python库中常见的 __all__ 变量是干啥的
    Thrift的使用-Python
  • 原文地址:https://www.cnblogs.com/HDK2016/p/7150068.html
Copyright © 2011-2022 走看看