• Spring注解解析 | P/C命名空间


    在这里插入图片描述

    💗wei_shuo的个人主页

    💫wei_shuo的学习社区

    🌐Hello World !


    注解实现自动装配

    @Autowire注解

    @Autowire注解,自动装配通过类型,名字如果Autowire不能唯一自动装配上属性,

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

    配置:

    • 导入约束
    
    <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 
           http://www.springframework.org/schema/beans/spring-beans.xsd
           
           http://www.springframework.org/schema/context  
           http://www.springframework.org/schema/context/spring-context.xsd
            ">
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 开启注解支持
    <context:annotation-config/>
    
    • 1

    实现:

    Dog/Cat类和方法的实现还是不变

    • People类
      • 实现@Autowire注解、可以在属性上使用、也可以在set方式使用
      • 编写@Autowire注解后,set方法可以省略不写
    public class People {
        @Autowired
        private Cat cat;
        @Autowired
        private Dog dog;
        private String name;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • beans.xml
        <bean id="cat" class="com.wei.pojo.Cat"/>
        <bean id="dog" class="com.wei.pojo.Dog"/>
        <bean id="people" class="com.wei.pojo.People"/>
    
    • 1
    • 2
    • 3
    • @Nullable 字段标记了这个注解,说明这个字段可以为null

    可以在带参构造中使用@Nullable

        public People(@Nullable String name) {
            this.name = name;
        }
    
    • 1
    • 2
    • 3

    @Qualifier注解

    指定一个唯一的bean对象注入

    • People类
    public class People {
    
        @Autowired
        @Qualifier(value = "cat111")
        private Cat cat;
        
        @Autowired
        @Qualifier(value = "dog222")
        private Dog dog;
        
        private String name;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • beans.xml
        <bean id="cat" class="com.wei.pojo.Cat"/>
        <bean id="cat111" class="com.wei.pojo.Cat"/>
    
        <bean id="dog" class="com.wei.pojo.Dog"/>
        <bean id="dog222" class="com.wei.pojo.Dog"/>
    
        <bean id="people" class="com.wei.pojo.People"/>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    @Resource注解

    默认按照名字装配Bean,即会按照name属性的值来找到具有相同id的Bean Definition 并注入

    • People类
    public class People {
    
        @Resource(name = "cat2")
        private Cat cat;
    
        @Resource
        private Dog dog;
    
        private String name;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • beans.xml
    <bean id="cat1" class="com.wei.pojo.Cat"/>
    <bean id="cat2" class="com.wei.pojo.Cat"/>
    
    <bean id="dog" class="com.wei.pojo.Dog"/>
    
    <bean id="people" class="com.wei.pojo.People"/>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    总结:

    • @Autowire 通过byType方式实现,而且必须要求对象存在,不能为null
    • @Resource 默认通过byname的方式实现,如果找不到名字,则通过byType实现,都不行则报错

    @Component

    放在类上,说明这个类被Spring管理了,等同于bean配置

    • Java类

    此处使用注解@Component @Value 则 beans.xml则无需书写bean

    //等价于@Component
    public class User {
        @Value("wei_shuo")  //相当于 
        public String name;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    @Component的衍生注解,在web开发中,按照MVC三层架构分层

    四个注解功能、作用相同,都是代表将某个类注册到Spring中、装配Bean

    • pojo —> @Component

    • dao —> @Resource

    • service —> @Service

    • controller —> @Controller

    @Scope

    作用域注解,限定Spring Bean的作用范围,在Spring配置文件定义Bean时,通过声明scope配置项,可以灵活定义Bean的作用范围

    @ComponentScan

    @ComponentScan(“com.wei.pojo”) 扫描包

    @Bean

    • 注册一个bean,就相当于bean标签
    • 方法的名字,相当于bean标签的id属性
    • 方法的返回值,相当于bean标签的class属性
        @Bean
        public User getUser(){
            return new User();      //就是返回要注入到bean的对象
        }
    
    • 1
    • 2
    • 3
    • 4

    @Configuration

    • @Configuration注解会被Spring容器托管,因为它本身就是一个@Component
    • @Configuration 代表这是一个配置类,相当于beans.xml
    @Configuration
    @ComponentScan("com.wei.pojo")      
    @Import(WeiConfig2.class)           
    public class WeiConfig {
        @Bean
        public User getUser(){
            return new User();      
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    @Value

    属性注入值

    • Java类

    此处使用@Scope注解使用prototype多实例注解模式

    //等价于@Component
    @Scope("prototype")		//作用域注解
    public class User {
        @Value("wei_shuo")  //相当于 
        public String name;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    P命名空间注入

    p命名空间注入,对应Set注入方式

    • 需要创建set/get方法

    p命名空间注入,可以直接注入属性的值:property

    • User类
    package com.wei.pojo;
    
    public class User {
        private String name;
        private int age;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    
    • 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
    • 27
    • 28
    • 29
    • 30
    • userbeans.xml文件
    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:p="http://www.springframework.org/schema/p"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    	
    
        
        <bean id="user" class="com.wei.pojo.User" p:name="秦疆" p:age="18"/>
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • Test测试
            ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
    //        指定类型User.class则不需要强制转换
            User user = context.getBean("user", User.class);
    //        强制转换
    //        User user = (User) context.getBean("user");
    
            System.out.println(user);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    C命名空间注入

    c命名空间注入,对应构造器注入

    • 需要创建无参/带参构造方法

    c命名空间注入,通过构造器 注入:construct-args

    • User类

    此处增加无参/带参构造方法

    public class User {
        private String name;
        private int age;
    
        public User() {
        }
    
    
        public User(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    
    • 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
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • userbeans.xml文件
    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:c="http://www.springframework.org/schema/c"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    	
    
        
        <bean id="user2" class="com.wei.pojo.User" c:name="wei_shuo" c:age="18"/>
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • Test测试
            ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
    
            User user = context.getBean("user2", User.class);
    
            System.out.println(user);
    
    • 1
    • 2
    • 3
    • 4
    • 5

    总结:

    p命名空间注入/c命名空间注入 都需要导入xml约束,不能直接使用

    • xmlns:p=“http://www.springframework.org/schema/p”

    • xmlns:c=“http://www.springframework.org/schema/c”

    Spring开发包名解释

    • dao包

    数据库操作,crud 即增删改查,对于数据库的增删改查的操作都在这里

    • pojo包

    简单java对象

    • service包

    service 服务器层,也叫业务逻辑层,调用dao中的方法

    Java方式配置

    使用Java的方式配置Spring,也就是不写bean.xml配置,使用注解代替

    • 实体列
    //@Configurable放在类上,说明这个类被Spring管理了,等同于bean配置,注册到容器中
    @Configurable
    public class User {
        private String name;
    
        public String getName() {
            return name;
        }
    
        @Value("CSDN")  //属性注入值
        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
    • 配置文件

      • @Configuration注解会被Spring容器托管,因为它本身就是一个@Component
      • @Configuration 代表这是一个配置类,相当于beans.xml
      • @Import注解,功能就是和Spring XML里面的一样. @Import注解是用来导入配置类或者一些需要前置加载的类.通俗的将就是将类放入到IOC容器中
      • @Bean注解,注册一个bean,就相当于bean标签,方法的名字,相当于bean标签的id属性,方法的返回值,相当于bean标签的class属性
    //@Configuration注解会被Spring容器托管,因为它本身就是一个@Component
    //@Configuration 代表这是一个配置类,相当于beans.xml
    
    @Configuration
    @ComponentScan("com.wei.pojo")      //扫描包
    @Import(WeiConfig2.class)           //@Import注解,功能就是和Spring XML里面的一样. @Import注解是用来导入配置类或者一些需要前置加载的类.通俗的将就是将类放入到IOC容器中
    public class WeiConfig {
    
    
        //注册一个bean,就相当于bean标签
        //方法的名字,相当于bean标签的id属性
        //方法的返回值,相当于bean标签的class属性
        @Bean
        public User getUser(){
            return new User();      //就是返回要注入到bean的对象
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 测试类
    public class MyTest {
    
        //如果完全使用了配置类方式去做,只能通过AnnotationConfig 上下文来获取容器,通过配置类的class对象加载
        public static void main(String[] args) {
            ApplicationContext context = new AnnotationConfigApplicationContext(WeiConfig.class);
            User getUser = (User) context.getBean("getUser");   //此处取方法名
            System.out.println(getUser.getName());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    🌼 结语:创作不易,如果觉得博主的文章赏心悦目,还请——点赞👍收藏⭐️评论📝冲冲冲🤞


    在这里插入图片描述

  • 相关阅读:
    【JavaScript从入门到入神】代码风格|注释|代码质量
    一小时上手MindSpore
    使用java语言编写一个简易的计算器(完整代码与详细步骤都有哦!)
    使用正则表达式模块“re”遇到的错误
    字符串总结
    客快物流大数据项目(七十六):使用Impala对kudu进行DML操作
    数据结构中的七大排序(Java实现)
    vue导出word文档
    springboot自动缓存
    22【解释器设计模式】
  • 原文地址:https://blog.csdn.net/weixin_62765017/article/details/127456652