Spring框架实现注入的方法有很多种,其一,构造注入
典型的三层架构:
1 package dao; 2 3 import entity.User; 4 5 /** 6 * 增加DAO接口,定义了所需的持久化方法 7 */ 8 public interface UserDao { 9 public void save(User user); 10 }
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 public UserServiceImpl() { 17 18 } 19 20 // 带参数构造方法 为dao进行赋值 21 public UserServiceImpl(UserDao dao) { 22 this.dao = dao; 23 } 24 25 public UserDao getDao() { 26 return dao; 27 } 28 29 // dao 属性的setter访问器,会被Spring调用,实现设值注入 30 public void setDao(UserDao dao) { 31 this.dao = dao; 32 } 33 34 public void addNewUser(User user) { 35 // 调用用户DAO的方法保存用户信息 36 dao.save(user); 37 } 38 }
实体类:
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 12 // getter & setter 13 public Integer getId() { 14 return id; 15 } 16 17 public void setId(Integer id) { 18 this.id = id; 19 } 20 21 public String getUsername() { 22 return username; 23 } 24 25 public void setUsername(String username) { 26 this.username = username; 27 } 28 29 public String getPassword() { 30 return password; 31 } 32 33 public void setPassword(String password) { 34 this.password = password; 35 } 36 37 public String getEmail() { 38 return email; 39 } 40 41 public void setEmail(String email) { 42 this.email = email; 43 } 44 45 }
核心配置文件:
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" xmlns:aop="http://www.springframework.org/schema/aop" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 6 http://www.springframework.org/schema/aop 7 http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> 8 <!--以上是Spring框架的头信息 --> 9 <!--放置要被管理的对象实例 --> 10 <!-- 使用构造注入的方式 --> 11 <bean id="userDao" class="dao.impl.UserDaoImpl"></bean> 12 <bean id="UserService" class="service.impl.UserServiceImpl"> 13 <!--通过构造函数的方式,实现构造注入 --> 14 <constructor-arg> 15 <ref bean="userDao" /> 16 </constructor-arg> 17 </bean> 18 19 </beans>
测试方法:
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 20 User user = new User(); 21 user.setId(1); 22 user.setUsername("test"); 23 user.setPassword("123456"); 24 user.setEmail("test@xxx.com"); 25 26 service.addNewUser(user); 27 } 28 29 }
运行结果:
12-29 16:22:52[INFO]org.springframework.context.support.ClassPathXmlApplicationContext -Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2db0f6b2: startup date [Sun Dec 29 16:22:52 CST 2019]; root of context hierarchy 12-29 16:22:52[INFO]org.springframework.beans.factory.xml.XmlBeanDefinitionReader -Loading XML bean definitions from class path resource [applicationContext.xml] 12-29 16:22:52[INFO]org.springframework.beans.factory.support.DefaultListableBeanFactory -Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2f686d1f: defining beans [userDao,UserService]; root of factory hierarchy 保存用户信息到数据库