iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >@profile注解如何在spring中使用
  • 912
分享到

@profile注解如何在spring中使用

springprofile 2023-05-30 23:05:49 912人浏览 薄情痞子
摘要

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

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

首先是新建Maven工程

mvn archetype:generate -DarchetypeCatalog=internal

下面是pom文件:

<properties>   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>   <springframework.version>4.3.7.RELEASE</springframework.version>  </properties>   <dependencies>   <dependency>    <groupId>junit</groupId>    <artifactId>junit</artifactId>    <version>4.12</version>    <scope>test</scope>   </dependency>   <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->   <dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-context</artifactId>    <version>${springframework.version}</version>   </dependency>   <!-- Https://mvnrepository.com/artifact/org.springframework/spring-test -->   <dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-test</artifactId>    <version>${springframework.version}</version>   </dependency>   </dependencies>  <build>   <plugins>    <plugin>     <groupId>org.apache.maven.plugins</groupId>     <artifactId>maven-compiler-plugin</artifactId>     <configuration>      <source>1.8</source>      <target>1.8</target>      <encoding>utf-8</encoding>     </configuration>    </plugin>    <plugin>     <artifactId>maven-assembly-plugin</artifactId>     <version>3.0.0</version>     <configuration>      <arcHive>       <manifest>        <mainClass>com.xueyou.demo</mainClass>       </manifest>      </archive>      <descriptorRefs>       <descriptorRef>jar-with-dependencies</descriptorRef>      </descriptorRefs>     </configuration>     <executions>      <execution>       <id>make-assembly</id> <!-- this is used for inheritance merges -->       <phase>package</phase> <!-- bind to the packaging phase -->       <Goals>        <goal>single</goal>       </goals>      </execution>     </executions>    </plugin>   </plugins>  </build>

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

@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;   @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的方式进行设置:

<context-param>  <param-name>spring.profiles.active</param-name>  <param-value>DOUBLEUPMINT</param-value> </context-param>

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

--结束END--

本文标题: @profile注解如何在spring中使用

本文链接: https://www.lsjlt.com/news/221982.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • @profile注解如何在spring中使用
    本篇文章给大家分享的是有关@profile注解如何在spring中使用,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。首先是新建maven工程mvn archetype:gene...
    99+
    2023-05-30
    spring profile
  • Spring @Profile注解如何使用
    这篇文章主要介绍“Spring @Profile注解如何使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Spring @Profile注解如何使用”文章能帮助大家解决问题。使用...
    99+
    2023-07-06
  • 如何在Spring中使用@Transactional注解
    这期内容当中小编将会给大家带来有关如何在Spring中使用@Transactional注解,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。@Transactionalservice A(){try...
    99+
    2023-06-15
  • 详解Spring @Profile注解的使用和源码解析
    目录介绍使用通过Environment设置profile通过JVM参数设置SpringBoot通过yml进行配置源码解析BeanDefinition注册shouldSkip源码Pro...
    99+
    2023-05-15
    Spring @Profile注解使用 Spring @Profile注解 Spring @Profile
  • 如何在Spring中使用@Override和@Autowired注解
    如何在Spring中使用@Override和@Autowired注解?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。一、Override首先,@Override 注解是伪代码...
    99+
    2023-06-15
  • Java中如何使用Spring注解
    Java中如何使用Spring注解,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。在Spring4之后,要使用注解开发,必须要保证aop的包导入了使用注解需要导入contex...
    99+
    2023-06-20
  • Spring中@ModelAttribute注解如何使用
    这期内容当中小编将会给大家带来有关Spring中@ModelAttribute注解如何使用,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。1.@ModelAttribute注释方法   例子(1)...
    99+
    2023-06-02
  • spring中如何使用@Service注解
    本篇文章为大家展示了spring中如何使用@Service注解,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。@Service注解的使用要说明@Service注解的使用,就得说一下我们经常在sprin...
    99+
    2023-06-20
  • 在Spring-Boot中如何使用@Value注解注入集合类
    我们在使用spring框架进行开发时,有时候需要在properties文件中配置集合内容并注入到代码中使用。本篇文章的目的就是给出一种可行的方式。 1.注入 通常来说,我们都使用@V...
    99+
    2024-04-02
  • Spring @InitBinder注解如何使用
    这篇文章主要讲解了“Spring @InitBinder注解如何使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Spring @InitBinder注解如何使用”吧!一...
    99+
    2023-07-05
  • C++中profile如何使用
    这篇文章将为大家详细讲解有关C++中profile如何使用,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。自从VC6以后,C++ profile功能便从Team Server Editions...
    99+
    2023-06-17
  • Spring @ComponentScan注解如何使用
    今天小编给大家分享一下Spring @ComponentScan注解如何使用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解...
    99+
    2023-07-05
  • 在Spring Boot中使用注解如何实现Redis 缓存
    在Spring Boot中使用注解如何实现Redis 缓存?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。一、创建 Caching 配置类RedisKeys.Javapackag...
    99+
    2023-05-31
    springboot redis 注解
  • 如何使用注解开发spring
    本篇文章为大家展示了如何使用注解开发spring,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。在Spring4之后,要使用注解开发,必须要保证aop的包导入了。使用注解需要导入context约束,增...
    99+
    2023-06-15
  • @Around注解怎么在Spring AOP中使用
    这期内容当中小编将会给大家带来有关@Around注解怎么在Spring AOP中使用,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。@Around注解可以用来在调用一个具体方法前和调用后来完成一些具体的任务...
    99+
    2023-06-06
  • PropertySource注解怎么在Spring boot中使用
    本篇文章给大家分享的是有关PropertySource注解怎么在Spring boot中使用,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。1.1. PropertySource...
    99+
    2023-05-30
    springboot propertysource
  • 注解如何在JAVA中使用
    注解如何在JAVA中使用 ?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。何为注解注解(Annotation)又称为元数据,在JDK1.5后引入,它的作用是:生成...
    99+
    2023-05-31
    java 注解 ava
  • Spring Security中的权限注解如何使用
    今天小编给大家分享一下Spring Security中的权限注解如何使用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下...
    99+
    2023-06-30
  • profile怎么在mysql中使用
    这篇文章给大家介绍profile怎么在mysql中使用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。profile是什么当我们要对某一条sql的性能进行分析时,可以使用它。Profil...
    99+
    2024-04-02
  • 在Spring Boot项目中如何实现使用 Mybatis中的@ Annotation注解
    在Spring Boot项目中如何实现使用 Mybatis中的@ Annotation注解?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。一、运行 springboot-myba...
    99+
    2023-05-31
    springboot mybatis @ annotation
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作