【Spring】12 Bean 的自动装配

本文主要介绍了Spring中使用@Autowired注解自动装配Bean的相关知识。包括该注解可应用的范围,默认情况下属性设置要求,当IOC容器存在多个兼容Bean时的处理方法,还提及它在数组、集合、Map上的应用,以及@Resource和@Inject注解与它的对比。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用 @Autowired 自动装配 Bean

@Autowired 注解自动装配具有兼容类型的单个 Bean属性

构造器, 普通字段(即使是非 public), 一切具有参数的方法都可以应用@Authwired 注解

在这里插入图片描述

TestObject.java(com.test.sping.beans.annotation.TestObject)
package com.test.sping.beans.annotation;

import org.springframework.stereotype.Component;

@Component
public class TestObject {

}

UserRepository.java(com.test.sping.beans.annotation.repository.UserRepository)
package com.test.sping.beans.annotation.repository;

public interface UserRepository {

    void save();
}
UserRepositoryImpl.java(com.test.sping.beans.annotation.repository.UserRepositoryImpl)
package com.test.sping.beans.annotation.repository;

import org.springframework.stereotype.Repository;

@Repository("userRepository")
public class UserRepositoryImpl implements UserRepository {

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

}
UserService.java(com.test.sping.beans.annotation.Service.UserService)
package com.test.sping.beans.annotation.Service;

import com.test.sping.beans.annotation.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

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

UserController.java(com.test.sping.beans.annotation.controller.UserController)
package com.test.sping.beans.annotation.controller;

import com.test.sping.beans.annotation.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    public void execute(){
        System.out.println("UserController execute...");
        userService.add();
    }
}

beans-annotation.xml
<beans>
	<context:component-scan base-package="com.test.sping.beans.annotation">
    </context:component-scan>
</beans>
Main.java(com.test.sping.beans.annotation.Main)
package com.test.sping.beans.annotation;

import com.test.sping.beans.annotation.Service.UserService;
import com.test.sping.beans.annotation.controller.UserController;
import com.test.sping.beans.annotation.repository.UserRepository;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {

        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-annotation.xml");

        UserController userController = (UserController) ctx.getBean("userController");
        System.out.println(userController);
        userController.execute();
    }
}

默认情况下, 所有使用 @Authwired 注解的属性都需要被设置. 当 Spring 找不到匹配的 Bean 装配属性时, 会抛出异常, 若某一属性允许不被设置, 可以设置 @Authwired 注解的 required 属性为 false

UserRepositoryImpl.java(com.test.sping.beans.annotation.repository.UserRepositoryImpl)
package com.test.sping.beans.annotation.repository;

import com.test.sping.beans.annotation.TestObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class UserRepositoryImpl implements UserRepository {

    @Autowired(required = false)
    public TestObject testObject;

    @Override
    public void save() {
        System.out.println("UserRepository Save...");
        System.out.println(testObject);
    }

}

默认情况下, 当 IOC 容器里存在多个类型兼容的 Bean 时, 通过类型的自动装配将无法工作. 此时可以在 @Qualifier 注解里提供 Bean 的名称. Spring 允许对方法的入参标注 @Qualifiter 已指定注入 Bean 的名称

在这里插入图片描述

UserService.java(com.test.sping.beans.annotation.Service.UserService)
package com.test.sping.beans.annotation.Service;

import com.test.sping.beans.annotation.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    private UserRepository userRepository;

    @Autowired
    @Qualifier("userRepositoryImpl")
    public void setUserRepository(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

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

UserJdbcRepository.java(com.test.sping.beans.annotation.repository.UserJdbcRepository)
package com.test.sping.beans.annotation.repository;

import org.springframework.stereotype.Repository;

@Repository
public class UserJdbcRepository implements UserRepository {

    @Override
    public void save() {
        System.out.println("UserJdbcRepository save...");
    }
}
UserRepositoryImpl.java(com.test.sping.beans.annotation.repository.UserRepositoryImpl)
package com.test.sping.beans.annotation.repository;

import com.test.sping.beans.annotation.TestObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class UserRepositoryImpl implements UserRepository {

    @Autowired(required = false)
    public TestObject testObject;

    @Override
    public void save() {
        System.out.println("UserRepository Save...");
        System.out.println(testObject);
    }
}

其他

  • @Authwired 注解也可以应用在数组类型的属性上, 此时 Spring 将会把所有匹配的 Bean 进行自动装配.
  • @Authwired 注解也可以应用在集合属性上, 此时 Spring 读取该集合的类型信息, 然后自动装配所有与之兼容的 Bean.
  • @Authwired 注解用在 java.util.Map 上时, 若该 Map 的键值为 String, 那么 Spring 将自动装配与之 Map 值类型兼容的 Bean, 此时 Bean 的名称作为键值

Spring 还支持 @Resource 和 @Inject 注解,这两个注解和 @Autowired 注解的功用类似

  • @Resource 注解要求提供一个 Bean 名称的属性,若该属性为空,则自动采用标注处的变量或方法名作为 Bean 的名称
  • @Inject 和 @Autowired 注解一样也是按类型匹配注入的 Bean, 但没有 reqired 属性
    建议使用 @Autowired 注解
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值