SpringBoot - MVC三层架构注解注入到容器中与从IOC容器获取实例注解

MVC三层架构注解

@RestController 控制层

@Controller 控制层

@Service 业务层

@Repository 持久层

@Component

作用:把类注入到IOC容器当中

范围:类上

参数:value 给类起类名


从IOC容器中获取实例注解

@Autowired 

作用:根据类自动注入

范围:变量上

 

@Qualifier

作用:配合@Autowired一起使用,根据注入时类其的类名注入

参数:value

 

@Resouce

作用:可以根据类注入或根据类名称注入

范围:变量上

参数:type ,name

 

@Value

作用:给基本数据类型属性注入值

范围:基本数据类型上

参数:value

 

基本使用:

    //Autowired
    @Autowired
    Student student_a;

    @Autowired
    @Qualifier(value = "student")
    Student student_aq;
    
    //Resource
    @Resource(name = "student")
    Student student_rn;

    @Resource(type = Student.class)
    Student student_rt;

    @Resource(type = Student.class,name = "student")
    Student student_rtn;
    
    //value
    @Value(value="Levi")
    private String name;

    @Value(value="18")
    private int age;

 

posted on 2022-12-20 15:34  Mikasa-Ackerman  阅读(79)  评论(0编辑  收藏  举报

导航