Java 企业级应用开发 part 3 - J2EE part 3

  • Spring
  • Spring MVC

Dependency Injection(依赖注入)

构造方法注入

首先我们建立一个 Java Bean

User1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.itheima;

import org.springframework.beans.factory.annotation.Autowired;

public class User1 {
private int id;
private String name;
private String password;

//有参构造方法
@Autowired(required = false)
public User1(int id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}

public String toString() {
return "id=" + id + ",name=" + name + ",password=" + password;
}
}

Java Bean 实际上是一个普通的 Java 类,但其按照一定的约定去写,例如包含 setter/getter 方法,提供无参构造方法等。Java Bean 的主要作用是封装数据,通常用于在不同层之间传递数据。
这里的 @Autowired(required = false) 是为了消除 IDEA 的警告提示,表示这个构造方法的参数不需要自动装配,因为我们采用 xml 手动配置。

接下来我们在 applicationContext.xml 中配置这个 Bean:

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user1" class="com.itheima.User1">
<constructor-arg name="id" value="1"></constructor-arg>
<constructor-arg name="name" value="张三"></constructor-arg>
<constructor-arg name="password" value="123"></constructor-arg>
</bean>
</beans>

在这里我们在 <beans> 标签中定义了一个 Bean,id 是 user1,class 是 com.itheima.User1。我们通过 <constructor-arg> 标签来指定构造方法的参数值。其中的 namevalue 属性分别对应构造方法的参数名称和参数值。

随后我们测试依赖注入的结果

TestUser1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.itheima;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestUser1 {
public static void main(String[] args) throws Exception {
//加载applicationContext.xml配置
ApplicationContext applicationContext = new
ClassPathXmlApplicationContext("applicationContext-User.xml");
//获取配置中的User1实例
User1 user1 = applicationContext.getBean("user1", User1.class);
System.out.println(user1);
}
}

首先我们通过 ApplicationContext 加载 applicationContext-User.xml 配置文件,然后通过 getBean 方法获取配置中的 User1 实例,并打印出来(调用 toString 方法)。

setter 方法注入

我们来创建另一个 Bean

User2.java
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.itheima;

public class User2 {
private int id;
private String name;
private String password;

public void setId(int id) {
this.id = id;
}

public void setName(String name) {
this.name = name;
}

public void setPassword(String password) {
this.password = password;
}

public String toString() {
return "id=" + id + ",name=" + name + ",password=" + password;
}
}

getter/setter 可由 IDEA 自动生成,只需要编写成员变量

接下里我们在 applicationContext-User2.xml 中配置这个 Bean:

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user2" class="com.itheima.User2">
<property name="id" value="2"></property>
<property name="name" value="李四"></property>
<property name="password" value="456"></property>
</bean>
</beans>

在这里我们通过 <property> 标签来指定 setter 方法的参数值。其中的 name 属性对应 setter 方法的名称(去掉 set 前缀,并将首字母小写),value 属性对应参数值。

同样的,我们测试依赖注入的结果:

TestUser2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.itheima;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestUser2 {
public static void main(String[] args) throws Exception {
//加载applicationContext.xml配置
ApplicationContext applicationContext = new
ClassPathXmlApplicationContext("applicationContext-User2.xml");
//获取配置中的User2实例
User2 user2 = applicationContext.getBean("user2", User2.class);
System.out.println(user2);
}
}