Spring中@Autowired、@Qualifier、@Resourse、@Value注解方式的使用

@Autowired:根据属性类型进行的自动装配

@Qualifier:根据属性名称进行注入

@Resourse:可以根据类型注入,也可以根据名称注入

@Value:注入普通类型属性

一、下面我们先来看@Autowired注解方式的使用

第①步:将service和dao创建对象,在service和dao类添加创建对象的注释

第②步:在service注入对象dao,在service类添加dao类型属性,在属性上面使用注释

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

@Service
public class UserService {
//    不需要添加set方法
    @Autowired
    private UserDao userDao;

    public void add(){
        System.out.println("UserService add...........................");
        userDao.add();
    }
}

在上面的代码中,使用的Autowired注入的方法,根绝UserDao找到它的实现类,如果只有一个实现类的时候可以使用,如果有多个实现类,便会报错,系统不知道要的是哪个实现类 

import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImpl implements UserDao {
    @Override
    public void add() {
        System.out.println("dao add...................");
    }
}
public interface UserDao {
    public abstract  void add();
}

XML文件的内容:   记得配置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"
       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  ">

    <context:component-scan base-package="com.company.Spring5.service"> </context:component-scan>
</beans>

测试类:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    @org.junit.Test
    public void test(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean10Zhujie.xml");
        UserService userService = applicationContext.getBean("userService",UserService.class);

        userService.add();
    }
}

运行结果:

二、

      下面是@Qualifier的使用   

       此注解要和上面的@Autowired一起使用

根据名称进行注入,让系统知道了我们具体要引入哪个实现类,当有多个实现类的时候,我们可以使用此方法

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

@Service
public class UserService {
//    不需要添加set方法
    @Autowired
    @Qualifier(value = "userDaoImpl1")
    private UserDao userDao;

    public void add(){
        System.out.println("UserService add...........................");
        userDao.add();
    }
}

import org.springframework.stereotype.Repository;

@Repository(value = "userDaoImpl1")
public class UserDaoImpl implements UserDao {
    @Override
    public void add() {
        System.out.println("dao add...................");
    }
}

XML配置文件和测试类不改变,测试结果也没有改变

三、

   @Resource  可以根据类型注入,可以根据名称注入

        首先看根据类型注入

               

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

@Service
public class UserService {
//    不需要添加set方法
    @Resource
    private UserDao userDao;

    public void add(){
        System.out.println("UserService add...........................");
        userDao.add();
    }
}

其他的代码块不变

  再来看根据名称注入  ,其他的代码块不变 

   注意,这里注入的时候,属性使用的是name,而不是value,别出错。

   值得注意的是,   @Resource不是Spring包里面的,而是java扩展包里面的

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

@Service
public class UserService {
//    不需要添加set方法
    @Resource(name="userDaoImpl1")
    private UserDao userDao;

    public void add(){
        System.out.println("UserService add...........................");
        userDao.add();
    }
}

最终的输出结果也是下图

四、

@Value:注入普通类型属性

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class UserService {
//    不需要添加set方法
    @Value(value = "abc")
    private String name;
    @Autowired
    @Qualifier(value = "userDaoImpl1")
    private UserDao userDao;

    public void add(){
        System.out.println("UserService add..........................."+name);
        userDao.add();
    }
}

测试结果:

     

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

我爱布朗熊

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

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

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

打赏作者

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

抵扣说明:

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

余额充值