美文网首页
Spring IoC/DI 09-Profile 注入不同环境下

Spring IoC/DI 09-Profile 注入不同环境下

作者: 周老一员 | 来源:发表于2020-02-21 17:50 被阅读0次

    Profile 注入不同环境下的 Bean

    原理

    在注入 Bean 的时候通过 Profile 为 Bean 设定环境

    实现

    创建一个普通类

    DataSource.java

    public class DataSource {
        private String url;
        private String username;
        private String password;
        private String driveClassName;
    
        @Override
        public String toString() {
            return "DataSource{" +
                    "url='" + url + '\'' +
                    ", username='" + username + '\'' +
                    ", password='" + password + '\'' +
                    ", driveClassName='" + driveClassName + '\'' +
                    '}';
        }
    
        public DataSource(String url, String username, String password, String driveClassName) {
            this.url = url;
            this.username = username;
            this.password = password;
            this.driveClassName = driveClassName;
        }
    
        public String getUrl() {
            return url;
        }
    
        public void setUrl(String url) {
            this.url = url;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public String getDriveClassName() {
            return driveClassName;
        }
    
        public void setDriveClassName(String driveClassName) {
            this.driveClassName = driveClassName;
        }
    }
    
    

    注入两个相同类型和名称的Bean,用Profile设定环境

    Java配置

    JavaConfig.java

    @Configuration
    public class JavaConfig {
    
        @Bean("ds")
        @Profile("dev")
        DataSource devDs(){
            return new DataSource("jdbc:mysql://localhost/dev", "dev", "dev", "com.mysql.jdbc.Driver");
        }
    
        @Bean("ds")
        @Profile("prod")
        DataSource prodDs(){
            return new DataSource("jdbc:mysql://localhost/prod", "prod", "prod", "com.mysql.jdbc.Driver");
        }
    }
    

    应用

    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.getEnvironment().setActiveProfiles("prod"); // 先设定环境
    context.register(JavaConfig.class); // 再加载Java配置类
    context.refresh(); // 最后刷新
    DataSource ds = (DataSource) context.getBean("ds");
    System.out.println(ds);
    

    XML配置

    applicationContext.xml

    <beans profile="dev">
        <bean class="org.daistudy.bean.DataSource" id="ds">
            <constructor-arg name="url" value="jdbc:mysql://localhost/dev"/>
            <constructor-arg name="username" value="dev"/>
            <constructor-arg name="password" value="dev"/>
            <constructor-arg name="driveClassName" value="com.mysql.jdbc.Driver"/>
        </bean>
    </beans>
    
    <beans profile="prod">
        <bean class="org.daistudy.bean.DataSource" id="ds">
            <constructor-arg name="url" value="jdbc:mysql://localhost/prod"/>
            <constructor-arg name="username" value="prod"/>
            <constructor-arg name="password" value="prod"/>
            <constructor-arg name="driveClassName" value="com.mysql.jdbc.Driver"/>
        </bean>
    </beans>
    

    应用

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
    context.getEnvironment().setActiveProfiles("dev");
    context.setConfigLocation("applicationContext.xml");
    context.refresh();
    DataSource ds = (DataSource) context.getBean("ds");
    System.out.println(ds);
    

    相关文章

      网友评论

          本文标题:Spring IoC/DI 09-Profile 注入不同环境下

          本文链接:https://www.haomeiwen.com/subject/eeqhqhtx.html