Java:Spring框架之IoC设计思想

Spring框架

       Spring框架是由于软件开发的复杂性而创建的。Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅仅限于服务器端的开发。从简单性、可测试性和松耦合性角度而言,绝大部分Java应用都可以从Spring中受益。  

IOC 

控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度。其中最常见的方式叫做依赖注入(Dependency Injection,简称DI),还有一种方式叫“依赖查找”(Dependency Lookup)。通过控制反转,对象在被创建的时候,由一个调控系统内所有对象的外界实体将其所依赖的对象的引用传递给它。也可以说,依赖被注入到对象中。

传统的项目开发

创建苹果类

package com.jt;

public class Apple {
    public void say(){
        System.out.println("hello");
    }
}

创建橘子类

package com.jt;

public class Orange {
    public void say(){
        System.out.println("world");
    }
}

创建测试类

package com.jt;

import org.junit.jupiter.api.Test;

public class TestDemo {
    @Test
    public void test(){
        Apple apple = new Apple();
        //Orange orange = new Orange();
        apple.say();
        //orange.say();
    }
}

由此可见:每个对象都是通过new的方式绑定,则属性与对象的耦合性非常高,而且到后期业务开发时需要修改属性值时需要修改源码,对代码来说很不友好,后去拓展也不方便。

可以通过面向接口的使用多态的方式降低耦合度,但只是在一定程度上降低耦合度。

所以需要使用IOC来降低耦合度

两种方式:1.通过配置文件方式。2.通过注解的方式

两者的区别:通过配置文件可以实现配置文件和java代码完全分离,但是后期实现大型项目的时候会有大量的对象太多不方便管理,用过注解的方式易于管理,虽然没有完全分离但是优点是大过于缺点的,所以通常使用注解的方式管理。

话不多说上代码~

IoC设计思想创建项目

创建接口Eat

package com.jt;

public interface Eat {
    void say();
}

创建Apple类和Orange类

package com.jt;

import org.springframework.stereotype.Component;

@Component
public class Apple implements Eat{
    @Override
    public void say(){
        System.out.println("吃苹果");
    }
}

package com.jt;

public class Orange implements Eat{
    @Override
    public void say(){
        System.out.println("吃橘子");
    }
}

创建User类 

package com.jt;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class User {

    @Autowired//注入 将容器中的对象注入到属性中
    @Qualifier("apple")
//    @Resource(name = "apple") // 不建议使用
    private Eat eat;

    public void say(){
        eat.say();
    }
}

创建配置类SpringConfig

package com.jt.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration//配置类
@ComponentScan("com.jt")//扫描包
public class SpringConfig {
}

创建测试类

package com.jt;

import com.jt.config.SpringConfig;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestDemo {
    @Test
    public void test(){
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        User user = context.getBean(User.class);
        user.say();
    }
}

运行结果

由此可见 使用IoC设计思想利用注解方式可以大大的降低属性与对象之间的耦合度 

三层代码结构

控制层 Controller 实现前后端参数接收的. @Controller注解

业务层 Service 实现后端业务操作的 @Service注解

持久层 Dao/Mapper 实现持久层业务操作 @Repository 

话不多说上代码~

从下往上创建

创建三层代码结构

创建持久层UserMapper接口和UserMapperImpl实现类

package com.jt.mapper;

public interface UserMapper {
    void addUser();
}

package com.jt.mapper;

import org.springframework.stereotype.Repository;

@Repository
public class UserMapperImpl implements UserMapper {

    @Override
    public void addUser() {
        System.out.println("新增用户DDD");
    }
}

创建业务层UserService接口和UserServiceImpl实现类

package com.jt.service;

public interface UserService {
    void addUser();
}

package com.jt.service;

import com.jt.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void addUser() {
        userMapper.addUser();
    }
}

创建控制层UserController类

package com.jt.controller;

import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    public void addUser(){
        userService.addUser();
    }
}

创建配置类SpringConfig

package com.jt.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Configuration
@ComponentScan("com.jt")
public class SpringConfig {

}

创建测试类TestDemo

package com.jt;

import com.jt.config.SpringConfig;
import com.jt.controller.UserController;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestDemo {
    @Test
    public void test(){
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        UserController bean = context.getBean(UserController.class);
        bean.addUser();
    }
}

总结:

@Component :组件通用注解,常用于Model类

@ComponentScan ("包路径"):包扫描,常用语配置类,扫描@Component放到Spring容器中

@Controller :常用于对Controller实现类进行标注

@Service:常用于对Service实现类进行标注

@Repository:常用于对DAO实现类进行标注

@Value :注入普通类型属性

@Autowired :注入对象类型,默认按照类型注入。结合@Qualifier注解完成按名称的注入。

@Resource :注入对象类型

Ioc就是将创建对象的过程放在容器中处理,通过容器管理对象。

实际上IoC处理的就是业务逻辑之间的耦合关系也就是业务层和Service和持久层Dao之间的解耦合。

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Niko_Guo486

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值