SAPSAP 修行SAP札记

SAP Commerce配置属性的优先级

2020-04-13  本文已影响0人  _扫地僧_

https://help.sap.com/viewer/b490bb4e85bc42a7aa09d513d0bcb18e/1905/en-US/8b8e13c9866910149d40b151a9196543.html

优先级从高到低:

  1. The local.properties file
  1. Extension-specific project.properties files
  2. The global project.properties file (that is, the project.properties file in the <${HYBRIS_BIN_DIR}/platform> directory)

这张表一目了然:

不推荐修改platform文件夹下面的project.properties文件,原因如下:

  1. Commerce升级时,这个文件的内容会被覆盖。
  2. 在生产场景里,可能根本不具备这个文件所在的文件夹的修改权限。

如何在代码里访问properties

SAP Commerce properties are provided in the global ApplicationContext. With this, you can inject a specific value by using the usual Spring notation:

<bean class="Foo">
  <property name="bar" value="${key_of_property}"/>
</bean>

The properties are provided by PropertyPlaceholderConfigurer with hybrisPropertiesConfigurer id, which gets the hybrisProperties bean injected. This bean fulfills the java.util.Properties class and holds all properties. You can use this bean in other scenarios too, especially if you want to use the SAP Commerce properties in your WebApplicationContext, having the global ApplicationContext as a parent. For this, define:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="properties" ref="hybrisProperties"/>
</bean>

关于ref的用法,可以参考这个例子:

BankService类:

Account是一个接口,有set和get方法。

bean.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
    <bean id="accountDetails" class="com.jwt.spring.AccountImpl" />
    <bean id="bankservice" class="com.jwt.spring.BankService">
        <property name="accdetails">
            <ref bean="accountDetails" />
        </property>
    </bean>
 
</beans>

Be aware that this mechanism is not tenant aware. You always get the properties of the master tenant and you do not recognize changes to properties done at runtime. As the configurator is a BeanFactoryPostProcessor, it gets applied once at a startup of a context.

测试代码:

package com.jwt.spring;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class Test {
    public static void main(String[] args) {
 
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "bean.xml");
        BankService bankSer = (BankService) context.getBean("bankservice");
 
        bankSer.withdraw(1000000, 2000.00);
        bankSer.deposit(1000000, 2000.00);
    }
}

ServiceLayer的访问方式:

On the ServiceLayer, SAP Commerce configuration is reachable using the ConfigurationService. Calling the getConfiguration() method returns a Configuration object. By factory default implementation, the returned object is a HybrisConfiguration object, as shown in the UML diagram.

最佳实践

A common practice to override property values is to copy individual properties from the project.properties and to paste them into the local.properties file with an edited value.

可以利用JVM的启动参数:

java -Dfoo="some string" SomeClass

等价于:
This is the equivalent to adding the following lines to the local.properties file, but the VM parameters are runtime only:

db.url=jdbc:mysql://localhost/trunk?useConfigs=maxPerformance&characterEncoding=utf8
db.driver=com.mysql.jdbc.Driver
db.username=root
db.password=root

要获取更多Jerry的原创文章,请关注公众号"汪子熙":


上一篇下一篇

猜你喜欢

热点阅读