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

相关推荐
weixin_445054727 小时前
力扣热题52
开发语言·python
mengyoufengyu7 小时前
JupyterLab4.5安装使用
python·jupyter·jupyterlab
weixin_462446237 小时前
Python 使用阿里云 STS 获取临时访问凭证并上传文件至 OSS:Flask API 实现
python·阿里云·flask
这就是佬们吗7 小时前
告别 Node.js 版本冲突:NVM 安装与使用全攻略
java·linux·前端·windows·node.js·mac·web
何中应7 小时前
@Autowrited和@Resource注解的区别及使用场景
java·开发语言·spring boot·后端·spring
一条咸鱼_SaltyFish7 小时前
[Day16] Bug 排查记录:若依框架二次开发中的经验与教训 contract-security-ruoyi
java·开发语言·经验分享·微服务·架构·bug·开源软件
橙露7 小时前
从零基础到实战:Python 数据分析三剑客(Pandas+NumPy+Matplotlib)核心应用指南
python·数据分析·pandas
荒诞硬汉7 小时前
递归的学习
java·学习
孤独天狼7 小时前
java设计模式
java
一勺菠萝丶7 小时前
Java 对接 PLC 实战:西门子 PLC 与永宏 PLC 通讯方式全面对比
java·开发语言·python