spring04:注解使用

spring04:注解使用



前言:


提示:以下是本篇文章正文内容:

一、 @Autowired + @Qualifier和 @Resource 和 @nullable

1. @Autowired +

在这里插入图片描述

2. @Resource :使用在类的属性上面(和@Autowired类似)

3. @nullable

在这里插入图片描述

二、 @Component 和 @Repository 和 @ Service 和 @Controller +(@Value)

作用:让spring扫描,可以识别bean。

使用value注入值

1. @Component + @Value(“ xxx ”)

在这里插入图片描述

2. @Repository

在这里插入图片描述

2. @ Service

在这里插入图片描述

2. @Controller

在这里插入图片描述

三、 @Scope

在这里插入图片描述



四、使用java的方式配置Spring(在springboot里面常见)

configuration就相当于一个配置类

在这里插入图片描述



在这里插入图片描述



在这里插入图片描述



在这里插入图片描述



具体的bean.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--指定要扫描的包,这个包下的注解就会生效-->
    <!--写完MVC之后,直接让beans.xml(容器)扫描这个huxiao的大包   -->
    <context:component-scan base-package="com.huxiao"></context:component-scan>
    <context:annotation-config/>

    // cat 和 dog的 bean对象:
    <bean id="cat" class="com.huxiao.pojo.Cat"></bean>
    <bean id="dog" class="com.huxiao.pojo.Dog"></bean>
    
</beans>



总结

提示:这里对文章进行总结:

💕💕💕

相关推荐

  1. spring中@validate注解使用

    2024-04-11 19:06:01       68 阅读
  2. Spring】@Scheduled 定时器注解使用

    2024-04-11 19:06:01       39 阅读
  3. 使用 @AspectJ 注解配置 Spring AOP

    2024-04-11 19:06:01       37 阅读
  4. Spring--注解

    2024-04-11 19:06:01       35 阅读
  5. spring注解

    2024-04-11 19:06:01       27 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-11 19:06:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-11 19:06:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-11 19:06:01       82 阅读
  4. Python语言-面向对象

    2024-04-11 19:06:01       91 阅读

热门阅读

  1. Linux 终端显示 Git 当前所在分支

    2024-04-11 19:06:01       33 阅读
  2. Open CASCADE学习|迭代NCollection_Sequence<gp_Pnt>

    2024-04-11 19:06:01       34 阅读
  3. npm常用命令详细介绍

    2024-04-11 19:06:01       35 阅读
  4. 运维记录 会产生无用日志的服务器

    2024-04-11 19:06:01       32 阅读
  5. spring事务问题的解决和处理

    2024-04-11 19:06:01       39 阅读
  6. 【数学建模】通过调整飞行角度使飞机顺利飞行

    2024-04-11 19:06:01       35 阅读
  7. MySQL的sql_mode模式简介

    2024-04-11 19:06:01       33 阅读
  8. 小红书12大限流原因

    2024-04-11 19:06:01       36 阅读
  9. Python 爬虫基础:利用 BeautifulSoup 解析网页内容

    2024-04-11 19:06:01       33 阅读
  10. Kolla-ansible部署OpenStack集群

    2024-04-11 19:06:01       31 阅读
  11. 字符串判等 51Nod-3288 2024年4月10日

    2024-04-11 19:06:01       37 阅读