zoukankan      html  css  js  c++  java
  • Spring(二)DI( Dependency Injection依赖注入)

    1.setter注入

    ①:bean类:

    public class DITest implements IDIBeanDAO{
        private String name;
        private int age;
        private Double fees;
        private Set<DITest> beans;
    //省略getter/setter方法
    }

    ②: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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd">
        <bean  id="setterBean1" class="com.gxxy.spring_04annotation.di.setter.DITest">
            <property name="name" value="LiSi"></property>
            <property name="age" value="20"></property>
            <property name="fees" value="134.2"></property>
            
        </bean>
        
        <bean  id="setterBean" class="com.gxxy.spring_04annotation.di.setter.DITest">
            <property name="name" value="ZhangSan"></property>
        <property name="age" value="18"></property> 
            <property name="fees" value="1826.3"></property>
            <property name="beans">
                <set>   SET的写法
                    <ref bean="setterBean1" />   
                </set>
                <!-- <property name="beans">
                <list>   list的写法
                    <ref bean=""/>
                </list>
                </property>
                
                <property name="testMap">
                <map>    map的写法
                    <entry key="" value=""/>
                    <entry key-ref="" value-ref="" />
                </map>
            </property> -->
            </property>
        </bean>
    </beans>

    2.Construct注入

    ①:Bean类:

    public class DITest implements IDIBeanDAO{
    
        private String name;
        private int age;
        private Double fees;
        public DITest() {}
        public DITest(String name, int age, Double fees) {
            this.name = name;
            this.age = age;
            this.fees = fees;
        }
    }
    //省略gettersetter 方法    

    ②: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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd">
        
        <bean  id="constructBean" class="com.gxxy.spring_04annotation.di.construct.DITest">
        <!-- <constructor-arg name="name" value="ZhangSan"></constructor-arg>        
        <constructor-arg name="age" value="18"></constructor-arg>        
        <constructor-arg name="fees" value="1235.2"></constructor-arg>     -->    
    <constructor-arg index="0" value="1235.2"></constructor-arg> <constructor-arg index="1" value="12"></constructor-arg> <constructor-arg index="2" value="1235.2"></constructor-arg> </bean> </beans>
  • 相关阅读:
    android 通过canvas旋转 绘制文字 竖直等不同方向的显示
    [转]在.NET环境中实现每日构建(Daily Build)NAnt篇
    [转]如何让Android字体自适应屏幕分辨率
    [转]调用相机并将照片存储到sd卡上
    [转]在.NET环境中实现每日构建(Daily Build)ccnet,MSBuild篇
    [转]使用ANT打包Android应用
    [转]通过创建一个位图的XY Chart来学习Android绘图类Rect,Paint,Bitmap,Canvas(附源码)
    [转]android webview学习
    [转]敏捷开发中编写高质量Java代码
    MSDN Visual C++ 开发中心
  • 原文地址:https://www.cnblogs.com/zhang-bo/p/6628145.html
Copyright © 2011-2022 走看看