现在通过一个小程序来了解IOC的思想,这个可能更加直观,首先项目结构就是controller(控制层,他决定接受数据以及分发),service(他负责抽象的一些服务逻辑),dao(负责跟数据库打交道)
先看一下我们操控的模型
package bean; public class User { private int id; private String username; private String password; public User(int id, String username, String password) { this.id = id; this.username = username; this.password = password; } public void setPassword(String password) { this.password = password; } public void setUsername(String username) { this.username = username; } public void setId(int id) { this.id = id; } @Override public String toString() { return "User{" + "id=" + id + ", username=‘" + username + ‘\‘‘ + ", password=‘" + password + ‘\‘‘ + ‘}‘; } }
接下来看一下数据库处理模块
// UserDao package dao; import bean.User; public interface UserDao { User getUser(); } // DataSources 数据库的相关配置类 package dao.impl; public class DataSources { private String username; private String password; private String url; private String driverClass; public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getDriverClass() { return driverClass; } public void setDriverClass(String driverClass) { this.driverClass = driverClass; } public DataSources() { } public DataSources(String username, String password, String url, String driverClass) { this.username = username; this.password = password; this.url = url; this.driverClass = driverClass; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @Override public String toString() { return "DataSources{" + "username=‘" + username + ‘\‘‘ + ", password=‘" + password + ‘\‘‘ + ", url=‘" + url + ‘\‘‘ + ", driverClass=‘" + driverClass + ‘\‘‘ + ‘}‘; } } // UserDaoImpl 数据库的相关操作 package dao.impl; import bean.User; import dao.UserDao; public class UserDaoImpl implements UserDao { DataSources ds; public UserDaoImpl() { } public UserDaoImpl(DataSources ds) { this.ds = ds; } public User getUser() { System.out.println("模拟数据库连接" + ds); return new User(1, "mark", "1234"); } }
接下来看一下service
// UserService service对外接口 package service; import bean.User; public interface UserService { User getUser(); } // UserServiceImpl 处理 package service.impl; import bean.User; import dao.UserDao; import service.UserService; public class UserServiceImpl implements UserService { UserDao userDao; public UserServiceImpl() { } public UserServiceImpl(UserDao userDao) { this.userDao = userDao; } public void setUserDao(UserDao ud) { this.userDao = ud; } public UserDao getUserDao() { return this.userDao; } public User getUser() { return userDao.getUser(); } }
我们看一下controller
package controller; import bean.User; import service.UserService; import service.impl.UserServiceImpl; public class UserController { UserService userService; public void getUser() { User user = userService.getUser(); System.out.println(user); } public void setUserService(UserServiceImpl userService) { this.userService = userService; } }
我们重点看一下applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 数据库连接 使用property注入 --> <bean id="db" class="dao.impl.DataSources"> <property name="url" value="jdbc:mysql://127.0.0.1:3306/user"/> <property name="username" value="root"/> <property name="password" value="mysql"/> <property name="driverClass" value="com.mysql.jdbc.Driver"/> </bean> <!-- 数据库操作 使用constructor-arg构造方法进行注入,因为是对象,所以我们使用ref --> <bean id="dao" class="dao.impl.UserDaoImpl"> <constructor-arg index="0" name="ds" ref="db"/> </bean> <!-- 服务端操作 我们使用p命名空间进行注入,对象所以要加上ref --> <bean id="service" class="service.impl.UserServiceImpl" p:userDao-ref="dao"/> <!-- 控制层操作 --> <bean id="controller" class="controller.UserController" p:userService-ref="service"/> </beans>
最终看一下测试代码
import controller.UserController; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestUser { @Test public void test(){ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserController uc = context.getBean("controller", UserController.class); uc.getUser(); } } // 模拟数据库连接DataSources{username=‘root‘, password=‘mysql‘, url=‘jdbc:mysql://127.0.0.1:3306/user‘, driverClass=‘com.mysql.jdbc.Driver‘} User{id=1, username=‘mark‘, password=‘1234‘}
代码运行通过,我们看到在service,dao,controller中,我们并没有初始化任何对象,所有的初始化的信息我们全部放在了applicationContext.xml里面,交给spring帮助我们进行相关操作,这样实现了解耦,并且以后需要进行修改,比如修改数据库,我们直接在applicationContext.xml里面修改就好,不需要去代码层一个项目一个项目的去找。
原文:https://www.cnblogs.com/yangshixiong/p/12188734.html