Spring 注解之@Autowired和@Resource

@Autowired和@Resource都是通过注解的方式自动装配对象

@Autowired是按类型装配

 

package spring.annotations.repository;

public interface UserRepository {
    
    public void save();

}

 

package spring.annotations.repository;

import org.springframework.stereotype.Repository;

@Repository  //让spring容器扫描配置文件并自动实例化
public class UserRepositoryImpl implements UserRepository {

    @Override
    public void save() {
         System.out.println("save user");
    }

}
 

 

package spring.annotations.service;

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

import spring.annotations.repository.UserRepository;

@Service
public class UserService {
    @Autowired //按类型注入实例化对象
    UserRepository userRepository;
    public void add(){
        System.out.println("UserService add");
        userRepository.save();
    }

}
 

1、当UserRepositoryImpl没有添加自动扫描注解 NoSuchBeanDefinitionException,bean对象未被定义

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [spring.annotations.repository.UserRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

 

2、可以添加(required=false)解决启动报错的问题

 

3、当再建一个UserJdbcRepositoryImpl 实现UserRepository

package spring.annotations.repository;

import org.springframework.stereotype.Repository;

@Repository
public class UserJdbcRepositoryImpl implements UserRepository {

    @Override
    public void save() {
         System.out.println("save user");
    }

}
 

启动时候会报不止一个bean实例

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [spring.annotations.repository.UserRepository] is defined: expected single matching bean but found 2: userJdbcRepositoryImpl,userRepositoryImpl

解决方法:

a、在userRepositoryImpl类中添加指定bean的名称

package spring.annotations.repository;

import org.springframework.stereotype.Repository;

@Repository("userRepository")  //指定bean名称
public class UserJdbcRepositoryImpl implements UserRepository {

    @Override
    public void save() {
         System.out.println("jdbc save user");
    }

}

b、添加 @Qualifier注解,指定装配具体的bean

package spring.annotations.service;

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

import spring.annotations.repository.UserRepository;

@Service
public class UserService {
    @Autowired(required=false)
     @Qualifier("userRepositoryImpl")
    UserRepository userRepository;
    public void add(){
        System.out.println("UserService add");
        userRepository.save();
    }

}

 

c、使用@Resource注解,指定装配bean的名称

package spring.annotations.service;

import javax.annotation.Resource;

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

import spring.annotations.repository.UserRepository;

@Service
public class UserService {

    @Resource(name="userJdbcRepositoryImpl")
    UserRepository userRepository;
    public void add(){
        System.out.println("UserService add");
        userRepository.save();
    }

}

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值