惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
Recent Commits to openclaw:main
Recent Commits to openclaw:main
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
罗磊的独立博客
V
Visual Studio Blog
爱范儿
爱范儿
H
Help Net Security
J
Java Code Geeks
I
InfoQ
Recent Announcements
Recent Announcements
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Recorded Future
Recorded Future
Jina AI
Jina AI
Microsoft Security Blog
Microsoft Security Blog
WordPress大学
WordPress大学
GbyAI
GbyAI
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Y
Y Combinator Blog
Google DeepMind News
Google DeepMind News
Scott Helme
Scott Helme
S
SegmentFault 最新的问题
S
Securelist
L
LINUX DO - 热门话题
Cyberwarzone
Cyberwarzone
C
Cisco Blogs
Simon Willison's Weblog
Simon Willison's Weblog
G
Google Developers Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
T
The Blog of Author Tim Ferriss
博客园_首页
B
Blog
F
Fortinet All Blogs
AWS News Blog
AWS News Blog
V
Vulnerabilities – Threatpost
S
Secure Thoughts
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Forbes - Security
Forbes - Security
S
Security @ Cisco Blogs
T
Threat Research - Cisco Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
Schneier on Security
Project Zero
Project Zero
Martin Fowler
Martin Fowler
C
Cybersecurity and Infrastructure Security Agency CISA
N
Netflix TechBlog - Medium
N
News and Events Feed by Topic

博客园 - PointNet

log4j2发送消息至Kafka log4j2自定义Appender(输出到文件/RPC服务中) log4j 不同模块输出到不同的文件 shell之磁盘容量检查,配合crontab可以定时清理磁盘 How to do conditional auto-wiring in Spring? MyBatis传入参数为集合 list 数组 map写法 Spring Boot @Autowired 没法自动注入的问题 mysql5.5 uuid做主键与int做主键的性能实测 dom4j解析xml字符串实例 spring自动注入是单例还是多例?单例如何注入多例? Spring中Bean的五个作用域 【总结】瞬时高并发(秒杀/活动)Redis方案 浅谈分布式事务 基于Redis实现分布式锁 MySQL事务隔离级别详解 Redis学习手册(Sorted-Sets数据类型) Redis的快照持久化-RDB与AOF Redis - 事务 ArrayList、Vector、HashMap、HashTable、HashSet的默认初始容量、加载因子、扩容增量
spring注解之@profile
PointNet · 2017-10-13 · via 博客园 - PointNet

spring中@profile与maven中的profile很相似,通过配置来改变参数。

例如在开发环境与生产环境使用不同的参数,可以配置两套配置文件,通过@profile来激活需要的环境,但维护两套配置文件不如maven中维护一套配置文件,在pom中通过profile来修改配置文件的参数来的实惠。

也有例外,比如我在开发中调用商城接口经常不能返回我需要的数据,每次都需要mock数据,所以我写了一个mock参数的借口调用类,在开发环境中就使用这个类,测试环境与生产环境则使用正常的借口调用类,这样就不用每次开发的时候去手动改一些代码。

注:@profile在3.2以后的版本支持方法级别和类级别,3.1版本只支持类级别。

言归正传,说下@profile使用方法。

一、注解配置

  1. @service("productRpc")  
  2. @profile("prop")  
  3. public class ProductRpcImpl implements ProductRpc  
  4.     public String productBaseInfo(Long sku){  
  5.         return productResource.queryBaseInfo(Long sku);  
  6.     }  
  7. }  
  8.   
  9.   
  10. @service("productRpc")  
  11. @profile("dev")  
  12. public class MockProductRpcImpl implements ProductRpc  
  13.     public String productBaseInfo(Long sku){  
  14.         return “iphone7”;  
  15.     }  
  16. }  
  17.   
  18. public class Demo(){  
  19.     @Resource(name="productRpc")  
  20.     private ProductRpc productRpc;  
  21.   
  22.     public void demo(){  
  23.         String skuInfo = productRpc.productBaseInfo(123123L);  
  24.         logger.info(skuInfo);  
  25.     }  
  26. }  

这样就完成了基于注解的profile配置。当配置为生产环境的时候会正常调用接口,当为开发环境的时候回调用mock接口。

二、XML配置

  1. <beans profile="dev">  
  2.     <bean id="beanname" class="com.pz.demo.ProductRPC"/>  
  3. </beans>  
  4.   
  5. <beans profile="dev">  
  6.     <bean id="beanname" class="com.pz.demo.MockProductRPC"/>  
  7. </beans>  

三、激活profile

注:spring在确定那个profile处于激活状态的时,需要依赖两个独立的属性:spring.profiles.active和spring.profile.default。如果设置了spring.profiles.actives属性,那么它的值就会用来确定那个profile是激活的。如果没有设置spring.profiles.active属性的话,那spring将会查找spring.profiles.default的值。如果spring.profiles.active和spring.profiles.default均没有设置。(红色部分未在项目中验证成功,待测试)

  1.在servlet上下文中进行配置(web.xml)

  1. <context-param>  
  2.     <param-name>spring.profiles.default</param-name>  
  3.     <param-value>dev</param-value>  
  4. </context-param>  

  2.作为DispatcherServlet的初始化参数

  1. ervlet>  
  2.     <servlet-name>springMVC</servlet-name>  
  3.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  4.     <init-param>  
  5.         <param-name>contextConfigLocation</param-name>  
  6.         <param-value>classpath:/spring-servlet.xml</param-value>  
  7.     </init-param>  
  8.     <init-param>  
  9.         <param-name>spring.profiles.default</param-name>  
  10.         <param-value>dev</param-value>  
  11.     </init-param>  
  12.     <load-on-startup>1</load-on-startup>  
  13. </servlet>  

  3.spring-junit使用@ActiveProfiles进行激活

  1. @RunWith(SpringJUnit4ClassRunner.class)  
  2. @ContextConfiguration(locations = "/spring-config.xml")  
  3. @ActiveProfiles("dev")  
  4. public class MainTest {  
  5.     ...  
  6. }  

    4.作为JNDI条目

    5.作为环境变量

    6.作为JVM的系统属性