@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
}
相关推荐
salipopl10 小时前
Spring Boot 整合 Druid 并开启监控
java·spring boot·后端
GISer_Jing10 小时前
AI原生前端工程化进阶实践:从流式交互架构到端云协同全链路落地
前端·人工智能·后端·学习
geNE GENT10 小时前
Spring Boot 实战篇(四):实现用户登录与注册功能
java·spring boot·后端
9523617 小时前
MyBatis
后端·spring·mybatis
uzong20 小时前
9 种 RAG 架构,每位 AI 开发者必学:完整实战指南
后端
小江的记录本20 小时前
【Kafka核心】架构模型:Producer、Broker、Consumer、Consumer Group、Topic、Partition、Replica
java·数据库·分布式·后端·搜索引擎·架构·kafka
止语Lab20 小时前
从手动到框架:Go DI 演进的三个拐点
开发语言·后端·golang
Daybreak1 天前
Elasticsearch 里的索引和 Mapping,到底是什么关系?
后端
Lee川1 天前
Prisma 实战指南:像搭积木一样设计古诗词数据库
前端·数据库·后端