Spring框架-使用P命名空间进行注入
这里需要注意的一点是,P命名空间进行注入,是建立在设值注入的基础上的,也就是说,一定要有setter方法才行,同时还要引入P命名空间的头信息(这点千万别忘记了)
典型的三层架构:
package dao;
import entity.User;
/**
* 增加DAO接口,定义了所需的持久化方法
*/
public interface UserDao {
public void save(User user);
}
1 package dao.impl;
2
3 import dao.UserDao;
4 import entity.User;
5
6 /**
7 * 用户DAO类,实现IDao接口,负责User类的持久化操作
8 */
9 public class UserDaoImpl implements UserDao {
10
11 public void save(User user) {
12 // 这里并未实现完整的数据库操作,仅为说明问题
13 System.out.println("保存用户信息到数据库");
14 }
15 }
1 package service;
2
3 import entity.User;
4
5 /**
6 * 用户业务接口,定义了所需的业务方法
7 */
8 public interface UserService {
9 public void addNewUser(User user);
10 }
1 package service.impl;
2
3 import service.UserService;
4 import dao.UserDao;
5 import entity.User;
6
7 /**
8 * 用户业务类,实现对User功能的业务管理
9 */
10 public class UserServiceImpl implements UserService {
11
12 // 声明接口类型的引用,和具体实现类解耦合
13 private UserDao dao;
14
15
16
17 // 生成无参构造方法
18 public UserServiceImpl() {
19
20 }
21
22 // 带参数构造方法 为dao进行赋值
23 public UserServiceImpl(UserDao dao) {
24 this.dao = dao;
25 }
26
27
28 public UserDao getDao() {
29 return dao;
30 }
31
32 // dao 属性的setter访问器,会被Spring调用,实现设值注入
33 public void setDao(UserDao dao) {
34 this.dao = dao;
35 }
36
37 public void addNewUser(User user) {
38 // 调用用户DAO的方法保存用户信息
39 dao.save(user);
40 System.out.println("注入进去的user对象的信息是:"+user.toString());
41 }
42 }
实体类:(这里也进行了相应的改动)
1 package entity;
2
3 /**
4 * 用户实体类
5 */
6 public class User implements java.io.Serializable {
7 private Integer id; // 用户ID
8 private String username; // 用户名
9 private String password; // 密码
10 private String email; // 电子邮件
11 private int age;//年龄
12
13
14 @Override
15 public String toString() {
16 return "User [id=" + id + ", username=" + username + ", password="
17 + password + ", email=" + email + ", age=" + age
18 + ", getAge()=" + getAge() + ", getId()=" + getId()
19 + ", getUsername()=" + getUsername() + ", getPassword()="
20 + getPassword() + ", getEmail()=" + getEmail()
21 + ", getClass()=" + getClass() + ", hashCode()=" + hashCode()
22 + ", toString()=" + super.toString() + "]";
23 }
24
25 public int getAge() {
26 return age;
27 }
28
29 public void setAge(int age) {
30 this.age = age;
31 }
32
33 // getter & setter
34 public Integer getId() {
35 return id;
36 }
37
38 public void setId(Integer id) {
39 this.id = id;
40 }
41
42 public String getUsername() {
43 return username;
44 }
45
46 public void setUsername(String username) {
47 this.username = username;
48 }
49
50 public String getPassword() {
51 return password;
52 }
53
54 public void setPassword(String password) {
55 this.password = password;
56 }
57
58 public String getEmail() {
59 return email;
60 }
61
62 public void setEmail(String email) {
63 this.email = email;
64 }
65
66 }
编写测试方法:
1 package test;
2
3 import org.junit.Test;
4 import org.springframework.context.ApplicationContext;
5 import org.springframework.context.support.ClassPathXmlApplicationContext;
6
7 import service.UserService;
8 import service.impl.UserServiceImpl;
9
10 import entity.User;
11
12
13 public class AopTest {
14
15 @Test
16 public void aopTest() {
17 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
18 UserService service = (UserService) ctx.getBean("userService");
19 User user = (User) ctx.getBean("user");
20 service.addNewUser(user);
21 }
22
23 }
运行结果:
保存用户信息到数据库
注入进去的user对象的信息是:User [id=null, username=强强, password=22222, email=1111@qq.com, age=15, getAge()=15, getId()=null, getUsername()=强强, getPassword()=22222, getEmail()=1111@qq.com, getClass()=class entity.User, hashCode()=1032986144, toString()=entity.User@3d921e20]