Spring框架如何使用P命名空間進行注入
這篇文章主要介紹了Spring框架如何使用P命名空間進行注入,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
這里需要注意的一點是,P命名空間進行注入,是建立在設(shè)值注入的基礎(chǔ)上的,也就是說,一定要有setter方法才行,同時還要引入P命名空間的頭信息(這點千萬別忘記了)
典型的三層架構(gòu):
package dao;
import entity.User;
/**
* 增加DAO接口,定義了所需的持久化方法
*/
public interface UserDao {
public void save(User user);
}
package dao.impl;
import dao.UserDao;
import entity.User;
/**
* 用戶DAO類,實現(xiàn)IDao接口,負責(zé)User類的持久化操作
*/
public class UserDaoImpl implements UserDao {
public void save(User user) {
// 這里并未實現(xiàn)完整的數(shù)據(jù)庫操作,僅為說明問題
System.out.println("保存用戶信息到數(shù)據(jù)庫");
}
}
package service;
import entity.User;
/**
* 用戶業(yè)務(wù)接口,定義了所需的業(yè)務(wù)方法
*/
public interface UserService {
public void addNewUser(User user);
}
package service.impl;
import service.UserService;
import dao.UserDao;
import entity.User;
/**
* 用戶業(yè)務(wù)類,實現(xiàn)對User功能的業(yè)務(wù)管理
*/
public class UserServiceImpl implements UserService {
// 聲明接口類型的引用,和具體實現(xiàn)類解耦合
private UserDao dao;
// 生成無參構(gòu)造方法
public UserServiceImpl() {
}
// 帶參數(shù)構(gòu)造方法 為dao進行賦值
public UserServiceImpl(UserDao dao) {
this.dao = dao;
}
public UserDao getDao() {
return dao;
}
// dao 屬性的setter訪問器,會被Spring調(diào)用,實現(xiàn)設(shè)值注入
public void setDao(UserDao dao) {
this.dao = dao;
}
public void addNewUser(User user) {
// 調(diào)用用戶DAO的方法保存用戶信息
dao.save(user);
System.out.println("注入進去的user對象的信息是:"+user.toString());
}
}
實體類:(這里也進行了相應(yīng)的改動)
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);
}
}
運行結(jié)果:
保存用戶信息到數(shù)據(jù)庫
注入進去的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]
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java 中createStatement()方法的實例詳解
這篇文章主要介紹了java 中createStatement()方法的實例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06
java實現(xiàn)文件復(fù)制、剪切文件和刪除示例
這篇文章主要介紹了java實現(xiàn)文件復(fù)制、剪切文件和刪除示例,需要的朋友可以參考下2014-04-04
關(guān)于spring版本與JDK版本不兼容的問題及解決方法
這篇文章主要介紹了關(guān)于spring版本與JDK版本不兼容的問題,本文給大家?guī)砹私鉀Q方法,需要的朋友可以參考下2018-11-11
Java中的ReentrantReadWriteLock實現(xiàn)原理詳解
這篇文章主要介紹了Java中的ReentrantReadWriteLock實現(xiàn)原理詳解,讀寫鎖實現(xiàn)了接口ReadWriteLock,適合于讀多寫少的情況,支持公平鎖和非公平鎖,支持可沖入(進入讀鎖后可再進入讀鎖,進入寫鎖后可再進入寫鎖和讀鎖),需要的朋友可以參考下2024-01-01

