网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

@profile注解如何在spring中使用

本篇文章给大家分享的是有关@profile注解如何在spring中使用,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

创新互联建站主营福鼎网站建设的网络公司,主营网站建设方案,重庆APP开发,福鼎h5微信小程序开发搭建,福鼎网站营销推广欢迎福鼎等地区企业咨询

首先是新建maven工程

mvn archetype:generate -DarchetypeCatalog=internal

下面是pom文件:

 
  UTF-8 
  4.3.7.RELEASE 
  
 
  
   
   junit 
   junit 
   4.12 
   test 
   
   
   
   org.springframework 
   spring-context 
   ${springframework.version} 
   
   
   
   org.springframework 
   spring-test 
   ${springframework.version} 
   
 
  
  
   
    
    org.apache.maven.plugins 
    maven-compiler-plugin 
     
     1.8 
     1.8 
     utf-8 
     
    
    
    maven-assembly-plugin 
    3.0.0 
     
      
       
       com.xueyou.demo 
       
      
      
      jar-with-dependencies 
      
     
     
      
      make-assembly  
      package  
       
       single 
       
      
     
    
   
 

整体看一下工程中的类和接口:

@profile注解如何在spring中使用

首先是Person类中有一个speak的方法,这个方法是MoveFactor这个借口提供的。Chinese、English和German都实现了这个接口。但是这三个类的@profile中的值是不同的。通过SpringTest中分配不同的activeprofile就能够实现调用不同的speak方法。

下面看代码:
MoveFactor.interface

package com.xueyou.demo; 
 
 
public interface MoveFactor { 
  void speak(); 
}

Person.java

package com.xueyou.demo; 
 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Component; 
 
@Component 
public class Person { 
 
  @Autowired 
  private MoveFactor moveFactor; 
 
  public void speak(){ 
    moveFactor.speak(); 
  } 
}

Chinese.java

package com.xueyou.demo; 
 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.Profile; 
import org.springframework.stereotype.Component; 
 
 
@Configuration 
@Profile(value = "dev") 
@Component 
public class Chinese implements MoveFactor { 
  @Override 
  public void speak() { 
    System.out.println("我是中国人"); 
  } 
}

English.java

package com.xueyou.demo; 
 
import org.springframework.context.annotation.Profile; 
import org.springframework.stereotype.Component; 
 
@Component 
@Profile("qa") 
public class English implements MoveFactor{ 
  @Override 
  public void speak() { 
    System.out.println("i am an English"); 
  } 
}

German.java

package com.xueyou.demo; 
 
import org.springframework.context.annotation.Profile; 
import org.springframework.stereotype.Component; 
 
 
@Component 
@Profile("prod") 
public class German implements MoveFactor{ 
  @Override 
  public void speak() { 
    System.out.println("i am a German"); 
  } 
}

使用springtest进行测试

package com.xueyou.demo; 
 
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; 
 
 
@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes = App.class) 
@ActiveProfiles("dev") 
public class SpringTest { 
 
  @Autowired 
  Person p; 
 
  @Test 
  public void testProfile(){ 
    p.speak(); 
  } 
 
}

运行结果:

@profile注解如何在spring中使用

当修改@ActiveProfile中的值时,输出的内容也会随之改变。

如果使用的是main函数进行真正的开发、测试和上线时,我们需要设置一下运行参数:

@profile注解如何在spring中使用

-D 后面加上需要设置的spring的属性,就能够在main函数中使用了。

App.java

package com.xueyou.demo; 
 
import org.springframework.context.ConfigurableApplicationContext; 
import org.springframework.context.annotation.AnnotationConfigApplicationContext; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
 
/** 
 * Hello world! 
// */ 
@Configuration 
@ComponentScan(basePackages = {"com.xueyou.demo"}) 
public class App { 
  public static void main(String[] args) { 
    ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(com.xueyou.demo.App.class); 
    Person p = context.getBean(Person.class); 
    p.speak(); 
  } 
}

运行结果:

@profile注解如何在spring中使用

如果需要得到当前的activeprofile可以通过ConfigurableApplicationContext的实例来的到。

@profile注解如何在spring中使用

最后提一下,如果是在web的后台项目中如何进行设置。这个时候我们通过xml的方式进行设置:

 
 spring.profiles.active 
 DOUBLEUPMINT 

以上就是@profile注解如何在spring中使用,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。


网页题目:@profile注解如何在spring中使用
当前链接:http://bjjierui.cn/article/gspeii.html

其他资讯