@Autowired注解使用说明

@Autowired & @Qualifier

@Autowired 主要用来自动装配,在容器中找到我们需要的Bean

@Qualifer 则是当一个接口有多个实现类的时候,可以作为条件更精确的找到我们需要的Bean

js 复制代码
public interface PaymentService{
    void processPayment();
}


@Component("creditCardPaymentService")
public class CreditCardPaymentService implements PaymentService{
    @Override
    public void processPayment(){
        System.out.println("Card")
    }
}

public class PaypalPaymentService implements PyamentService{
    @Override
    public void processPayment(){
        System.out.println("Paypal")
    }
}

#使用,如果前面不区分,这里会报错,不知道使用哪一个
@Component
public class Shpping{

    @Autowired
    @Qualifer("creditCardPaymentService")
    private PaymentService paymentService
}

@Autowired的高级使用

当@Autowired注解一个方法,并且该方法的参数是一个接口的时候,Spring会自动收集这个接口的所有实现类

1.Spring的依赖注入,容器会扫描所有已注册的Bean,寻找和目标匹配的实例

2.当Spring遇到Collection,List, Set或者数组类型的依赖时,会自动收集所有匹配该泛型类型的Bean

3.当@Autowired用在方法上,Spring会在创建Bean后调用改方法,自动传入所有匹配的参数

js 复制代码
public interface PaymentProcessor{
    void process()
}

@Component
public class CreditCardProcessor implements PaymentProcessor{
    @Override
    pubic void process(){
    }
}

@Component
public class PaypalProcessor implements PaymentProcessor{
    @Override
    public void processor(){
    }
}

#使用@Autowired方法注入
@Service
public class PaymentService{

    private List<PaymentProcessor> processors;
    
    public void setProcessors(List<PaymentProcessor> processors){
        this.processors = processors;
    }
    
    //xxxxxxxxx

}

@Conditional

@Conditional是Spring Framework中实现条件化配置的核心方式。它允许根据特定的条件来决定是否注册一个Bean.

自定义Conditional

js 复制代码
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.core.env.Environment;


public class MyCustomCondition implements Condition{
    @Override
    public boolean matches(ConditionContext context,AnnotatedTypeMetadata metadata){
        //从环境变量获取
        Environment env = context.getEnvironment();
        return "true".equals(env.getProperty("myapp.feature.enabled"))
    }
}

可以用在@Bean方法上,@Component类上或者@Configuration类上

js 复制代码
@Configuration
public class AppConfig{
    @Bean
    @Conditional(MyCustomCondition.class)
    public MyService(){
        return new MyService();
    }
}

SpringBoot 衍生注解

1.@ConditionalOnProperty 根据配置决定是否生效

2.@ConditionalOnBean 根据容器中是否存在Bean而生效

3.@ConditionalOnWebApplication 当前应用是否事Web应用而生效

js 复制代码
@Service
@ConditionalOnProperty(
    value = "myapp.feature.enabled",      //属性
    havingValue = "true",                 //属性值
    matchIfMissing = false                //如果属性缺失,是否匹配,false表示缺失不匹配
)
public class MyServiceImpl implements MyService{
    //  只有当myapp.feature.enable=true
}
相关推荐
涡能增压发动积1 天前
同样的代码循环 10次正常 循环 100次就抛异常?自定义 Comparator 的 bug 让我丢尽颜面
后端
Wenweno0o1 天前
0基础Go语言Eino框架智能体实战-chatModel
开发语言·后端·golang
swg3213211 天前
Spring Boot 3.X Oauth2 认证服务与资源服务
java·spring boot·后端
tyung1 天前
一个 main.go 搞定协作白板:你画一笔,全世界都看见
后端·go
gelald1 天前
SpringBoot - 自动配置原理
java·spring boot·后端
殷紫川1 天前
深入拆解 Java 内存模型:从原子性、可见性到有序性,彻底搞懂 happen-before 规则
java·后端
元宝骑士1 天前
FIND_IN_SET使用指南:场景、优缺点与MySQL优化策略
后端·mysql
用户31952370347711 天前
记一次 PostgreSQL WAL 日志撑爆磁盘的排查
后端
nghxni1 天前
LightESB PlatformHttp v3.0.0:JSONPath 订单转换 HTTP 路由实战
后端
武子康1 天前
大数据-263 实时数仓-Canal 增量订阅与消费原理:MySQL Binlog 数据同步实践
大数据·hadoop·后端