Spring属性注入DI

1.构造方法注入(只需提供一个构造方法)

    javabean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.mickeymouse.ioc;
public class Car {
     private String name;
     private Double price;
     
     //提供构造方法
     public Car(String name, Double price) {
         super ();
         this .name = name;
         this .price = price;
     }
     @Override
     public String toString() {
         return "Car [name=" + name + ", price=" + price + "]" ;
     }
     
}

Xml配置

1
2
3
4
5
<!-- Bean的属性注入:构造方法注入 -->
     <bean id= "car" class = "com.mickeymouse.ioc.Car" >
         <constructor-arg name= "name" value= "宝马" ></constructor-arg>
         <constructor-arg name= "price" value= "4343434.0" ></constructor-arg>
     </bean>

测试类

1
2
3
4
5
6
7
8
9
10
11
12
/**
      * 属性注入之构造方法注入
      */
     @Test
     public void test5(){
         //获取配置文件
         String path = "applicationContext.xml" ;
         //加载配置文件
         AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext(path);
         Car car = (Car) applicationContext.getBean( "car" );
         System.out.println(car);
     }

结果:




2 . set方法注入(只需提供set方法)

javabean:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.mickeymouse.ioc;
public class Car {
     private String name;
     private Double price;
     
     public String getName() {
         return name;
     }
     public void setName(String name) {
         this .name = name;
     }
     public Double getPrice() {
         return price;
     }
     public void setPrice(Double price) {
         this .price = price;
     }
     @Override
     public String toString() {
         return "Car [name=" + name + ", price=" + price + "]" ;
     }
     
}

配置文件

1
2
3
4
5
<!-- Bean的属性注入:Set方法注入 -->
     <bean id= "car" class = "com.mickeymouse.ioc.Car" >
         <property name= "name" value= "兰博基尼" />
         <property name= "price" value= "3423432.0" />
     </bean>

测试类:

1
2
3
4
5
6
7
8
9
10
11
12
/**
      * 属性注入之set方法注入
      */
     @Test
     public void test6(){
         //获取配置文件
         String path = "applicationContext.xml" ;
         //加载配置文件
         AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext(path);
         Car car = (Car) applicationContext.getBean( "car" );
         System.out.println(car);
     }

结果图




3 . P名称空间的注入----->Spring的2.5版本才开始

    一 . 引入P名称空间:

1
2
3
4
5
6
7
8
9
10
11
12
<? xml version = "1.0" encoding = "UTF-8" ?>
        xmlns:p = "http://www.springframework.org/schema/p"
        
        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 definitions here -->
     <!-- Bean的属性注入:p名称空间注入 -->
     < bean id = "car" class = "com.mickeymouse.ioc.Car" p:name = "长安宝马" p:price = "432423.5" ></ bean >
</ beans >

    二 . javabean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.mickeymouse.ioc;
public class Car {
     private String name;
     private Double price;
     
     public String getName() {
         return name;
     }
     public void setName(String name) {
         this .name = name;
     }
     public Double getPrice() {
         return price;
     }
     public void setPrice(Double price) {
         this .price = price;
     }
     @Override
     public String toString() {
         return "Car [name=" + name + ", price=" + price + "]" ;
     }
}

测试类

1
2
3
4
5
6
7
8
9
10
11
12
/**
      * 属性注入之P名称空间注入
      */
     @Test
     public void test7(){
         //获取配置文件
         String path = "applicationContext.xml" ;
         //加载配置文件
         AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext(path);
         Car car = (Car) applicationContext.getBean( "car" );
         System.out.println(car);
     }

结果图:





4 . Bean属性注入之 SPEL表达式方式--->sping3.0以后才开始出现

javabean:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.mickeymouse.ioc;
public class Car {
     private String name;
     private Double price;
     
     public String getName() {
         return name;
     }
     public void setName(String name) {
         this .name = name;
     }
     public Double getPrice() {
         return price;
     }
     public void setPrice(Double price) {
         this .price = price;
     }
     @Override
     public String toString() {
         return "Car [name=" + name + ", price=" + price + "]" ;
     }
}

XML表达式:

1
2
3
4
5
<!-- Bean的属性注入:SPEL表达式方式 -->
     < bean id = "car" class = "com.mickeymouse.ioc.Car" >
         < property name = "name" value = "#{'劳斯莱斯'}" />
         < property name = "price" value = "#{3434.00}" />
     </ bean >

测试类

1
2
3
4
5
6
7
8
9
10
11
12
/**
      * 属性注入之SPEL表达式注入
      */
     @Test
     public void test8(){
         //获取配置文件
         String path = "applicationContext.xml" ;
         //加载配置文件
         AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext(path);
         Car car = (Car) applicationContext.getBean( "car" );
         System.out.println(car);
     }

结果





XML关于数组,集合(list  map  set  properties)的属性赋值配置写法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<bean id= "collectionBean" class = "com.itheima.spring.demo6.CollectionBean" >
         <property name= "arrs" >
             <list>
                 <value>老王</value>
                 <value>凤姐</value>
                 <value>如花</value>
             </list>
         </property>
         
         <property name= "list" >
             <list>
                 <value>豆豆</value>
                 <value>奶茶</value>
                 <value>绿茶</value>
             </list>
         </property>
         
         <property name= "set" >
             <set>
                 <value>王尧</value>
                 <value>刘健</value>
                 <value>周玉</value>
             </set>
         </property>
         
         <property name= "map" >
             <map>
                 <entry key= "老王2" value= "38" />
                 <entry key= "凤姐" value= "38" />
                 <entry key= "如花" value= "29" />
             </map>
         </property>
         
         <property name= "properties" >
             <props>
                 <prop key= "username" >root</prop>
                 <prop key= "password" > 123 </prop>
             </props>
         </property>
     </bean>



转载于:https://my.oschina.net/mickeymouse/blog/519123

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值