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

相关推荐
2401_8445829518 分钟前
工具包:软件架构设计的实用技巧与经验分享
python
RFID固定资产管理系统38 分钟前
适配媒体行业的固定资产管理软件有哪些功能与核心优势
大数据·人工智能·python·媒体
码智社1 小时前
JSON 零基础到资深全体系教程
java·json
SunnyDays10111 小时前
Python 为 PowerPoint 添加动画:进入、退出、动作路径与文本动画(详解)
python·powerpoint·动画·动画效果·文本动画
wgego2 小时前
基础的反序列化一些总结(php和java)
java·开发语言·笔记
难以怀瑾2 小时前
pytest.param`与 allure.dynamic.title()全解析
python
机建狂魔3 小时前
Codex 接入第三方模型 API 实战:以 Mimo 为例
java·服务器·数据库·ai·ai编程·codex
山峰哥3 小时前
数据库工程与SQL调优:从慢查询到秒级响应的实战之路
java·开发语言·数据库·sql·深度优先·启发式算法
乐观的Terry3 小时前
11、发布系统-用户认证与权限体系
java·spring boot·spring·spring cloud·mybatis
前端开发张小七3 小时前
Java 学习笔记 · 第二课:面向对象核心(封装、继承、多态)及接口与异常
java·后端·程序员