您的位置:首页 > 编程语言 > Java开发

Spring属性注入DI

2015-10-20 00:00 513 查看

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"

?>


<

beans

xmlns

=

"http://www.springframework.org/schema/beans"


   

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>

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: