Spring Boot 教程

@ConditionalOnProperty 注解

在 Spring Boot 中,@ConditionalOnProperty 注解用于判断是否存在指定的属性,以及判断属性的值是否是我们期待的值。我们可以通过查看它的源码进一步了解:

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(OnPropertyCondition.class)
public @interface ConditionalOnProperty {
   // name 的别名
   String[] value() default {};
   // 指定属性的前缀,例如:acme.system.feature
   String prefix() default "";
   // 指定要验证属性的名称,如果你指定了前缀,则为“前缀.name”
   String[] name() default {};
   // 属性期望值的字符串表示形式。如果未指定,则该属性不得等于false。
   String havingValue() default "";
   // 指定如果未设置该属性,条件是否应该匹配。默认为false。
   boolean matchIfMissing() default false;
}

示例

本实例将在 application.properties 属性文件中定义属性,通过属性值是否为1来控制是否实例化 UserService 和 OrderService。

(1)创建 application.properties 文件,内容如下:

# 0-禁用;1-启动
hxstrive.service.user.enable=1
hxstrive.service.order.enable=0
package com.huangx.springboot.autoconfig.service;

(2)创建 UserService 和 OrderService 服务,代码如下:

a、UserService.java

package com.huangx.springboot.autoconfig.service;

public class UserService {
    // 什么也不做
}

b、OrderService.java

package com.huangx.springboot.autoconfig.service;

public class OrderService {
    // 什么也不做
}

(3)创建 User 和 Order 配置类,代码如下:

a、UserConfig.java

package com.huangx.springboot.autoconfig.config;

import com.huangx.springboot.autoconfig.service.UserService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnProperty(prefix = "hxstrive.service", name = "user.enable",
        havingValue = "1")
public class UserConfig {

    @Bean
    public UserService userService() {
        System.out.println("UserService -> userService()");
        return new UserService();
    }

}

b、OrderConfig.java

package com.huangx.springboot.autoconfig.config;

import com.huangx.springboot.autoconfig.service.OrderService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnProperty(prefix = "hxstrive.service", name = "order.enable",
        havingValue = "1")
public class OrderConfig {

    @Bean
    public OrderService orderService() {
        System.out.println("OrderConfig -> orderService()");
        return new OrderService();
    }

}

(4)客户端,验证 UserService 和 OrderService 实例是否被创建。代码如下:

package com.huangx.springboot.autoconfig;

import com.huangx.springboot.autoconfig.service.OrderService;
import com.huangx.springboot.autoconfig.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class Demo8Application {

    @Autowired
    private ApplicationContext applicationContext;

    public static void main(String[] args) {
        SpringApplication.run(Demo8Application.class, args);
    }

    @GetMapping("/")
    public String index() {
        UserService userService = null;
        try {
            userService = applicationContext.getBean(UserService.class);
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }

        OrderService orderService = null;
        try {
            orderService = applicationContext.getBean(OrderService.class);
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }

        return "userService=" + userService + "<br/>" +
                "orderService=" + orderService;
    }

}

运行上面程序,在浏览器中访问 http://localhost:8080 地址,如下图: 

说说我的看法
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号