zoukankan      html  css  js  c++  java
  • Spring4 In Action-3.5.1-@PropertySource运行时注入值

    Spring4 In Action-3.5.1-@PropertySource运行时注入值

    代码链接:

    接口:

    package com.zte.sound.service.bean;
    /**
     * 光盘 
     */
    public interface CompactDisc {
        public void play();
    }

    实现类:

    package com.zte.sound.service.bean.imp;
    
    import org.springframework.stereotype.Component;
    
    import com.zte.sound.service.bean.CompactDisc;
    
    //@Component注解会告诉Spring创建这个类的实例bean(注意,启动Component注解功能需要在xml里面配置)
    //@Component("newName")//这里,配置类已经创建了
    public class SgtPeppers implements CompactDisc {
    
        private String title;
        private String artist;
        
        public SgtPeppers(String title,String artist){
            this.title=title;
            this.artist=artist;
            System.out.println("SgtPeppers类实例化");
        }
        
        public void play() {
            System.out.println("Sgt Playing:title="+title+" artist="+artist);
        }
    
    }

    新实现类:

    package com.zte.sound.service.bean.imp;
    
    import org.springframework.stereotype.Component;
    
    import com.zte.sound.service.bean.CompactDisc;
    
    //@Component注解会告诉Spring创建这个类的实例bean(注意,启动Component注解功能需要在xml里面配置)
    //@Component("newName")//这里,配置类已经创建了
    public class SgtPeppers_new implements CompactDisc {
    
        private String title;
        private String artist;
        
        public SgtPeppers_new(String title,String artist){
            this.title=title;
            this.artist=artist;
            System.out.println("SgtPeppers_new类实例化");
        }
        
        public void play() {
            System.out.println("Sgt_new Playing:title="+title+" artist="+artist);
        }
    
    }

    配置文件:

    sgtppers.title=title_new
    sgtppers.artist=artist_new

    配置类:

    package configs;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Import;
    
    import com.zte.sound.service.bean.CompactDisc;
    import com.zte.sound.service.bean.imp.SgtPeppers;
    
    @Configuration
    @ComponentScan(basePackageClasses={SgtPeppers.class})
    public class CDPlayerConfig {
    
        @Bean
        public CompactDisc returnSgtPeppers(){//播放SgtPeppers
            return new SgtPeppers("title","artist");
        }
        
    }

    新配置类:

    package configs;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.core.env.Environment;
    
    import com.zte.sound.service.bean.imp.SgtPeppers;
    import com.zte.sound.service.bean.imp.SgtPeppers_new;
    
    @Configuration
    @ComponentScan(basePackageClasses={SgtPeppers.class})
    @PropertySource("classpath:/configs/app.properties")
    public class ExpressiveConfig {
        
        @Autowired
        Environment env;
    
        @Bean
        public SgtPeppers_new returnSgtPeppers_new(){
            String title=env.getProperty("sgtppers.title");
            String artist=env.getProperty("sgtppers.artist");
            return new SgtPeppers_new(title, artist);
        }
    
    }

    测试类:

    package test;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    import com.zte.sound.service.bean.CompactDisc;
    
    import configs.CDPlayerConfig;
    import configs.ExpressiveConfig;
    
    @RunWith(SpringJUnit4ClassRunner.class)//Spring的Junit测试,会在测试开始时,创建Spring的应用上下文
    //@ContextConfiguration(classes=CDPlayerConfig.class)//表明配置类
    @ContextConfiguration(classes=ExpressiveConfig.class)//表明配置类
    public class SpringTest1 {
    
        //自动装配
        @Autowired
        private CompactDisc sp;
        
        @Test 
        public void instanceSpring(){
            //自动装配
            sp.play();
        }
    }
  • 相关阅读:
    Vue学习笔记之Vue指令系统介绍
    Vue学习笔记之Vue的使用
    Vue学习笔记之Vue介绍
    vim笔记
    python安装包的方式
    基本认证与摘要认证
    curl常用指令
    python优缺点小结
    环境变量的作用
    mac os、linux及unix之间的关系
  • 原文地址:https://www.cnblogs.com/zjsy/p/7535760.html
Copyright © 2011-2022 走看看