Spring - 基于 Java 的容器配置

x33g5p2x  于2022-10-06 转载在 Spring  
字(3.3k)|赞(0)|评价(0)|浏览(309)

在本文中,我们将介绍如何在 Java 代码中使用注解来配置 Spring 容器。

基本概念:@Bean 和 @Configuration

Spring 新的 Java 配置支持中的核心工件是 @Configuration-annotated 类和 @Bean-annotated 方法。

@Bean 注解用于指示方法实例化、配置和初始化要由 Spring IoC 容器管理的新对象。对于熟悉 Spring 的 XML 配置的人来说,@Bean 注解与元素的作用相同。您可以将@Bean 注解的方法与任何 Spring @Component 一起使用。但是,它们最常与@Configuration bean 一起使用。
使用 @Configuration 注解类表明它的主要目的是作为 bean 定义的来源。此外,@Configuration 类允许通过调用同一类中的其他 @Bean 方法来定义 bean 间的依赖关系。最简单的@Configuration 类如下所示:

下面的简单示例显示了 @Bean 和 @Configuration 注解的用法。

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.companyname.projectname.customer.CustomerService;
import com.companyname.projectname.order.OrderService;

@Configuration
public class Application {

    @Bean
    public CustomerService customerService() {
        return new CustomerService();
    }

    @Bean
    public OrderService orderService() {
        return new OrderService();
    }
}

前面的配置完全等价于下面的 Spring XML:

<beans>
        <bean id="customerService" class="com.companyname.projectname.CustomerService"/>
        <bean id="orderService" class="com.companyname.projectname.OrderService"/>
</beans>

注意XML中的方法名和bean名是完全一样的。

使用 AnnotationConfigApplicationContext 实例化 Spring 容器

Spring 3.0 引入了 AnnotationConfigApplicationContext 类,它实现了 ApplicationContext 接口。它不仅能够接受 @Configuration 类作为输入,还能够接受普通的 @Component 类和使用 JSR-330 元数据注解的类。

当@Configuration 类作为输入提供时,@Configuration 类本身被注册为 bean 定义,并且该类中所有声明的 @Bean 方法也被注册为 bean 定义。
当提供 @Component 和 JSR-330 类时,它们被注册为 bean 定义,并且假定在必要时在这些类中使用诸如 @Autowired@Inject 之类的 DI 元数据。

创建 Spring IOC 容器

在实例化 AnnotationConfigApplicationContext 时,我们可以使用 @Configuration 类作为输入。这允许 Spring 容器的完全无 XML 使用,如以下示例所示:

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Application {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        MessageService messageService = context.getBean(MessageService.class);
        messageService.setMessage("TwitterMessageService Implementation");
        System.out.println(messageService.getMessage());

        MessageService messageService1 = context.getBean(MessageService.class);
        System.out.println(messageService1.getMessage());
        context.close();
    }
}

AnnotationConfigApplicationContext 不仅限于使用 @Configuration 类。任何 @Component 或 JSR-330 注解类都可以作为输入提供给构造函数,如以下示例所示:

public static void main(String[] args) {
    ApplicationContext ctx = new AnnotationConfigApplicationContext(MyServiceImpl.class, Dependency1.class, Dependency2.class);
    MyService myService = ctx.getBean(MyService.class);
    myService.doStuff();
}

使用 register(Class<?>... ) 以编程方式构建容器

您可以使用无参数构造函数实例化 AnnotationConfigApplicationContext,然后使用 register() 方法对其进行配置。这种方法在以编程方式构建 AnnotationConfigApplicationContext 时特别​​有用。以下示例显示了如何执行此操作:

public static void main(String[] args) {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(AppConfig.class, OtherConfig.class);
    ctx.register(AdditionalConfig.class);
    ctx.refresh();
    MyService myService = ctx.getBean(MyService.class);
    myService.doStuff();
}

使用 scan(String…) 启用组件扫描

要启用组件扫描,您可以注解您的 @Configuration 类,如下所示:

package net.javaguides.spring.scope;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "net.javaguides.spring")
public class AppConfig {
 
}

使用 @ComponentScan 注解在基于 java 的配置中启用自动组件扫描,注解您的 @Configuration 类,如上面的代码示例所示。

相关文章