• 【后端】Spring注解开发


    ✨使用注解开发

    注意 :必须开启注解的支持 让注解生效(在 applicationContext.xml 中配置)

    <!--    指定要扫描的包 这个包下注解就会生效-->
    <context:component-scan base-package="com.example"/>
    <context:annotation-config/>
    

    Maven依赖

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.9</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    

    applicationContext.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"
           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">
    
    <!--    指定要扫描的包 这个包下注解就会生效-->
        <context:component-scan base-package="com.example"/>
        <context:annotation-config/>
        
    </beans>
    

    ✨自动装配注解

    @Autowired 自动装配通过类型、名字匹配

    @Qualifier

    如果@Autowired不能唯一自动装配上属性(多个不同id的bean标签对应同一个类)

    则需要通过 @Qualifier(value = "xxx")

    @Nullable 字段标记了这个注解 说明这个字段可以为Null

    @Resource 自动装配通过名字、类型匹配


    ✨衍生注解

    @Component 组件 说明这个类被Spring管理了 是Bean

    在web开发中 按照MVC三层架构开发

    • dao -> @Repository
    • service -> @Service
    • controller -> @Controller

    这四个注解功能一样 都是将某个类注册到Spring中 装配Bean


    ✨作用域

    @Scope


    ✨AOP

    Maven依赖

    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.7</version>
        <scope>runtime</scope>
    </dependency>
    

    applicationContext.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: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/aop
            https://www.springframework.org/schema/aop/spring-aop.xsd">
    
    <!--    开启注解支持-->
    <!--    JDK(默认 proxy-target-class="false")-->
    <!--    CGlib(proxy-target-class="true")-->
        <aop:aspectj-autoproxy/>
    </beans>
    

    注解实现AOP

    @Aspect 标注这个类是一个切面

    @After 相当于 <aop:After method="method" pointcut-ref="pointcut"/>

    @Before 相当于 <aop:before method="method" pointcut-ref="pointcut"/>

    @Around 相当于 <aop:around method="method" pointcut-ref="pointcut"/>

    @AfterReturning 相当于 <aop:after-returning method="method" pointcut-ref="pointcut"/>

    @AfterThrowing 相当于 <aop:after-throwing method="method" pointcut-ref="pointcut"/>


    ✨XML与注解


    XML与注解对比

    • XML适用于任何场合!维护简单方便
    • 注解不是自己的类无法使用 维护相对复杂

    使用参考

    • XML用来管理bean;
    • 注解只负责完成属性的注入

    ✨Spring官方文档

    Spring Framework Documentation


    ✨课程链接

    【狂神说Java】Spring5最新完整教程IDEA版通俗易懂_哔哩哔哩_bilibili


    ⭐转载请注明出处

    本文作者:双份浓缩馥芮白

    原文链接:https://www.cnblogs.com/Flat-White/p/15110606.html

    版权所有,如需转载请注明出处。

  • 相关阅读:
    mac登录界面的背景壁纸图片位置
    【转载】MAC系统修改帐号短名和个人文件夹名称
    ios 6.x系统UITextView 设置为密码输入无效的问题
    一个简单的果冻弹动动画
    ios中的自动释放池
    ios 静态库联合调试
    【转】IOS制作静态库
    objective-c中为什么不能实现多重继承及如何变通实现
    回调中释放自己会不会导致崩溃?
    【转载】Objective-C runtime 消息机制
  • 原文地址:https://www.cnblogs.com/Flat-White/p/15110606.html
Copyright © 2020-2023  润新知