好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Spring:bean注入--Set方法注入

Set 方法注入

1.新建一个空的 maven项目。

2.导入依赖

?

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

properties>

         < project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding >

         <!--这里是java 版本号-->

         < maven测试数据piler.source >11</ maven测试数据piler.source > 

         < maven测试数据piler.target >11</ maven测试数据piler.target >

         <!--这里是方便版本控制-->

         < spring.version >5.3.1</ spring.version >

         < lombok.version >1.18.20</ lombok.version >

         < junit.version >4.12</ junit.version >

</ properties >

< dependencies >

     < dependency >

         < groupId >org.springframework</ groupId >

         < artifactId >spring-beans</ artifactId >

         < version >${spring.version}</ version >

     </ dependency >

     < dependency >

         < groupId >org.springframework</ groupId >

         < artifactId >spring-context</ artifactId >

         < version >${spring.version}</ version >

     </ dependency >

     < dependency >

         < groupId >org.projectlombok</ groupId >

         < artifactId >lombok</ artifactId >

         < version >${lombok.version}</ version >

     </ dependency >

     < dependency >

         < groupId >junit</ groupId >

         < artifactId >junit</ artifactId >

         < version >${junit.version}</ version >

     </ dependency >

</ dependencies >

3.工程项目结构

4.新建包 com.crush.pojo

5.新建Java类Student

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

@Data // set、get 方法

@AllArgsConstructor // 全参构造

@NoArgsConstructor   // 无参构造

public class Student {

     /**

      * 学号

      */

     private Long number;

     /**

      * 学生姓名

      */

     private String name;

     /**

      * 所在学校

      */

     private String school;

}

resource 下 beans.xml文件

?

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

<? xml version = "1.0" encoding = "UTF-8" ?>

< beans xmlns = "http://HdhCmsTestspringframework.org/schema/beans"

        xmlns:xsi = "http://HdhCmsTestw3.org/2001/XMLSchema-instance"

        xsi:schemaLocation = "http://HdhCmsTestspringframework.org/schema/beans http://HdhCmsTestspringframework.org/schema/beans/spring-beans.xsd" >

     <!--第一种方式 set 方式注入

         1、有set方法才可以注入

         2、默认是单例模式 singleton

         -->

     < bean id = "student" class = "com.crush.pojo.Student" scope = "singleton" >

         <!--值可以跟在在标签后面 也可以 写在标签内部-->

         < property name = "number" >

             < value >1</ value >

         </ property >

         < property name = "name" value = "wyh" />

         < property name = "school" value = "hngy" />

     </ bean >

         <!--这个id 就是 applicationContext.getBean("【bean-id】", Student.class);

         此处id 大多数时候命名规则就是 类名的第一个字母改为小写

         class:Student

         bean id一般就为: student

      -->

     < bean id = "ssss" class = "com.crush.pojo.Student" scope = "singleton" >

         <!--值可以跟在在标签后面 也可以 写在标签内部-->

         < property name = "number" >

             < value >1</ value >

         </ property >

         < property name = "name" value = "wyh" />

         < property name = "school" value = "hngy" />

     </ bean >

</ beans >

写一个测试类

?

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

public class Test {

 

     /**

      * 通过 ClassPathXmlApplicationContext 获取 Spring 应用程序的 上下文 ApplicationContext

      */

     @org .junit.Test

     public void test(){

         ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "beans.xml" );

         // 第一种方式 获取ioc 容器中的Student 强制类型转换

         Student student = (Student) applicationContext.getBean( "student" );

         // 第二种方式 直接在后面写明类的标签。

         Student student1 = applicationContext.getBean( "student" , Student. class );

         // student.setName("cccc"); 给其中一个修改 就会全部修改 可以自己打开测试下

         System.out.println(student);

         System.out.println(student1);

         // 这里结果为true

         // 解释:因为Spring 默认构造出来的对象 默认是单例的。 无论获取多少次 ,都是单例的。

         System.out.println(student==student1);

     }

         /**

      * 通过 FileSystemXmlApplicationContext 获取 Spring 应用程序的 上下文 ApplicationContext

      * 还有第三种是 通过Web服务器实例化 ApplicationContext 容器

      */

     @org .junit.Test

     public void test2(){

         //这里的路径 也可以 用绝对路径

         ApplicationContext applicationContext = new FileSystemXmlApplicationContext( "src\\main\\resources\\beans.xml" );

         Student student = applicationContext.getBean( "student" , Student. class );

         System.out.println(student);

     }

}

小小思考

为什么 new ClassPathXmlApplicationContext([beans.xml]); 要用ApplicationContext 来接收,而不用ClassPathXmlApplicationContext 接收呢?

?

1

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");

?

1

ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");

解释:

按照面向接口编程的思想,声明变量应该是接口类型的,然后创建一个该接口的实现类的实例赋值给该变量。 ApplicationContext是接口,ClassPathXmlApplicationContext是它的一个实现类。所以你就看到了 ApplicationContext ac = new ClassPathXmlApplicationContext(…)

总结

本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注的更多内容!

原文链接:https://blog.csdn.net/weixin_45821811/article/details/117651505

查看更多关于Spring:bean注入--Set方法注入的详细内容...

  阅读:20次