当前位置: 首页 > news >正文

Spring IOC(五个类注解)

controller、service、Repository、ComponentConfiguration



package com.java.ioc;

import com.java.ioc.Controller.HelloController;
import com.java.ioc.rep.UserRepository;
import com.java.ioc.service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class DemoApplication {

   public static void main(String[] args) {
      //告诉Spring帮我们管理对象,ioc:五个类注解:controller
       ConfigurableApplicationContext context= SpringApplication.run(DemoApplication.class, args);
1.//    HelloController tean=context.getBean(HelloController.class);
//    tean.prin();

//    //等于HelloController helloController=new HelloController();
//    //helloController.prin();

2.//    HelloController bean2=(HelloController) context.getBean("helloController");
//    bean2.prin();

3.//    HelloController tean1=(HelloController) context.getBean("aaa");//@Controller("aaa")重命名
//    tean1.prin();

4.//    HelloController bean3=context.getBean("helloController",HelloController.class);
//    bean3.prin();

//
//    //ioc:类注解:service
//    UserService dean1=context.getBean(UserService.class);
//    dean1.p();
//
//    //ioc:类注解:Repository
//    UserRepository dean2=context.getBean(UserRepository.class);
//    dean2.p();
//
      //ioc:类注解:Component

      //ioc:类注解:Configuration
   }

}

eg:

相同点:Component的衍生注解

不同点:

@Controller:控制层,接收参数

控制层必须使用Controller其他可以替换但不建议,因为不同注解含义不同可能存在不必要麻烦)

@Service:业务逻辑层

@Repository:数据层

@Configuratrin:配置层

@Component:组件层(但边界不是很清晰)

@Bean

//Spring默认扫描路径为启动类所在路径
//@ComponentScan("com.java.ioc")//加这个后以这个为准@SpringBootApplication//启动类

相关文章:

  • 【PyTorch】.pt文件
  • SQL Server运维实战:十大高频问题分析与解决方案
  • C语言 第五章 指针(1)
  • Linux内核传输层DCCP分析
  • 靶场(十二)---小白心得靶场思路---Cockpit
  • 基于SpringBoot+Vue开发的在线音乐视频播放平台
  • 深入理解嵌入式开发中的三个重要工具:零长度数组、container_of 和 typeof
  • docker4-容器命令及其案例
  • 【MySQL】MySQL是如何处理请求的?
  • 详解布隆过滤器及其模拟实现
  • HuskyLens:让AI视觉开发更简单
  • 定时器相关
  • 【css酷炫效果】纯CSS实现3D翻转卡片动画
  • 【Pandas】pandas Series dt
  • VBA技术资料MF279:点击任意工作表任意单元格显示其地址
  • Qt Graphics View
  • Vue3 + TS组件封装指南
  • 大模型面试高频考点-显存占用
  • QoS 技术详解:原理、应用与配置实践
  • Java中,`Thread`类的`sleep`方法使用整理
  • 美国务卿与以色列总理通话,讨论加沙局势
  • 见微知沪|科学既要勇攀高峰,又要放低身段
  • AI赋能科学红毯,机器人与科学家在虚实之间叩问“科学精神”
  • 舞者王佳俊谈“与AI共舞”:像多了一个舞伴,要考虑它的“感受”
  • 女生“生理期请病假要脱裤子证明”?高校回应:视频经处理后有失真等问题
  • 小雨伞保险经纪母公司手回集团通过港交所聆讯