面试官:为什么要用 Spring?它到底解决了什么问题?
点击关注公众号,Java干货及时送达
为什么要用 Spring?它到底解决了什么问题?
目的:对比传统方式和 Spring注入方式创建对象以达到解耦的目的,以Service层调用 Dao层为例。
方式一:传统方式
1.Service层
/**
* @author :Prannt
* @description :Service层
* @program : Test
*/
public class UserService {
public void add(){
System.out.println("service add...");
UserDao dao = new UserDaoImpl();
dao.query();
}
}
2.UserDao接口
/**
* @description :接口中只有一个抽象方法
*/
public interface UserDao {
void query();
}
3.UserDao接口的实现类
/**
* @description :接口的实现类,重写 query方法
*/
public class UserDaoImpl implements UserDao{
@Override
public void query() {
System.out.println("dao query...");
}
}
//测试
public class test {
@Test
public void test1(){
UserService service = new UserService();
service.add();
}
}
Spring Boot 基础就不介绍了,推荐下这个实战教程:https://github.com/javastacks/spring-boot-best-practice
结论一:由以上过程可以看出,在UserService类中直接调用实现类的query方法,一旦实现类出问题,UserService立即报错,具有高度的耦合性
方式二:Spring注入对象
2.Service层import Dao.UserDao;/** * @author :Prannt * @description : * @program : Demo01 */public class UserService { public void add(){ System.out.println("service add......"); dao.update(); } //创建dao类型的属性,生成对应的set方法 private UserDao dao; public void setDao(UserDao dao) { this.dao = dao; }}3.UserDao接口public interface UserDao { void update();}4.接口的实现类public class UserDaoImpl implements UserDao{ @Override public void update() { System.out.println("Dao update......"); }}推荐一个 Spring Boot 基础教程及实战示例:https://github.com/javastacks/spring-boot-best-practice测试import Service.UserService;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestBean { @Test public void testBean(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml"); UserService userService = context.getBean("userService",UserService.class); userService.add(); }}测试结果" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2">1.xml文件配置bean
2.Service层import Dao.UserDao;/** * @author :Prannt * @description : * @program : Demo01 */public class UserService { public void add(){ System.out.println("service add......"); dao.update(); } //创建dao类型的属性,生成对应的set方法 private UserDao dao; public void setDao(UserDao dao) { this.dao = dao; }}3.UserDao接口public interface UserDao { void update();}4.接口的实现类public class UserDaoImpl implements UserDao{ @Override public void update() { System.out.println("Dao update......"); }}推荐一个 Spring Boot 基础教程及实战示例:https://github.com/javastacks/spring-boot-best-practice测试import Service.UserService;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestBean { @Test public void testBean(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml"); UserService userService = context.getBean("userService",UserService.class); userService.add(); }}测试结果" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2"> "1.0" encoding="UTF-8"?>
"http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
"userService" class="Service.UserService">
"dao" ref="Dao">
"Dao" class="Dao.UserDaoImpl">
2.Service层import Dao.UserDao;/** * @author :Prannt * @description : * @program : Demo01 */public class UserService { public void add(){ System.out.println("service add......"); dao.update(); } //创建dao类型的属性,生成对应的set方法 private UserDao dao; public void setDao(UserDao dao) { this.dao = dao; }}3.UserDao接口public interface UserDao { void update();}4.接口的实现类public class UserDaoImpl implements UserDao{ @Override public void update() { System.out.println("Dao update......"); }}推荐一个 Spring Boot 基础教程及实战示例:https://github.com/javastacks/spring-boot-best-practice测试import Service.UserService;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestBean { @Test public void testBean(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml"); UserService userService = context.getBean("userService",UserService.class); userService.add(); }}测试结果" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2">2.Service层
2.Service层import Dao.UserDao;/** * @author :Prannt * @description : * @program : Demo01 */public class UserService { public void add(){ System.out.println("service add......"); dao.update(); } //创建dao类型的属性,生成对应的set方法 private UserDao dao; public void setDao(UserDao dao) { this.dao = dao; }}3.UserDao接口public interface UserDao { void update();}4.接口的实现类public class UserDaoImpl implements UserDao{ @Override public void update() { System.out.println("Dao update......"); }}推荐一个 Spring Boot 基础教程及实战示例:https://github.com/javastacks/spring-boot-best-practice测试import Service.UserService;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestBean { @Test public void testBean(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml"); UserService userService = context.getBean("userService",UserService.class); userService.add(); }}测试结果" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2"> import Dao.UserDao;
/**
* @author :Prannt
* @description :
* @program : Demo01
*/
public class UserService {
public void add(){
System.out.println("service add......");
dao.update();
}
//创建dao类型的属性,生成对应的set方法
private UserDao dao;
public void setDao(UserDao dao) {
this.dao = dao;
}
}
2.Service层import Dao.UserDao;/** * @author :Prannt * @description : * @program : Demo01 */public class UserService { public void add(){ System.out.println("service add......"); dao.update(); } //创建dao类型的属性,生成对应的set方法 private UserDao dao; public void setDao(UserDao dao) { this.dao = dao; }}3.UserDao接口public interface UserDao { void update();}4.接口的实现类public class UserDaoImpl implements UserDao{ @Override public void update() { System.out.println("Dao update......"); }}推荐一个 Spring Boot 基础教程及实战示例:https://github.com/javastacks/spring-boot-best-practice测试import Service.UserService;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestBean { @Test public void testBean(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml"); UserService userService = context.getBean("userService",UserService.class); userService.add(); }}测试结果" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2">3.UserDao接口
2.Service层import Dao.UserDao;/** * @author :Prannt * @description : * @program : Demo01 */public class UserService { public void add(){ System.out.println("service add......"); dao.update(); } //创建dao类型的属性,生成对应的set方法 private UserDao dao; public void setDao(UserDao dao) { this.dao = dao; }}3.UserDao接口public interface UserDao { void update();}4.接口的实现类public class UserDaoImpl implements UserDao{ @Override public void update() { System.out.println("Dao update......"); }}推荐一个 Spring Boot 基础教程及实战示例:https://github.com/javastacks/spring-boot-best-practice测试import Service.UserService;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestBean { @Test public void testBean(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml"); UserService userService = context.getBean("userService",UserService.class); userService.add(); }}测试结果" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2"> public interface UserDao {
void update();
}
2.Service层import Dao.UserDao;/** * @author :Prannt * @description : * @program : Demo01 */public class UserService { public void add(){ System.out.println("service add......"); dao.update(); } //创建dao类型的属性,生成对应的set方法 private UserDao dao; public void setDao(UserDao dao) { this.dao = dao; }}3.UserDao接口public interface UserDao { void update();}4.接口的实现类public class UserDaoImpl implements UserDao{ @Override public void update() { System.out.println("Dao update......"); }}推荐一个 Spring Boot 基础教程及实战示例:https://github.com/javastacks/spring-boot-best-practice测试import Service.UserService;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestBean { @Test public void testBean(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml"); UserService userService = context.getBean("userService",UserService.class); userService.add(); }}测试结果" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2">4.接口的实现类
2.Service层import Dao.UserDao;/** * @author :Prannt * @description : * @program : Demo01 */public class UserService { public void add(){ System.out.println("service add......"); dao.update(); } //创建dao类型的属性,生成对应的set方法 private UserDao dao; public void setDao(UserDao dao) { this.dao = dao; }}3.UserDao接口public interface UserDao { void update();}4.接口的实现类public class UserDaoImpl implements UserDao{ @Override public void update() { System.out.println("Dao update......"); }}推荐一个 Spring Boot 基础教程及实战示例:https://github.com/javastacks/spring-boot-best-practice测试import Service.UserService;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestBean { @Test public void testBean(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml"); UserService userService = context.getBean("userService",UserService.class); userService.add(); }}测试结果" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2"> public class UserDaoImpl implements UserDao{
@Override
public void update() {
System.out.println("Dao update......");
}
}
推荐一个 Spring Boot 基础教程及实战示例:https://github.com/javastacks/spring-boot-best-practice
2.Service层import Dao.UserDao;/** * @author :Prannt * @description : * @program : Demo01 */public class UserService { public void add(){ System.out.println("service add......"); dao.update(); } //创建dao类型的属性,生成对应的set方法 private UserDao dao; public void setDao(UserDao dao) { this.dao = dao; }}3.UserDao接口public interface UserDao { void update();}4.接口的实现类public class UserDaoImpl implements UserDao{ @Override public void update() { System.out.println("Dao update......"); }}推荐一个 Spring Boot 基础教程及实战示例:https://github.com/javastacks/spring-boot-best-practice测试import Service.UserService;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestBean { @Test public void testBean(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml"); UserService userService = context.getBean("userService",UserService.class); userService.add(); }}测试结果" linktype="text" imgurl="" imgdata="null" data-itemshowtype="0" tab="innerlink" data-linktype="2"> import Service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestBean {
@Test
public void testBean(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
UserService userService = context.getBean("userService",UserService.class);
userService.add();
}
}
结论二:观察以上过程,在UserService类中,没有直接new实现类,而是通过将Dao注入外部配置文件中的方式,使用“第三方文件”来达到解耦的目的。
总结
第一种传统方式创建对象,就像图一中的齿轮组。如果有一个齿轮出了问题,就可能会影响到整个齿轮组的正常运转。
而用Spring注入对象犹如图二的齿轮,利用第三方xml文件使任意两个齿轮之间无必然联系,B齿轮坏了也影响不到A、C、D齿轮。齿轮组相互之间的依赖关系降到最低。
原文链接:https://blog.csdn.net/qq_38050259/article/details/113414419
版权声明:本文为CSDN博主「Prannt」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
关注Java技术栈看更多干货
评论