Springシンプルケース(spring profile)u03


私はspring 4 xを使っていますが、ここではspring 4の新しい特性についても議論します。
     spring profileは個人的には違う状況で違う方案を使うためです。個人的には、どのように使いたいですか?
     無駄話は多く言わないで直接コードをかけます。まず配置ファイルです。

package com.expect.oa.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.expect.oa.*")
//@ComponentScan(basePackages={"com.expect.oa.DI.*","com.expect.oa.DI2.*"})//        
public class SpringConfig {

}
     いくつかのクラスを作成して、ここで論理が必要です。二つの種類が同じクラスを作ることができます。このようにspringは間違いがあります。しかし、ここでprofileを使いました。この時にアクティブ化されたどの案がどのような案で構成されているかを確認します。この言語の表現力は本当に汚いです。これは知っています。コードを見ましょう。

package com.expect.oa.profile;
//     ,      A, B    。
public class ProEntity {

	public ProEntity(String pro) {
		
		System.out.println(pro);
		
	}
}

     次はクラスAです。上のクラスを作るために使われています。心が本当に疲れました。ハハ:

package com.expect.oa.profile;

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

@Configuration
@Profile(value={"profileA"})//     ,     A   
public class Profile_A {

	@Bean
	public ProEntity getProEntity (){
		return new ProEntity("  A  ");
	}
	
}
     以下はクラスBで、上のクラスを構成するためのものです。心が疲れて、上のコードです。

package com.expect.oa.profile;

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

@Configuration
@Profile(value={"profileB"})//     ,     B   
public class Profile_B {
	
	@Bean
	public ProEntity getProEntity (){
		return new ProEntity("  B  ");
	}

}
     はい、テストしましょう。

package com.expect.oa.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.expect.oa.config.SpringConfig;
import com.expect.oa.profile.ProEntity;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SpringConfig.class})
@ActiveProfiles(value={"profileB"})//     ,     B   。        A
public class TestProfile {

	@Autowired
	ProEntity pEntity;
	
	@Test
	public void testProfile (){
		
	}
	
}