11、代码与配置分离

1、  基于反射的工厂设计模式

在前面的设计模式中,我们展示了简单的工厂模式,但是它有个弊端,就是如果我们要增加一个子类的话,我们必须修改我们的对象工厂(增加新类的判断)

而通过反射模式,我们可以解决这个问题,代码如下

interface Fruit{

    publicvoid eat();

}

class Apple implements Fruit{

    publicvoid eat(){ System.out.println("we eat apple");}

}

class Orange implements Fruit{

    publicvoid eat(){ System.out.println("we eat Orange");}

}

class Factory{

    publicstatic Fruit getInstance(String className){

        Fruit f =null;

        //此处使用了反射方法生成类的实例对象

        //所以不管真假多少个Fruit的实现类,都不需要修改Factory

        try{

            Class<?> c = Class.forName(className);

            f =(Fruit)c.newInstance();

        }

        catch(Exception ex){

            ex.printStackTrace();

        }

        return f;

    }

}

 

publicclass hello{

    publicstaticvoid main(String args[])throws Exception{

       

        Fruit f1 = Factory.getInstance("Apple");

        f1.eat();

        Fruit f2 = Factory.getInstance("Orange");

        f2.eat();

    }

}

 

 

2、  结合属性文件的工厂模式

以上工厂使用反射获取接口的实例对象,但在操作时需要传入完整的包类名称,而且用户无法知道工厂有多少个类可以使用

这时我们可以结合属性配置文件来配置需要使用的类信息,程序与配置的分离是java开发中一个非常重要的模式,如下

import java.util.Properties;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

 

interface Fruit{

    publicvoid eat();

}

class Apple implements Fruit{

    publicvoid eat(){ System.out.println("we eat apple");}

}

class Orange implements Fruit{

    publicvoid eat(){ System.out.println("we eat Orange");}

}

class Factory{

    publicstatic Fruit getInstance(String className){

        Fruit f =null;

        try{

            Class<?> c = Class.forName(className);

            f =(Fruit)c.newInstance();

        }

        catch(Exception ex){

            ex.printStackTrace();

        }

        return f;

    }

}

//从文件获取属性配置信息

class Init{

    publicstatic Properties getProperties(){

        Properties ps =new Properties();

        //配置文件

        File f =new File("C:"+ File.separator +"AAA\\Fruit.properties");

       

        try{

            if(f.exists()){

                //加载配置文件内容到属性对象

                ps.load(new FileInputStream(f));

            }else{

                ps.setProperty("App","Apple");

                ps.setProperty("Org","Orange");

                //将属性对象写入到配置文件中

                ps.store(new FileOutputStream(f),"Fruit Config");

            }

        }catch(Exception e){

            e.printStackTrace();

        }

       

        return ps;

    }

}

 

publicclass hello{

    publicstaticvoid main(String args[])throws Exception{

        //获取属性配置

        Properties classConf = Init.getProperties();

        //根据key读取配置的value的值

        //可以看到我们简化了包类名,相当于包类名的简写

        Fruit f1 = Factory.getInstance(classConf.getProperty("App"));

        f1.eat();

        Fruit f2 = Factory.getInstance(classConf.getProperty("Org"));

        f2.eat();

    }

}

 

 

3、  Annotation,注解,用于标识程序的一些额外信息

 

 

4、  JDBC,java database connector(java数据库连接器)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中,可以通过配置文件将线程池的参数与代码分离。具体步骤如下: 1. 在`application.properties`或`application.yml`配置文件中添加线程池的参数,例如: ```properties # 线程池核心线程数 spring.task.execution.pool.core-size=10 # 线程池最大线程数 spring.task.execution.pool.max-size=20 # 线程池队列容量 spring.task.execution.pool.queue-capacity=100 # 线程池线程空闲时间 spring.task.execution.pool.keep-alive=60s ``` 2. 在代码中使用`@ConfigurationProperties`注解将配置文件中的参数注入到线程池配置类中,例如: ```java import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "spring.task.execution.pool") public class ThreadPoolConfig { private int coreSize; private int maxSize; private int queueCapacity; private String keepAlive; // 省略getter和setter方法 } ``` 3. 在需要使用线程池的地方,通过依赖注入的方式使用线程池配置类,例如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; @Service public class MyService { @Autowired private ThreadPoolConfig threadPoolConfig; @Async public void doSomething() { // 使用线程池执行异步任务 // ... } } ``` 通过以上步骤,就可以将Spring Boot中的线程池配置参数与代码分离,方便进行参数的修改和管理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值