• spring注解简单开发


    7.spring注解注解开发

    7.1 环境准备

    • 在spring4之后想要使用注解实现 Spring 自动装配,还需要引入Spring 提供的 spring-aop 的 Jar 包。

    • 使用注解需要导入context约束,增加注解支持,开启注解扫描包

     <?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
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/context/spring-aop.xsd">
         <!--配置注解的支持-->
         <context:annotation-config/>
         <!--新版本配置扫描注解就可以运行,开启组件扫描功能-->
         <!--指定需要扫描的包,这个包下的注解会生效-->
         <context:component-scan base-package="com.zk"></context:component-scan>
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 这里也可以用空格扫描多个包

    • 有了context:component-scan,另一个context:annotation-config/标签可以移除掉,因为已经被包含进去了。

    • 举例:

    • 实体类加@Component注解

    /**
     * @Component相当于xml中的bean
     * @Component组件
      */
    @Component
    @Scope("singleton")
    public class User {
    
    
        private String name;
    
        public String getName() {
            return name;
        }
        @Value("tony")
        public void setName(String name) {
            this.name = name;
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    '}';
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 编写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"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/context/spring-aop.xsd">
    
        <!--指定需要扫描的包,这个包下的注解会生效-->
        <context:component-scan base-package="com.zk"></context:component-scan>
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 调用测试
    @Test
    public void test01(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        User user = context.getBean("user", User.class);
        System.out.println(user.toString());
    }
    //User{name='tony'}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    7.2 spring注解

    注解定义 Bean

    • 使用注解定义 Bean,Spring 提供了以下多个注解,这些注解可以直接标注在 Java 类上,将它们定义成 Spring Bean。
    注解说明
    @Component该注解用于描述 Spring 中的 Bean,它是一个泛化的概念,仅仅表示容器中的一个组件(Bean),并且可以作用在应用的任何层次,例如 Service 层、Dao 层等。 使用时只需将该注解标注在相应类上即可。
    @Repository该注解用于将数据访问层(Dao 层)的类标识为 Spring 中的 Bean,其功能与 @Component 相同。
    @Service该注解通常作用在业务层(Service 层),用于将业务层的类标识为 Spring 中的 Bean,其功能与 @Component 相同。
    @Controller该注解通常作用在控制层(如 Struts2 的 Action、SpringMVC 的 Controller),用于将控制层的类标识为 Spring 中的 Bean,其功能与 @Component 相同。
    • @Component放在类上,说明这个类被spring作为bean管理了
    • 注解方式注册的bena,调用时beanid为类首字母小写字符串,由于这种注册方式时单例模式,在容器中只有一份,所以可以通过@Autowired拿到

    属性注入类

    • @Value(“tony”)作用在基本数据属性或String上为其注入值,相当于bean的 元素中的 value 属性

    • @Value(“tony”)也可以作用在setter方法上

    @Value("tony")
    private String name;
    
    • 1
    • 2
    • 引用类型用@atuowired

    作用域类注解

    @Scope("singleton")作用在类上表示时单例类
    
    • 1

    7.3 小结

    • xml和注解对比

    xml万能,适用于任何场合,后期维护简单方便

    注解:自己的类引用bean和被其他bean引用比较麻烦,维护比较麻烦,若实体类多的时候。

    最开始是为了不让程序员频繁改代码,注解之后又要回去改代码。

    • 配合使用

    xml用来管理bean

    注解负责完成属性注入

    注意使用注解要导入约束并开启注解扫描包,包路径设置对

    本专栏下一篇:SpringAOP

  • 相关阅读:
    音视频从入门到精通——FFmpeg结构体:AVCodecContext分析
    mysql杂记漫谈
    Kafka中遇到的错误:
    Allure-pytest功能特性介绍
    LeetCode337:打家劫舍III
    云计算-私有云-国产-华为-FusionCloud
    逻辑运算与按位运算
    javaee spring 测试aop 切面
    【数组基础知识】
    JWT详细讲解
  • 原文地址:https://blog.csdn.net/weixin_42045639/article/details/125530071