zoukankan      html  css  js  c++  java
  • Java Spring学习笔记----Bean的依赖注入(设值注入方式)1

    Spring常用的两种依赖注入方式:一种是设值注入方式,利用Bean的setter方法设置Bean的属性值;另一种是构造注入,通过给Bean的构造方法传递参数来实现Bean的属性赋值;

    1、设值注入方式

    直接上代码例子,示例的树结构图如下

     Shape.java接口内容

    package chapter3;
    
    public interface Shape {
        public double area();//求形状的面积
    }    

    Circle.java内容:

    package chapter3;
    
    public class Circle implements Shape {
    
        double r;
        public double getR(){
            return this.r;
        }
        public void setR(double r){
            this.r=r;
        }
        
        @Override
        public double area() {
            // TODO Auto-generated method stub
            return Math.PI*Math.pow(r, 2);
        }
    
    }

    Rectangle.java内容

    package chapter3;
    
    public class Rectangle implements Shape {
    
        double width,height;
        
        public double getWidth(){
            return this.width;
        }
        public void setWidth(double width){
            this.width=width;
        }
        
        public double getHeight(){
            return this.height;
        }
        public void setHeight(double height){
            this.height=height;
        }
        
        @Override
        public double area() {
            // TODO Auto-generated method stub
            return width*height;
        }
    
    }

    AnyShape.java内容

    package chapter3;
    
    public class AnyShape {
        Shape shape;
        public void setShape(Shape shape) {this.shape=shape;}
        public Shape getShape(){return this.shape;}
        public void outputArea(){
            System.out.println("面积="+shape.area());
        }
    }

    Test.java内容

    package chapter3;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.*;
    
    public class Test {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            ApplicationContext context=new FileSystemXmlApplicationContext("src/myContext.xml");
            AnyShape shape=(AnyShape)context.getBean("anyShape");
            shape.outputArea();
            
            AnyShape shape2=(AnyShape)context.getBean("anyShape2");
            shape2.outputArea();
        }
    
    }

    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-2.5.xsd">
               
               <bean id="myShape" class="chapter3.Circle">
               <property name="R">
               <value>2.5</value>
               </property>
               </bean>
               
               <bean id="myShape2" class="chapter3.Rectangle">
               <property name="height">
               <value>2</value>
               </property>
               <property name="width">
               <value>5</value>
               </property>
               </bean>
               
               <bean id="anyShape" class="chapter3.AnyShape">
               <property name="shape">
               <ref bean="myShape"/>
               </property>
               </bean>
               
                <bean id="anyShape2" class="chapter3.AnyShape">
               <property name="shape">
               <ref bean="myShape2"/>
               </property>
               </bean>
    
    </beans>

     在配置文件中,每个Bean的id属性定义Bean的标识。查找和引用Bean是通过该标识进行的。Bean的class属性顶了Bean对应的类和路径名称。通过Bean的子元素property实现属性值的设置,它是通过调用相应属性的setter方法实现属性值的注入。标识为“anyshape”的Bean在设置shape的属性时通过<ref/>标签引用了标识为“myshape”的bean,也就是shape属性由标识为“myshape”的Bean决定的。

  • 相关阅读:
    message:"iconv(): Detected an illegal character in input string"
    VM Mac OS 无法开机
    你的爱 歌词
    大脑结构 | 前脑 | 中脑 | 后脑 | 脑干 | 大脑发育
    【分布计算环境笔记】10 SOA、网格计算、云计算与P2P技术
    【职场Tips】Language in IBM——献给所有即将成为IBMer的童鞋
    【系统工程师的自我修养】sed篇
    【Java学习笔记】如何写一个简单的Web Service
    Unison(双向同步软件)的安装与配置【转】
    发生java.lang.OutOfMemoryError: Direct buffer memory【转】
  • 原文地址:https://www.cnblogs.com/zyfadmin/p/8988360.html
Copyright © 2011-2022 走看看