Theoretical derivation of IOC
Actions to create a project before Sping
1.UserDao 接口
2.UserDaoImpl 实现类
3.UserService 业务接口
4.UserServiceImpl 业务实现类
Create a new maven project
File->new->project
Select the maven project -> next
Modify it to your own
Import the jar package in the pom.xml file
Remember to modify the maven repository location
delete src file
After removing:
Purpose:
In order to build sub-projects, many things can be managed in one project
Create a new module
The project process before the emergence of Spring
1. New package: com.kuang.dao
2. Create a new UserDao interface
3. Add a method to the UserDao interface
package com.kuang.dao;
public interface UserDao {
void getUser();
}
4. Create an implementation class UserDaoImpl of the UserDao interface
package com.kuang.dao;
public class UserDaoImpl implements UserDao{
@Override
public void getUser() {
System.out.println("默认获取用户的数据");
}
}
5. Create an implementation class UserDaoMysqlImpl of the UserDao interface
package com.kuang.dao;
public class UserDaoMysqlImpl implements UserDao{
@Override
public void getUser() {
System.out.println("默认获取mysql的数据");
}
}
6. Create an implementation class UserDaoOracleImpl of the UserDao interface
package com.kuang.dao;
public class UserDaoOracleImpl implements UserDao{
@Override
public void getUser() {
System.out.println("默认获取oracle的数据");
}
}
7. Create com.kuang.service package
8. Create UserService interface
9. Create UserService interface implementation class UserServiceImpl
package com.kuang.service;
import com.kuang.dao.UserDao;
import com.kuang.dao.UserDaoImpl;
/*
* 业务层(service层)调用数据访问层(dao层)
*
* java中除了继承还有组合的概念
* */
public class UserServiceImpl implements UserService{
// 将UserDao引入UserService
private UserDao userDao = new UserDaoImpl();
@Override
public void getUser() {
//调用
userDao.getUser();
}
}
10. Test
Create myTest class under Test package
Code:
import com.kuang.service.UserService;
import com.kuang.service.UserServiceImpl;
public class myTest {
public static void main(String[] args) {
//用户实际调用的是业务层,dao层用户不需要接触
UserService userService= new UserServiceImpl();
// 用户调用业务层的getUser方法
userService.getUser();
}
}
Test Results
Add a mysql implementation in the Dao layer
The code of the service layer needs to be changed
package com.kuang.service;
import com.kuang.dao.UserDao;
import com.kuang.dao.UserDaoImpl;
import com.kuang.dao.UserDaoMysqlImpl;
/*
* 业务层(service层)调用数据访问层(dao层)
*
* java中除了继承还有组合的概念
* */
public class UserServiceImpl implements UserService{
// 将UserDao引入UserService
// private UserDao userDao = new UserDaoImpl();
// 要想实现UserDaoMysqlImpl,private UserDao userDao = new UserDaoImpl();需要修改为下面的
private UserDao userDao = new UserDaoMysqlImpl();
@Override
public void getUser() {
userDao.getUser();
}
}
Test Results
As long as the user request changes, the service layer needs to be changed manually. The program cannot adapt to the user's changes
Optimization: use set to dynamically implement value injection
Only need to change the UserDaoServiceImpl layer code
package com.kuang.service;
import com.kuang.dao.UserDao;
import com.kuang.dao.UserDaoImpl;
import com.kuang.dao.UserDaoMysqlImpl;
/*
* 业务层(service层)调用数据访问层(dao层)
*
* java中除了继承还有组合的概念
* */
public class UserServiceImpl implements UserService{
// 将UserDao引入UserService
// private UserDao userDao = new UserDaoImpl();
// 要想实现UserDaoMysqlImpl,private UserDao userDao = new UserDaoImpl();需要修改为下面的
// private UserDao userDao = new UserDaoMysqlImpl();
private UserDao userDao;
//利用set方法进行动态实现值的注入
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void getUser() {
userDao.getUser();
}
}
test
import com.kuang.dao.UserDaoMysqlImpl;
import com.kuang.service.UserService;
import com.kuang.service.UserServiceImpl;
public class myTest {
public static void main(String[] args) {
//用户实际调用的是业务层,dao层用户不需要接触
UserService userService= new UserServiceImpl();
((UserServiceImpl)userService).setUserDao(new UserDaoMysqlImpl());
// 用户调用业务层的getUser方法
userService.getUser();
}
}
在我们之前的业务中,用户的需求可能会影响我们原来的代码,我们需要根据用户的需求去修改原代码!
如果程序代码量非常大,修改一个代码的成本十分昂贵
We use a Set interface implementation, the code has changed revolutionary
private UserDao userDao;
//利用set方法进行动态实现值的注入
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
● Before: the program is actively creating objects! Control is in the hands of the programmer
● After using set injection, the program no longer has the initiative, but becomes a passive receiving object!
This kind of thinking solves the problem in essence. Programmers do not need to manage the creation of objects, the coupling of the system is greatly reduced, and they can focus more on business implementation.
By way of configuration file
The above steps 1-9 remain unchanged
, and then create a new beans.xml in the resource package
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="mysqlImpl" class="com.kuang.dao.UserDaoMysqlImpl" />
<bean id="oracleImpl" class="com.kuang.dao.UserDaoOracleImpl" />
<bean id="UserServiceImpl" class="com.kuang.service.UserServiceImpl">
<!-- ref:引用Spring中创建好的对象
value:具体的值,基本数据类型
-->
<property name="userDao" ref="oracleImpl" />
</bean>
</beans>
test:
import com.kuang.dao.UserDaoMysqlImpl;
import com.kuang.service.UserService;
import com.kuang.service.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class myTest {
public static void main(String[] args) {
//获取ApplicationContext:拿到Spring的容器
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserServiceImpl userServiceImpl = (UserServiceImpl)context.getBean("UserServiceImpl");
userServiceImpl.getUser();
}
}