欢迎光临散文网 会员登陆 & 注册

三大框架 —— 业务层框架Spring+IoC+DI

2020-11-28 11:28 作者:每天一个秃顶小技巧  | 我要投稿

1. 初识Spring框架

Spring是一个业务层框架,实现类ioc容器和aop。
用spring添加依赖

业务层主要处理业务逻辑如:

  • 注册时在业务层判断用户名是否存在。

  • 获取商品价格时,判断用户是否是会员,商品有没有参加店铺活动,有没有参加商城活动。

@Service :表示这个类是业务层的类,spring就会创建对象,放在容器中
@autowired:由spring提供的,spring框架从容器中找到一个类型为UserService 的对象并赋值。容器管理对象的生命周期。容器类似于池(池有大小)。
eg:
@autowired
UserService userService;

2. IoC控制反转

2.1 实现原理

IOC(控制反转)就是依赖倒置原则的一种代码设计思路。就是把原先在代码里面需要实现的对象创建、对象之间的依赖,反转给容器来帮忙实现。
Spring IOC容器通过xml,注解等其它方式配置类及类之间的依赖关系,完成了对象的创建和依赖的管理注入。实现IOC的主要设计模式是工厂模式。

优点:

  • 集中管理,实现类的可配置和易管理。

  • 降低了类与类之间的耦合度。

2.2 代码实现举例

package com.tedu.springIOC01.service; //定义一个业务层的接口 public interface UserService { public String register(); }


package com.tedu.springIOC01.service; //接口的实现类 import org.springframework.stereotype.Service; //@Service 表明这是一个业务层类,由框架创建对象 @Service public class UserServiceImpl implements UserService{ @Override public String register() { //判断用户名是否注册过 return "注册成功"; } }


//控制层 @RestController public class UserController { //@Autowired 注解,则由框架给userService赋值,不用程序员创建 @Autowired UserService userService; @RequestMapping("/register") public String register() { String result=userService.register(); return result; } }

2.3 相关面试题

IOC和DI的关系

DI(依赖注入):由IOC容器在运行期间, 动态地将某种依赖关系注入到对象之中。

关系:
IoC(控制反转)是设计思想,IoC有三个核心:BeanFactory、反射、DI(依赖注入)。BeanFactory利用反射实现对象的创建,DI实现对象关系管理。

什么是自动装配

利用注解方式,我们只需要写@Autowired注解,底层就会去容器中找对应的对象,如果有获取到,利用反射调用其对应的set方法,设值。而这个调用过程都是自动,我们没有手工去写set方法。所以这个过程也称为自动装配。

3. 框架原理 (代码实现)

package com.tedu.mall; //业务层 public class UserServiceImpl { }


package com.tedu.mall; //控制层 import org.spring.ioc.Autowired; public class UserController { @Autowired UserServiceImpl userServiceImpl; }


package org.spring.ioc; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; //定义注解 @Target(ElementType.FIELD) //定义注解的作用目标 @Retention(RetentionPolicy.RUNTIME)//定义注解的使用范围 public @interface Autowired { }


package org.spring.ioc; import java.lang.reflect.Field; import java.util.HashMap; import java.util.Set; import com.tedu.mall.UserController; import com.tedu.mall.UserServiceImpl; //spring框架 public class SpringIOCMain { private static Object object; static HashMap<String, Object> container = new HashMap<>(); //容器 public static void main(String[] args) throws Throwable { //扫描类,创建对象,放到容器 loadObject(); //判断属性有没有加autowired注解,加了给属性赋值 autowiredProcess(); //测试userController的userServiceImpl属性有没有值 UserController userController = (UserController) container.get("userController"); System.out.println(userController.toString()); } private static void loadObject() { UserController userController = new UserController(); container.put("userController", userController);//key:类名的首字母小写   value的值可以随便 UserServiceImpl userServiceImpl = new UserServiceImpl(); container.put("userServiceImpl", userServiceImpl); } private static void autowiredProcess() throws Throwable { // 遍历容器中的所有对象 Set<String> keySet = container.keySet(); for (String key : keySet) { System.out.println("key="+key); //输出key=userController key=userServiceImpl //得到对象 Object object  = container.get(key); //得到对象的类对象 Class clazz = object.getClass(); //取出对象的所有属性 Field[] fields = clazz.getDeclaredFields(); //判断属性有没有加autowired注解 for (Field field : fields) { Autowired autowired = field.getAnnotation(Autowired.class); //如果加了,则从容器中找到对应的对象,赋值 if (autowired!=null) { //serviceInstance是userServiceImpl对象 Object serviceInstance = container.get(field.getName()); //给属性赋值 field.setAccessible(true);//变成public field.set(object, serviceInstance); } } } } }

来源:CSDN
作者:Like0217
原文:三大框架 -- 业务层框架Spring+IoC+DI


三大框架 —— 业务层框架Spring+IoC+DI的评论 (共 条)

分享到微博请遵守国家法律