spring使用案例 属性注入

news/2025/2/23 23:07:44

构造方法注入

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();



http://www.niftyadmin.cn/n/3863228.html

相关文章

Hashtable无序,用Dictionary代替

//输出1234 public void TestDictionary() {Dictionary<string, int> dic new Dictionary<string, int>();dic.Add("One", 1);dic.Add("Two", 2);dic.Add("Three", 3);dic.Add("Four", 4);foreach (var r in dic.Keys) …

SQL性能优化(不断总结)

1.查询的模糊匹配 尽量避免在一个复杂查询里面使用 LIKE %parm1%—— 红色标识位置的百分号会导致相关列的索引无法使用&#xff0c;最好不要用. 解决办法: 其实只需要对该脚本略做改进&#xff0c;查询速度便会提高近百倍。改进方法如下&#xff1a; a、修改前台…

413 Request Entity Too Large 的解决方法

增加如下两行到nginx.conf的http{}段&#xff0c; 增大nginx上传文件大小限制 #设置允许发布内容为8M client_max_body_size 8M; client_body_buffer_size 128k; 另外如果运行的是php&#xff0c;那么还要检查php.ini&#xff0c;这个大小client_max_body_size要和php.ini中的…

一次数据表十万的加载经历

1、减少日志的输出 设置为off2、调整jvm的大小 百度widows 配置tomcat-Xms3048M -Xmx3048M -Xmn700M -Xss512K -XX:PermSize300M -XX:MaxPermSize300M -XX:SurvivorRatio8 -XX:MaxTenuringThreshold5 -XX:GCTimeRatio19 -Xnoclassgc -XX:DisableExplicitGC -XX:UseParNewGC -XX…

Gradle离线安装

Google弃Eclipse搞出了Android Studio&#xff0c;结果光安装个 Android Studio&#xff0c;启动后还要在线安装Gradle&#xff0c;然后又被墙&#xff0c;卡死半天下不下来&#xff0c;真心折腾。 首先到C:\Users\用户名\.gradle\wrapper\dists\gradle-2.2.1-all\下确认gradle…

android studio f4出现Multiple entries with same key

Multiple entries with same key: Google Inc.:Google APIs:22Google APIs, Android 22 (API 22) and Google Inc.:Google APIs:22Google APIs (API 22) 是sdk上有重复的Google APIs 如22下面可能有两个Google APIs Google APIs&#xff08;这两个可能有些区别&#xff0c;如…

| 运算符和0x

& | | 是 1|0 为1 11|00 为11 & 是 1&0 为0 11||00 为00 android中 书写在java类上的是十进制的 左移右移<< >> 左移运算 a15&#xff0c;a>>2 表示把000001111右移为00000011(十进制3 右移运算 a<<4 指把a的各二进位向左移动4…

Android View 深度分析

一、初识 ViewRoot 和 DecorView Activity中有一个成员为Window&#xff0c;其实例化对象为PhoneWindow&#xff0c;PhoneWindow为抽象Window类的实现类。 这里先简要说明下这些类的职责&#xff1a; 1.Window是一个抽象类&#xff0c;提供了绘制窗口的一组通用API。 2.Phon…