Spring属性注入DI
博客专区 > T-Brain 的博客 > 博客详情
Spring属性注入DI
T-Brain 发表于2年前
Spring属性注入DI
  • 发表于 2年前
  • 阅读 6
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

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>



共有 人打赏支持
粉丝 2
博文 44
码字总数 46138
×
T-Brain
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: