• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
第二天半
博客园    首页    新随笔    联系   管理    订阅  订阅

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();
    }
}

 

posted @ 2017-09-17 15:36  第二天半  阅读(188)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3