Spring注解驱动的DI

注解驱动的依赖注入(DI)是Spring框架中一种简洁且强大的依赖注入方式,通过使用注解来标识和管理Bean及其依赖关系。注解驱动的DI使得配置更加简洁,代码更加清晰。

常用注解

  1. @Component:标识一个类为Spring管理的组件。
  2. @Service:标识一个类为服务层组件,功能上与@Component相同,但语义上更明确。
  3. @Repository:标识一个类为数据访问层组件,功能上与@Component相同,但语义上更明确。
  4. @Controller:标识一个类为Spring MVC控制器,功能上与@Component相同,但语义上更明确。
  5. @Autowired:用于自动注入依赖对象,可以标注在构造器、字段或Setter方法上。
  6. @Qualifier:与@Autowired一起使用,用于指定注入的Bean的名称。
  7. @Value:用于注入属性值。
  8. @Configuration:标识一个类为Spring配置类。
  9. @ComponentScan:用于指定Spring扫描的包,以发现和注册Bean。
示例代码

以下是一个使用注解驱动的DI的示例:

Java代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

// 定义一个组件
@Component
public class MyBean {
    public void doSomething() {
        System.out.println("Doing something...");
    }
}

// 定义一个服务层组件
@Service
public class MyService {
    private final MyBean myBean;

    @Autowired
    public MyService(MyBean myBean) {
        this.myBean = myBean;
    }

    public void performAction() {
        myBean.doSomething();
    }
}

// 定义一个配置类
@Configuration
@ComponentScan(basePackages = "com.example")
public class AppConfig {
    @Bean
    public MyBean myBean() {
        return new MyBean();
    }
}

// 主类
public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        MyService myService = context.getBean(MyService.class);
        myService.performAction();
    }
}

在这个示例中:

  1. MyBean类使用@Component注解标识为Spring管理的组件。
  2. MyService类使用@Service注解标识为服务层组件,并通过构造器注入MyBean,使用@Autowired注解标识构造器。
  3. AppConfig类使用@Configuration注解标识为Spring配置类,并使用@ComponentScan注解指定要扫描的包。
  4. Main类中,通过AnnotationConfigApplicationContext加载配置类AppConfig,并获取MyService的Bean实例,调用其方法。

使用@Qualifier注解

当有多个相同类型的Bean时,可以使用@Qualifier注解指定要注入的Bean的名称。

示例代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Component("bean1")
public class MyBean1 {
    public void doSomething() {
        System.out.println("Doing something in MyBean1...");
    }
}

@Component("bean2")
public class MyBean2 {
    public void doSomething() {
        System.out.println("Doing something in MyBean2...");
    }
}

@Service
public class MyService {
    private final MyBean1 myBean1;
    private final MyBean2 myBean2;

    @Autowired
    public MyService(@Qualifier("bean1") MyBean1 myBean1, @Qualifier("bean2") MyBean2 myBean2) {
        this.myBean1 = myBean1;
        this.myBean2 = myBean2;
    }

    public void performAction() {
        myBean1.doSomething();
        myBean2.doSomething();
    }
}

@Configuration
@ComponentScan(basePackages = "com.example")
public class AppConfig {
}

public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        MyService myService = context.getBean(MyService.class);
        myService.performAction();
    }
}

在这个示例中:

  1. MyBean1MyBean2类分别使用@Component注解标识为Spring管理的组件,并指定了名称bean1bean2
  2. MyService类通过构造器注入MyBean1MyBean2,并使用@Qualifier注解指定要注入的Bean的名称。

总结

注解驱动的依赖注入是Spring框架中一种简洁且强大的依赖注入方式,通过使用注解来标识和管理Bean及其依赖关系。常用的注解包括@Component@Service@Repository@Controller@Autowired@Qualifier@Value@Configuration@ComponentScan。注解驱动的DI使得配置更加简洁,代码更加清晰,是现代Spring应用程序开发中常用的方式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

扬子鳄008

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

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

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

打赏作者

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

抵扣说明:

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

余额充值