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

相关推荐
莫逸风1 小时前
【AgentScope 2.0】 0. 学习指南
java·llm·agent·agentscope
C^h2 小时前
python函数学习
人工智能·python·机器学习
Fanta丶2 小时前
4.Python set()集合、dict(字典、映射)、 数据容器的通用功能
python
决战灬2 小时前
langgraph之interrupt(理论篇)
人工智能·python·agent
z123456789862 小时前
2026最新两款AI编程工具深度对比实测
java·数据库·ai编程
兜客互动2 小时前
2026年AI关键词拓展挖掘软件,高效助力内容创作精准获流
人工智能·python
weixin_538601972 小时前
智能体测开Day30pytest测试框架
python
MC皮蛋侠客2 小时前
uv 系列(七):CI/CD、Docker 与私有索引——生产级交付
python·ci/cd·docker·uv
邪神与厨二病3 小时前
牛客周赛 Round 153
python·算法
yaoxin5211233 小时前
470. Java 反射 - Member 接口与 AccessFlag
java·开发语言·python