[Spring]@Autowired,@Required,@Qualifier注解

news/2024/7/24 8:40:35 标签: java

@Required注解

@Required注解用于setter方法,表明这个属性是必要的,不可少的,必须注入值

假设有个测试类,里面有name和password两个属性

我给两个属性的setter方法都加了@Required注解

package com.example.demo1.Implements;

import com.example.demo1.Interface.UserService;
import org.springframework.beans.factory.annotation.Required;

public class UserClientImpl implements UserService {

    private String name;
    private String password;

    public UserClientImpl(){}

    public UserClientImpl(String name,String password){
        this.name = name;
        this.password = password;
    }

    public String getName() {
        return name;
    }

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

    public String getPassword() {
        return password;
    }

    @Required
    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public void save() {
        System.out.println("客户端保存信息"+name+"--"+password);
    }
}

现在我只给一个属性加注入,另一个不加

可以看到报错

然后我补上注入之后就没问题了

@Autowoired注解

 其实看名字就可以看出来,这个是跟自动装填有关

使用它需要加一行代码

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcess></bean>

1,在属性前加此注解

先给定两个类

package com.example.demo1.Other;

public class CustomerTest {
    public CustomerTest(){
        System.out.println("在Customer.构造方法中...");
    }
    public void show(){
        System.out.println("在Customer.show方法中...");
    }
}
package com.example.demo1.Implements;

import com.example.demo1.Interface.Customer;
import com.example.demo1.Other.CustomerTest;
import org.springframework.beans.factory.annotation.Autowired;

public class CustomerImpl implements Customer {


    private String name;
    private String id;
    @Autowired
    private CustomerTest customerTest;

    public CustomerTest getCustomerTest() {
        return customerTest;
    }

    public void setCustomerTest(CustomerTest customerTest) {
        this.customerTest = customerTest;
    }

    public String getName() {
        return name;
    }

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

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }

    @Override
    public void show() {
        System.out.println(id+"..."+name);
        customerTest.show();
    }
}

第二个类在第三个成员变量前面加个此注解

然后applicationContext这样写

 <bean id="Customer"
          class="com.example.demo1.Implements.CustomerImpl">
    </bean>
    <bean id="CustomerTest" class="com.example.demo1.Other.CustomerTest"></bean>

在打印一下结果

ApplicationContext instance = new ClassPathXmlApplicationContext("applicationContext.xml");
        CustomerImpl customer = (CustomerImpl) instance.getBean("Customer");
        customer.show();
        ((ClassPathXmlApplicationContext) instance).registerShutdownHook();

可以看到Customer对象是自动装填了的

2,在构造函数之前加此注解

效果和上面是一样的,不演示了

3,@Autowired(required=false)的作用

这里跟之前的@Required的作用类似

默认情况下,@Autowired 注释意味着依赖是必须的,它类似于 @Required 注释,然而,你可以使用 @Autowired 的 (required=false) 选项关闭默认行为。

这里跟@Required的效果类似,不演示了

 

@Qualifier注解

在之前的学习注解的过程中,显然,用自动装配的时候,如果配置的bean是相同的类的生成的对象,会报错,于是为了解决这个问题,@Qualifier就出来了

@Qualifier和@Autowired一起使用,在@Qualifier后面的括号里欲装配的bean的名称,就可以让Spring自动给我们装配合适的bean

首先applicationContext.xml ,因为要用到context标签,需要用到aop.jar,所以在这之前记得引入Springframework-aop.jar,这个标签就像之前的注解一样,需要注册那几个Processor才能起作用

<?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
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config/>
    <bean id="Profile" class="com.example.demo1.Testclass.Profile"></bean>

    <bean id="student1" class="com.example.demo1.Testclass.Student">
        <property name="name" value="student1"></property>
        <property name="age" value="2"></property>
    </bean>
    <bean id="student2" class="com.example.demo1.Testclass.Student">
        <property name="age" value="3"></property>
        <property name="name" value="student2"></property>
    </bean>
</beans>

注解是放在要装配放属性的上方

package com.example.demo1.Testclass;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class Profile {
    @Autowired
    @Qualifier(value = "student1")
    private Student student;
    public Profile(){
        System.out.println("Inside Profile constructor." );
    }
    public void setStudent(Student student) {
        this.student = student;
    }

    public void printAge() {
        System.out.println("Age : " + student.getAge() );
    }
    public void printName() {
        System.out.println("Name : " + student.getName() );
    }
}

 

package com.example.demo1.Testclass;

public class Student {
    private Integer age;
    private String name;
    public void setAge(Integer age) {
        this.age = age;
    }
    public Integer getAge() {
        return age;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
}

之后Spring就会识别出合适的bean并注入了

测试代码

ApplicationContext instance = new ClassPathXmlApplicationContext("applicationContext.xml");
        Profile profile = (Profile) instance.getBean("Profile");
        profile.printName();
        ((ClassPathXmlApplicationContext) instance).registerShutdownHook();

结果也正是这样

 

转载于:https://www.cnblogs.com/Yintianhao/p/9721525.html


http://www.niftyadmin.cn/n/1440472.html

相关文章

python去掉字符串中空格的方法

1.strip()&#xff1a;把头和尾的空格去掉 2.lstrip()&#xff1a;把左边的空格去掉 3.rstrip()&#xff1a;把右边的空格去掉 4.replace(c1,c2)&#xff1a;把字符串里的c1替换成c2。故可以用replace( ,)来去掉字符串里的所有空格 5.split()&#xff1a;通过指定分隔符对字符串…

[Vue] 14.Vue中的组件:创建子组件以及在父组件中用子组件

一、父组件 创建父组件的实例如下&#xff1a; <script>const app Vue.createApp({template: <div>hello world</div>});const vm app.mount(#root)</script>定义两个全局子组件‘hello’和’world’ <script>const app Vue.createApp({te…

中英文词频统计

#英文小说 词频统计fo open(gc.txt, r, encodingutf-8)#提取字符串gc fo.read().lower()fo.close()print(gc)运行结果&#xff1a; strgc str.lower(gc) #换小写 sep .,;;!?~~-_... #替换字符 for ch in sep: gc gc.replace(ch, )print(gc)运行结果&#xff1a; str…

流媒体激活宽带3G产业链 将改写传媒版图(转)

3G、宽带、数字电视&#xff0c;这些在中国正炙手可热的词汇背后的一条条产业链中&#xff0c;正在加入一支新军-流媒体。11月17日&#xff0c;RealNetworks中国流媒体论坛在中国也是在亚洲首次召开&#xff0c;RealNetworks公司创始人、董事长兼首席执行官Rob Glaser亲自来华&…

weui 腾讯官方样式库

2019独角兽企业重金招聘Python工程师标准>>> https://github.com/Tencent/weui 小程序和公众号开发可以使用。 转载于:https://my.oschina.net/swingcoder/blog/2253460

[Vue] 15.Vue中的组件:组件传值及传值校验

一、父组件传值给子组件 父组件通过属性将值传给子组件&#xff0c;子组件通过props接收传过来的值&#xff1a; <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta http-equiv"X-UA-Compatible"…

RESTful api 功能测试

0 为什么要写测试代码 代码写好了&#xff0c;如果能点或者能看&#xff0c;开发人员一般会自己点点或看看&#xff0c;如果没有发现问题就提交测试&#xff1b;更进一步&#xff0c;代码写好后&#xff0c;运行测试代码&#xff0c;通过后提交测试。将流程抽象下&#xff1a; …

动态网站设计十八般武艺——ASP篇(一)(转)

看了《如何令你的网站“动感十足”》一文后&#xff0c;是否令你怦然心动&#xff1f;是否已经急不可待地想构建属于你自己的动态网站&#xff1f;本文将以Active Server Pages为中心&#xff0c;向你全面展示制作动态商业网站的步骤和技巧并通过大量的实例&#xff0c;让你在不…