spring使用案例 属性注入

来源:互联网 发布:notepad是什么软件 编辑:程序博客网 时间:2024/05/23 15:07

构造方法注入

1、新建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="bean" class="spring_day_2.TastDome">
          <constructor-arg type="int" value="7500000"/>
        </bean>
        <bean id="bean2" class="spring_day_2.TastDome">
        <!--name的值必须和构造方法中的参数名一致  -->
          <constructor-arg name="val" value="7500000"/>
        </bean>
         <bean id="bean3" class="spring_day_2.TastDome">
            <!--index必须和构造方法中的参数位置一致  -->
          <constructor-arg index="0" value="7500000"/>
        </bean>
        
        <!-- 构造方法参数是类的实例
                    注意 如果声明了有参数的构造方法 那么必须自己实现一个无参数的不然会报
                    Failed to instantiate [spring_day_2.TastDome]: No default constructor found; nested exception is java.lang.NoSuchMethodException: spring_day_2.TastDome.<init>()
         -->
          <bean id="tastDome" class="spring_day_2.TastDome"/>
          <bean id="bean4" class="spring_day_2.TastDome2">
                  <constructor-arg   ref="tastDome"/>
        </bean>
  </beans>

2、新建对应实体类

package spring_day_2;

public class TastDome {
        private int value;
        public void add(){
            System.out.println(value);
        }
        /**
         * 无参数的构造
         */
        public TastDome(){
        }
        public TastDome(int val){
            this.value=val;
        }
}

package spring_day_2;

public class TastDome2 {
        private TastDome tastDome;
        public void add(){
            tastDome.add();
        }
        public TastDome2(TastDome tastDome ){
            this.tastDome=tastDome;
        }
}

3、使用

package spring_day_2;

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

public class Spring_dome {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext context=new ClassPathXmlApplicationContext("MyXml2.xml");
        TastDome user=(TastDome) context.getBean("bean");
        user.add();
        
        user=(TastDome) context.getBean("bean2");
        user.add();
        user=(TastDome) context.getBean("bean3");
        user.add();
        TastDome2    user2=(TastDome2) context.getBean("bean4");
        user2.add();
    }

}

属性注入

1、新建类

package spring_day_2;

public class TastDome3 {
        private TastDome tastDome;
        private int val;
        public void add(){
            System.out.println(val);
            tastDome.add();
        }
        public void setTastDome(TastDome tastDome) {
            this.tastDome = tastDome;
        }
        public void setVal(int val) {
            this.val = val;
        }
        
}

2、编辑xml

  <!-- set注入 -->
             <bean id="bean5" class="spring_day_2.TastDome3">
                    <property name="tastDome"  ref="tastDome"/>
                    <property name="val" value="1"/>
             </bean>
             
            

3、使用

    TastDome3    user3=(TastDome3) context.getBean("bean5");
        user3.add();
   


p空间

1、编写xml

<!-- 命名空间 -->
     <bean id="tastDome2" class="spring_day_2.TastDome"/>
      <bean id="bean6" class="spring_day_2.TastDome3" p:tastDome-ref="tastDome"  p:val="1"/>

   

2、使用

user3=(TastDome3) context.getBean("bean6");
        user3.add();


0 0
原创粉丝点击