Spring框架-使用P命名空间进行注入

smalllove 2019-12-29

这里需要注意的一点是,P命名空间进行注入,是建立在设值注入的基础上的,也就是说,一定要有setter方法才行,同时还要引入P命名空间的头信息(这点千万别忘记了)

典型的三层架构:

package dao;

import entity.User;

/**
 * 增加DAO接口,定义了所需的持久化方法
 */
public interface UserDao {
    public void save(User user);
}
package dao.impl;

import dao.UserDao;
import entity.User;

/**
 * 用户DAO类,实现IDao接口,负责User类的持久化操作
 */
public class UserDaoImpl implements UserDao {

    public void save(User user) {
        // 这里并未实现完整的数据库操作,仅为说明问题
        System.out.println("保存用户信息到数据库");
    }
}
package service;

import entity.User;

/**
 * 用户业务接口,定义了所需的业务方法
 */
public interface UserService {
    public void addNewUser(User user);
}
package service.impl;

import service.UserService;
import dao.UserDao;
import entity.User;

/**
 * 用户业务类,实现对User功能的业务管理
 */
public class UserServiceImpl implements UserService {

    // 声明接口类型的引用,和具体实现类解耦合
    private UserDao dao;

    

    // 生成无参构造方法
    public UserServiceImpl() {

    }

    // 带参数构造方法   为dao进行赋值
    public UserServiceImpl(UserDao dao) {
        this.dao = dao;
    }


    public UserDao getDao() {
        return dao;
    }

    // dao 属性的setter访问器,会被Spring调用,实现设值注入
    public void setDao(UserDao dao) {
        this.dao = dao;
    }

    public void addNewUser(User user) {
        // 调用用户DAO的方法保存用户信息
        dao.save(user);
        System.out.println("注入进去的user对象的信息是:"+user.toString());
    }
}

实体类:(这里也进行了相应的改动)

package entity;

/**
 * 用户实体类
 */
public class User implements java.io.Serializable {
    private Integer id; // 用户ID
    private String username; // 用户名
    private String password; // 密码
    private String email; // 电子邮件
    private int age;//年龄
    

    @Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", password="
                + password + ", email=" + email + ", age=" + age
                + ", getAge()=" + getAge() + ", getId()=" + getId()
                + ", getUsername()=" + getUsername() + ", getPassword()="
                + getPassword() + ", getEmail()=" + getEmail()
                + ", getClass()=" + getClass() + ", hashCode()=" + hashCode()
                + ", toString()=" + super.toString() + "]";
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    // getter & setter
    public Integer getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

}

编写测试方法:

package test;

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

import service.UserService;
import service.impl.UserServiceImpl;

import entity.User;


public class AopTest {

    @Test
    public void aopTest() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService service = (UserService) ctx.getBean("userService");
        User user = (User) ctx.getBean("user");
        service.addNewUser(user);
    }

}

运行结果:

保存用户信息到数据库
注入进去的user对象的信息是:User [id=null, username=强强, password=22222, , age=15, getAge()=15, getId()=null, getUsername()=强强, getPassword()=22222, getEmail(), getClass()=class entity.User, hashCode()=1032986144, toString()]

相关推荐