Springboot注解实现自动装配

38 篇文章 0 订阅

Springboot注解实现自动装配

jdk1.5支持的注解,Spring2.5就支持注解了。

The introduction of annotation-based configuration raised the question of whether this approach is "better"than XML.

要使用注解须知:

  • 1.导入约束:context约束

  • 2.配置注解的支持:context:annotation-config/

    <?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:annotation-config></context:annotation-config>
        <bean id="cat" class="com.example.springannotation.dao.Cat"/>
        <bean id="cat1" class="com.example.springannotation.dao.Cat"/>
        <bean id="people" class="com.example.springannotation.dao.People"/>
    </beans>
    
    @Autowired	//**如果显示定义了Autowired的required属性为false,说明这个对象可以为null,否则不允许为空**
    @Nullable // 字段标记了这个注解,说明这个字段可以为null;
    //直接在属性上使用即可!也可以在set方式上使用!
    //使用Autowired 我们可以不用编写Set方法了,前提是你这个自动装配的属性在IOC (Spring)容器中存在,且符合名字byname!
    

    如果显示定义了Autowired的required属性为false,说明这个对象可以为null,f否则不允许为空

    如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解【@Autowired】完成的时候,我们可以使用@Qualifier(value=“xxx”)去配置@Autowired的使用,指定一个唯一的bean对象注入!

    @Resource(name="cat") //效果与Autowired几乎一致
    

小结:

@Resource和@ Autowired的区别:

  • 都是用来自动装配的,都可以放在属性字段上

  • Autowired通过byType的方式实现,而且必须要求这个对象存在!【常用】

  • @Resource默认通过byname的方式实现,如果找不到名字,则通过byType实现!

    如果两个都找不到的情况下,就报错!【常用】

  • 执行顺序不同:@ Autowired通过byType的方式实现。@Resource默认通过byname的方式实现。

测试代码:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
// 实体类
@Component
public class People {
    @Autowired(required = false)
    private int id;
    
    @Autowired
    @Qualifier(value="cat1")
    private Cat cat;

    @Resource(name="cat")
    private Cat cat2;

    @Autowired(required = false)
    private String name ="ming";
    @Autowired(required = false)
    private int age = 2;

    public People() {
    }

    public Cat getCat2() {
        return cat2;
    }

    public void setName(@Nullable String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public Cat getCat() {
        return cat;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "People{" +
                "id=" + id +
                ", cat=" + cat +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

//测试类
package com.example.springannotation.service;

import com.example.springannotation.dao.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestMain {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        People people = (People) context.getBean("people");
        people.getCat().Shout();
        people.getCat2().Shout();
        System.out.println(people.toString());
        people.setName(null);
        System.out.println(people.toString());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

脑袋不够用的小渣渣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值