Spring高级话题——Conditional

@Conditional根据满足某一个特定条件创建一个特定Bean。比方说,当某一个jar包在一个类路径下的时候,自动配置一个或多个Bean,或者只有某个Bean被创建才会创建另外一个Bean。总的来说,就是根据特定条件控制Bean的创建行为,这样我们就可以利用这个特性进行一些自动的配置。

实例代码:(该实例代码是以jar包在哪个环境下运行,调用哪个类)

ConditionConfig.java

package com.minivison.caixing.learn.condition;

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

/**
 * <Description> <br>
 *
 * @author caixing<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2018年04月17日 <br>
 */
@Configuration
public class ConditionConfig {

    @Bean
    @Conditional(WindowsCondition.class)
    public ListService windowListService(){
        return new WindowsService();
    }

    @Bean
    @Conditional(LinuxCondition.class)
    public ListService linuxListService(){
        return new LinuxService();
    }


}

LinuxCondition.java

package com.minivison.caixing.learn.condition;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * <Description> <br>
 *
 * @author caixing<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2018年04月17日 <br>
 */
public class LinuxCondition implements Condition {

    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        return conditionContext.getEnvironment().getProperty("os.name").contains("Linux");
    }
}

LinuxService.java

package com.minivison.caixing.learn.condition;

/**
 * <Description> <br>
 *
 * @author caixing<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2018年04月17日 <br>
 */
public class LinuxService implements ListService {
    @Override
    public String showListCmd() {
        return "ls";
    }
}

ListService.java

package com.minivison.caixing.learn.condition;

/**
 * <Description> <br>
 *
 * @author caixing<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2018年04月17日 <br>
 */
public interface ListService {
    String showListCmd();
}

WindowsCondition.java

package com.minivison.caixing.learn.condition;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * <Description> <br>
 *
 * @author caixing<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2018年04月17日 <br>
 */
public class WindowsCondition implements Condition {
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        return conditionContext.getEnvironment().getProperty("os.name").contains("Windows");
    }
}

WindowsService.java

package com.minivison.caixing.learn.condition;

/**
 * <Description> <br>
 *
 * @author caixing<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2018年04月17日 <br>
 */
public class WindowsService implements ListService {
    @Override
    public String showListCmd() {
        return "dir";
    }
}

Main.java

package com.minivison.caixing.learn;

import com.minivison.caixing.learn.condition.ConditionConfig;
import com.minivison.caixing.learn.condition.ListService;
import com.minivison.caixing.learn.scheduled.TaskSchedulerConfig;
import com.minivison.caixing.learn.scheduled.TaskService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import javax.imageio.stream.FileImageInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * <Description> <br>
 *
 * @author caixing<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2018年04月12日 <br>
 */
public class Main {
    public static void main(String[] args) throws Exception {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConditionConfig.class);
        ListService service = context.getBean(ListService.class);
        System.out.println(service.showListCmd());
        //context.close();
        //String str = Base64Utils.encodeToString(image2byte("E:\\123.jpg"));
        //System.out.println(str);

    }

    public static byte[] image2byte(String path) {
        byte[] data = null;
        FileImageInputStream input = null;
        try {
            input = new FileImageInputStream(new File(path));
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int numBytesRead = 0;
            while ((numBytesRead = input.read(buf)) != -1) {
                output.write(buf, 0, numBytesRead);
            }
            data = output.toByteArray();
            output.close();
            input.close();
        } catch (FileNotFoundException ex1) {
            ex1.printStackTrace();
        } catch (IOException ex1) {
            ex1.printStackTrace();
        }
        return data;
    }
}

阅读更多
文章标签: Spring的条件判断
个人分类: java spring
上一篇spring高级话题——定时任务
下一篇java远程调试
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭