Spring入门+IoC注解详情【依赖注入、整合Junit、配置类】---多以案例讲述(二)

简介: Spring入门+IoC注解详情【依赖注入、整合Junit、配置类】---多以案例讲述

4. 依赖注入接口【通过注解加载配置类】【整合junit


4.1 相关注解说明

@RunWith(SpringRunner.class) //spring 整合Junit ,告诉这个类是一个spring的测试类
@ContextConfiguration(classes = 配置类类名.class) //加载配置类
@Ressource(name="xxx") //xxx表示对应Dao包下@Component("xxx")注解标明的类对象
@Component("xxx") //注解标明将该类取明为xxx,作为键存入Ioc容器中 
@Configuration  //注解标明该类是一个配置类
@ComponentScan(basePackages = "扫描需要被加载的类所在的包")


4.2 图集导航

  • UserDao【接口】
1.public interface UserDao3 {
    void selectAll();
}


UserDaoImpl【Dao接口实现类】【数据访问层】

@Component("userDao3")  //把一个类添加到Component容器中
public class UserdaoImpl3  implements UserDao3 {
    @Override
    public void selectAll() {
        System.out.println("userDao3接口 : selectAll");
    }
}

UserService 【接口】【业务逻辑层】

public interface UserService3 {
    void selectAll();
}
  • UserServiceImpl 【Service接口实现类】
//实现类
@Component("userService3")
public class UserServiceImpl3 implements UserService3 {
    @Resource(name = "userDao3") //依赖注入
    private UserDao3 userDao3 ;
    @Override
    public void selectAll() {
        userDao3.selectAll();
        System.out.println("userService3 : selectAll");
    }
}

Demo03Configuration【配置类】

@Configuration //配置类
@ComponentScan(basePackages = {"com.czxy.demo03"})  //所需要扫描的包
public class Demo03Configuration {
}
  • TestDemo03 【测试类】【表示层】
@RunWith(SpringRunner.class) //spring 整合Junit ,告诉这个类是一个spring的测试类
@ContextConfiguration(classes = Demo03Configuration.class) //加载配置类
public class TestDemo03 {
    @Resource(name = "userService3")  //依赖注入
    private UserService3 userService3;
    @Test
    public void testDemo03(){
        userService3.selectAll();
    }
}

测试结果


8715154aefc449759aa0ac189cfdb7c3.png


4.3图集总结

54e7e67f04474b18a8b8055c1a54ad46.png


5.IoC详情【Bean创建】


5.1 相关注解说明


Bean相关注解 
@Component  :将修饰的资源交予spring管理。 value属性:为资源命名(唯一标识)
@Controller :衍生注解,与@Component作用和属性相同。特用于修饰表示层的资源。web层(Servlet层)
@Service    :衍生注解,与@Component作用和属性相同。特用于修饰业务逻辑层的资源。(Service层)
@Repository :衍生注解,与@Component作用和属性相同。特用于修饰数据访问层的资源。 (dao层)


5.2 图集导航 【以Student为例】


c7c94ec385fe46e28741f95069302a9f.png


StudentDao 【dao接口】

public interface StudentDao {
    //查询所有
    void selectAll();
}

StudentdaoImpl 【dao接口实现类】

@Repository("studentDao4")
public class StudentDaoImpl implements StudentDao {
    @Override
    public void selectAll() {
        System.out.println("studentDao4 : selectAll");
    }
}
  • StudentService 【service接口】
public interface StudentService {
    void selectAll();
}


StudentServiceImpl 【service接口实现类】

//将该类放入Ioc容器中,key : 注解名 value: 该StudentServiceImpl类
@Service("studentService4")
public class StudentServiceImpl implements StudentService {
    //依赖注入   studentDao4 = Dao包中放入Ioc中 类的键key[标注注解的名]
    @Resource(name = "studentDao4")
    private StudentDao studentDao;
    @Override
    public void selectAll() {
        studentDao.selectAll();
        System.out.println("studentService4 : selectAll");
    }
}

StudentServlet 【servle实现类】

@Controller("studentController")
public class StudentServlet {
    //依赖注入service
    @Resource(name = "studentService4")
    private StudentService studentService ;
    public void selectAll(){
        studentService.selectAll();
        System.out.println("studentController : selectAll");
    }
}

Demo04Configuration 【配置类】

@Configuration //标注这个一个配置类
@ComponentScan(basePackages = {"com.czxy.demo4_Ioc"}) //标注放入要扫描的包的路径
public class Demo04Configuration {
}

Test04_Student 【测试类】

@RunWith(SpringRunner.class) //标注这是这个spring测试类
@ContextConfiguration(classes = Demo04Configuration.class)  //加载配置类
public class Test04_Student {
    @Resource(name = "studentController")
    private StudentServlet studentServlet;
    @Test
    public void run(){
        studentServlet.selectAll();
    }
}
  • 测试结果


0f1a5494bfea4b07811a3ead922b9646.png


5.3图集总结


ea49998ebbd84b139206ca771ec9a5f1.png


6.基本注解归纳


@RunWith(SpringRunner.class) //spring 整合Junit ,告诉这个类是一个spring的测试类
@ContextConfiguration(classes = 配置类类名.class) //加载配置类
@Ressource(name="xxx") //xxx表示对应Dao包下@Component("xxx")注解标明的类对象
@Component("xxx") //注解标明将该类取明为xxx,作为键存入Ioc容器中 
@Configuration  //注解标明该类是一个配置类
@ComponentScan(basePackages = "扫描需要被加载的类所在的包")
Bean相关注解 
@Component  :将修饰的资源交予spring管理。 value属性:为资源命名(唯一标识)
@Controller :衍生注解,与@Component作用和属性相同。特用于修饰表示层的资源。web层(Servlet层)
@Service    :衍生注解,与@Component作用和属性相同。特用于修饰业务逻辑层的资源。(Service层)
@Repository :衍生注解,与@Component作用和属性相同。特用于修饰数据访问层的资源。 (dao层)


7. 图集总结


类对象都可以通过@Component() 注解加入IoC容器,但是为什么又要用@Controller、@Service、@Repository注解呢?


是为了更好的区分不同层,更好的体现三层架构模式


在我们没有为他们赋予名称时,它会默认命名为 ---> 类名的首字母该小写


31bc9c0eda9e4c27b127f5a155046f1e.png




目录
打赏
0
0
0
0
11
分享
相关文章
Spring IOC—基于注解配置和管理Bean 万字详解(通俗易懂)
Spring 第三节 IOC——基于注解配置和管理Bean 万字详解!
171 26
【SpringFramework】Spring整合JUnit
本文简述Spring整合JUnit单元测试组件的通用方法,可以简化Spring框架测试。
118 14
SpringBoot缓存注解使用
Spring Boot 提供了一套方便的缓存注解,用于简化缓存管理。通过 `@Cacheable`、`@CachePut`、`@CacheEvict` 和 `@Caching` 等注解,开发者可以轻松地实现方法级别的缓存操作,从而提升应用的性能和响应速度。合理使用这些注解可以大大减少数据库的访问频率,优化系统性能。
215 89
Spring MVC常用的注解
@RequestMapping:用于处理请求 url 映射的注解,可用于类或方法上。用于类上,则表示类中 的所有响应请求的方法都是以该地址作为父路径。 @RequestBody:注解实现接收http请求的json数据,将json转换为java对象。 @ResponseBody:注解实现将conreoller方法返回对象转化为json对象响应给客户。 @Controller:控制器的注解,表示是表现层,不能用用别的注解代替 @RestController : 组合注解 @Conntroller + @ResponseBody @GetMapping , @PostMapping , @Put
Spring Boot的核心注解是哪个?他由哪几个注解组成的?
Spring Boot的核心注解是@SpringBootApplication , 他由几个注解组成 : ● @SpringBootConfiguration: 组合了- @Configuration注解,实现配置文件的功能; ● @EnableAutoConfiguration:打开自动配置的功能,也可以关闭某个自动配置的选项 ● @ComponentScan:Spring组件扫描
什么是Spring IOC 和DI ?
IOC : 控制翻转 , 它把传统上由程序代码直接操控的对象的调用权交给容 器,通过容器来实现对象组件的装配和管理。所谓的“控制反转”概念就是对组件对象控制权的转 移,从程序代码本身转移到了外部容器。 DI : 依赖注入,在我们创建对象的过程中,把对象依赖的属性注入到我们的类中。
SpringBoot+@Async注解一起用,速度提升
本文介绍了异步调用在高并发Web应用性能优化中的重要性,对比了同步与异步调用的区别。同步调用按顺序执行,每一步需等待上一步完成;而异步调用无需等待,可提升效率。通过Spring Boot示例,使用@Async注解实现异步任务,并借助Future对象处理异步回调,有效减少程序运行时间。
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于注解的整合
本文介绍了Spring Boot集成MyBatis的两种方式:基于XML和注解的形式。重点讲解了注解方式,包括@Select、@Insert、@Update、@Delete等常用注解的使用方法,以及多参数时@Param注解的应用。同时,针对字段映射不一致的问题,提供了@Results和@ResultMap的解决方案。文章还提到实际项目中常结合XML与注解的优点,灵活使用两者以提高开发效率,并附带课程源码供下载学习。
25 0
|
2月前
|
SpringBoot:SpringBoot通过注解监测Controller接口
本文详细介绍了如何通过Spring Boot注解监测Controller接口,包括自定义注解、AOP切面的创建和使用以及具体的示例代码。通过这种方式,可以方便地在Controller方法执行前后添加日志记录、性能监控和异常处理逻辑,而无需修改方法本身的代码。这种方法不仅提高了代码的可维护性,还增强了系统的监控能力。希望本文能帮助您更好地理解和应用Spring Boot中的注解监测技术。
75 16
课时148:junit测试工具
课时148介绍了JUnit测试工具的使用,包括定义、配置和编写测试程序。JUnit是流行的用例测试工具,用于确保代码稳定性。