ChatGPT 可用网址,仅供交流学习使用,如对您有所帮助,请收藏并推荐给需要的朋友。
https://ckai.xyz
一. 传统方法的一般实现
Dao层:
# 基类
public interface UserDao {
String getUser();
}
# 一般的实现
public class UserDaoImp implements UserDao{
public String getUser() {
// do something
String str = "The data of User";
return str;
}
}
# Mysql的实现
public class UserDaoMySqImp implements UserDao{
public String getUser() {
return "the Data of Mysql";
}
}
# Oracle的实现
public class UserDaoOracleImp implements UserDao{
public String getUser() {
return "The data of Oracle";
}
}
Service层:
# 基类
public interface UserService {
String getUser();
}
# 代码耦合性太高
public class UserServiceImp implements UserService{
private UserDao userDao = new UserDaoImp();
# MySql的实现
private UserDao userDao = new UserDaoMySqImp();
#Oracle的实现
private UserDao userDao = new UserDaoOracleImp();
public String getUser() {
return userDao.getUser();
}
}
public static void main(String[] args) {
UserService userService = new UserServiceImp();
String user = userService.getUser();
System.out.println(user);
}
############### 改进 1.0
public class UserServiceImp implements UserService{
private UserDao userDao;
# 这里是多态实现的,不涉及到具体的,接耦
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public String getUser() {
return userDao.getUser();
}
}
public static void main(String[] args) {
UserService userService = new UserServiceImp();
# 对MySql的实现
((UserServiceImp) userService).setUserDao(new UserDaoMySqImp());
# 对 ((UserServiceImp) userService).setUserDao(new UserDaoOracleImp());
((UserServiceImp) userService).setUserDao(new UserDaoMySqImp());
String user = userService.getUser();
System.out.println(user);
String user = userService.getUser();
System.out.println(user);
}
#### 改进2.0 工厂模式
#### 改进3.0 工程方法模式
二. Spring 依赖倒置实现(DI)(HelloSpring)
1.0 创建 maven 项目
2.0 在 pom.xml 中导入spring 相关的依赖:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.10</version>
</dependency>
</dependencies>
'''
- 在 com.pojo 实体中创建
public class Hello {
private String name;
### 一定要设置 setter 和 getter,spring 通过它来实现
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Hello{" +
"name='" + name + '\'' +
'}';
}
}
4.0 在Resouce 下面创建 beans.xml : ( 到 Spring的文档官可以找到)
<?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="hello" class="com.pojo.Hello">
<property name="name" value="Spring-name">
</property>
</bean>
</beans>
在Test中
public class MyTest {
public static void main(String[] args) {
//获取Spring的上下文对象, 可以去Spring 文档中找到
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Hello hello = (Hello) context.getBean("hello");
System.out.println(hello.toString() );
}
}
- 其中 Hello 是谁创建的 ?
Spring 创建的 - Hello的属性怎么设置
通过Spring 容易来设置的
5.0 用这个来实现一种的东西
首先,在beans中配置:
<?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="mySqlImp" class="com.dao.UserDaoMySqImp" />
<bean id="OracleImp" class="com.dao.UserDaoOracleImp" />
<bean id="userServiceImp" class="com.service.UserServiceImp">
<!-- ref : 创建文件中创建好的对象,
value: 具体的值
-->
<property name="userDao" ref="mySqlImp" />
</bean>
</beans>
在Main 就可以不用new 来实现
import com.dao.UserDaoMySqImp;
import com.dao.UserDaoOracleImp;
import com.service.UserService;
import com.service.UserServiceImp;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTesy {
public static void main(String[] args) {
ApplicationContext contex = new ClassPathXmlApplicationContext("beans.xml");
UserServiceImp userService = (UserServiceImp) contex.getBean("userServiceImp");
System.out.println( userService.getUser());
}
}
如需要改用,只需要在配置文件 pom.xml 中进行修改即可:
ref = "mySqlImp" 换成 ref = "OracleImp" 即可
<bean id="userServiceImp" class="com.service.UserServiceImp">
<property name="userDao" ref="mySqlImp" />
</bean>