Spring IOC(五个类注解)

复制代码
controller、service、Repository、Component 、Configuration



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//启动类

相关推荐
fengxin_rou几秒前
Java垃圾回收机制深度解析:从原理到实战
java·jvm·性能优化·gc·垃圾回收
专注搞钱2 分钟前
用Python写了个SPC自动分析工具,效率提升10倍
开发语言·python
弗锐土豆2 分钟前
使用eclipse、java、maven、j60870、oceanbase按照IEC104协议采集、存储电力数据
java·oceanbase·电表·iec104·抄表
小则又沐风a3 分钟前
进程最终篇---进程控制(模拟实现xshell)
java·linux·服务器·前端
番石榴AI3 分钟前
JiaJiaOCR-2.2.0:面向Java ocr的开源库
java·ocr
码云骑士6 分钟前
【3.Java基础】Java运算符详解:从算数运算到逻辑判断,一篇文章全部掌握
java·开发语言
yijianace9 分钟前
Python爬虫实战:ThreadPoolExecutor多线程采集书籍信息与图片下载
开发语言·爬虫·python
Web打印10 分钟前
HttpPrinter web打印控件 官方文档(https://wiki.httpprinter.com/)快速检索目录
java·javascript·chrome
我登哥MVP10 分钟前
Spring Boot 从“会用”到“精通”:内容协商原理
java·spring boot·后端·spring·java-ee·maven·lua
cfm_291414 分钟前
Java JVM 零基础入门
java·jvm