装配SpringBean--依赖注入

所谓依赖注入,我觉得说白了其实就是给成员变量赋值,不管这个成员变量是基本类型还是引用类型,Spring中常用的依赖注入方式有两种:

1、构造器注入

2、setter注入

下面通过代码实例说明这两种注入的实现方式。

一、构造器注入

1、构造注入的原理

构造注入是利用类的构造方法,大部分情况下我们是通过类的构造函数(含参数或不含参数)创建一个对象,Spring中也可以通过反射方法通过构造函数完成注入。所以这种注入方式一般是用于引用类型。

2、代码实现

 在spring中实现需要三个步骤:

1⃣️创建类,提供构造方法。

 1 public class Employee {
 2     private Integer id;
 3 
 4     private String username;
 5 
 6     private String pwd;
 7 
 8     private String sex;
 9 
10     private String random;
11 
12     public Employee() {
13     }
14    //通过含参数的构造方法进行注入
15     public Employee(Integer id, String username, String pwd, String sex, String random) {
16         this.id = id;
17         this.username = username;
18         this.pwd = pwd;
19         this.sex = sex;
20         this.random = random;
21     }
22

2⃣️配置spring文件,如果构造函数含有参数,还需要定义参数。

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         http://www.springframework.org/schema/beans/spring-beans.xsd">
 6 
 7     <!-- 通过构造函数注入 -->
 8     <bean id="employee" class="com.hyc.pojo.Employee">
 9         <constructor-arg name="id" value="2" />
10         <constructor-arg name="username" value="李四" />
11         <constructor-arg name="pwd" value="lisi123" />
12         <constructor-arg name="sex" value="male" />
13         <constructor-arg name="random" value="32412" />
14     </bean>
15 
16 </beans>

上面的配置文件中主要有以下几点需要注意:

  • id:通过id获取该bean(资源)
  • class:需要装配的类的全限定名
  • constructor-arg:配置构造函数的参数,属性name是参数名称,value是参数值

注意⚠️:参数类型无需定义,spring会自动转化。因为我的类中定义的id是整形,在配置文件中没有定义数据类型,我debug时发现注入成功之后它已经被转换为Integer类型了。

3⃣️通过spring装配(之后的文章会介绍,本文先以XML方式实现)

 1 public class SpringIocTest {
 2 
 3     public static void main(String[] args) {
 4         ClassPathXmlApplicationContext context = null;
 5         String xmlPath = "classpath*:spring-iocconfig.xml";
 6         try {
 7             context = new ClassPathXmlApplicationContext(xmlPath);
 8             //装配bean
 9             Employee user = (Employee) context.getBean("employee");
10             System.out.println(user.getUsername());
11         } catch (BeansException e) {
12             System.out.println(e.getMessage());
13         } finally {
14             if (null != context) {
15                 context.destroy();
16             }
17         }
18     }
19 }

上面代码用来测试是否注入成功,ClassPathXmlApplicationContext是通过XML的方式装配bean,其中最重要的是context.getBean("employee")这句,其中的employee就是在配置文件中定义的id,这样就能获取到这个bean实例,下面看以下测试结果(预测应该就是上面配置的李四):

注意⚠️:因为调用了getName方法,所以在定义类的时候需要提供getter方法,我没有贴出来!!

看结果与预测是一致的,并且从打印出的信息看出注入的过程中加载了配置文件获取bean。

上面就是构造注入的实现过程。

二、setter注入

setter注入是Spring中最常使用的注入方式,它是利用java bean中定义的setter方法实现注入。使用构造注入时需要给构造函数传多个参数,如果改用setter方法,就不需要定义构造方法了,只要定义一个无餐构造函数和对应的setter方法即可。setter注入实现的步骤也有三步:

1⃣️创建类,提供无参构造方法和setter方法

 1 public class Employee {
 2     private Integer id;
 3 
 4     private String username;
 5 
 6     private String pwd;
 7 
 8     private String sex;
 9 
10     private String random;
11 
12     public Employee() {
13     }
14 
15     public Integer getId() {
16         return id;
17     }
18 
19     public void setId(Integer id) {
20         this.id = id;
21     }
22 
23     public String getUsername() {
24         return username;
25     }
26 
27     public void setUsername(String username) {
28         this.username = username == null ? null : username.trim();
29     }
30 
31     public String getPwd() {
32         return pwd;
33     }
34 
35     public void setPwd(String pwd) {
36         this.pwd = pwd == null ? null : pwd.trim();
37     }
38 
39     public String getSex() {
40         return sex;
41     }
42 
43     public void setSex(String sex) {
44         this.sex = sex == null ? null : sex.trim();
45     }
46 
47     public String getRandom() {
48         return random;
49     }
50 
51     public void setRandom(String random) {
52         this.random = random == null ? null : random.trim();
53     }
54 }

一般定义setter和getter方法,setter设置值,getter获取值。

2⃣️配置bean文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         http://www.springframework.org/schema/beans/spring-beans.xsd">
 6 
 7     <!-- 通过setter方法注入-->
 8     <bean id="employee1" class="com.hyc.pojo.Employee">
 9         <property name="id" value="3" />
10         <property name="username" value="张三" />
11         <property name="pwd" value="lisi123" />
12         <property name="sex" value="male" />
13         <property name="random" value="32412" />
14     </bean>
15 
16 </beans>

注意⚠️:上面的配置中:

  • id:获取这个bean的标识
  • class:需要注入类的全限定名
  • property:类中的成员变量(属性),需要设置哪些变量的值就设置哪些
  • value:属性值

3⃣️通过spring装配

 1 public class SpringIocTest {
 2 
 3     public static void main(String[] args) {
 4         ClassPathXmlApplicationContext context = null;
 5         String xmlPath = "classpath*:spring-ioc.xml";
 6         try {
 7             context = new ClassPathXmlApplicationContext(xmlPath);
 8             //装配bean
 9             Employee user = (Employee) context.getBean("employee1");
10             System.out.println(user.getUsername());
11         } catch (BeansException e) {
12             System.out.println(e.getMessage());
13         } finally {
14             if (null != context) {
15                 context.destroy();
16             }
17         }
18     }
19 }

同构造注入中介绍的一样,也是通过XML方式获取bean,只不过为了区分在XML中将bean的id定义为employee1。查看测试结果(预期应该是张三)

结果符合预期,注入成功。

以上就是依赖注入的两种实现方式。

猜你喜欢

转载自www.cnblogs.com/hellowhy/p/9710554.html