想了解如何在Spring中以编程方式解析属性占位符的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于spring占位符的相关问题,此外,我们还将为您介绍关于asp.net–如何在Sitecor
想了解如何在Spring中以编程方式解析属性占位符的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于spring 占位符的相关问题,此外,我们还将为您介绍关于asp.net – 如何在Sitecore中以编程方式创建项目、ios – 如何在Swift中以编程方式创建约束?、java – Spring属性占位符无法正常工作、Spring 3表达式语言如何与属性占位符交互?的新知识。
本文目录一览:- 如何在Spring中以编程方式解析属性占位符(spring 占位符)
- asp.net – 如何在Sitecore中以编程方式创建项目
- ios – 如何在Swift中以编程方式创建约束?
- java – Spring属性占位符无法正常工作
- Spring 3表达式语言如何与属性占位符交互?
如何在Spring中以编程方式解析属性占位符(spring 占位符)
我目前在基于Spring 3.1.0.M1的Web应用程序上工作,基于注释,并且在应用程序的一个特定位置解析属性占位符时遇到问题。
这是故事。
1)在我的Web应用程序上下文中(由DispatcherServlet加载),我有
mvc-config.xml:
<!-- Handles HTTP GET requests for /resources/version/** --><resources mapping="/${app.resources.path}/**" location="/static/" cache-period="31556926"/>...<!-- Web properties --><context:property-placeholder location=" classpath:app.properties "/>
2)在app.properties中,有2个属性,其中包括:
app.properties:
# Properties provided (filtered) by Maven itselfapp.version: 0.1-SNAPSHOT...# Static resources mappingapp.resources.path: resources/${app.version}
3)我的JSP
2.1模板中有一个JSP自定义标记。该标签负责根据环境设置,应用程序版本,spring主题选择等来进行完整的资源路径构建。自定义标签类扩展了spring:url实现类,因此它可以被视为常用的url标签,但具有有关正确路径的一些附加知识。
我的问题是我无法在JSP自定义标记实现中正确解析$
{app.resources.path}。JSP自定义标签是由servlet容器而不是Spring管理的,因此不参与DI。所以我不能只使用通常的@Value(“
$ {app.resources.path}”)并由Spring自动解决它。
我所拥有的只是Web应用程序上下文实例,因此我必须以编程方式解析我的属性。
到目前为止,我尝试了:
ResourceTag.java:
// returns nullPropertyResolver resolver = getRequestContext().getWebApplicationContext().getBean(PropertyResolver.class);resolver.getProperty("app.resources.path");// returns null, its the same web context instance (as expected)PropertyResolver resolver2 = WebApplicationContextUtils.getRequiredWebApplicationContext(pageContext.getServletContext()).getBean(PropertyResolver.class);resolver2.getProperty("app.resources.path");// throws NPE, resolver3 is null as StringValueResolver is not boundStringValueResolver resolver3 = getRequestContext().getWebApplicationContext().getBean(StringValueResolver.class);resolver3.resolveStringValue("app.resources.path");// null, since context: property-placeholder does not register itself as PropertySourceEnvironment env = getRequestContext().getWebApplicationContext().getEnvironment();env.getProperty("app.resources.path");
所以现在我有点坚持了。我知道解析占位符的能力就在上下文中,我只是不知道正确的方法。
任何帮助或想法检查,我们高度赞赏。
答案1
小编典典我认为您可以只定义一个新的util:properties,而不是着眼于上下文占位符的内部工作:
<util:properties id="appProperties" location="classpath:app.properties" />
并在您的代码中像这样使用它:
Properties props = appContext.getBean("appProperties", Properties.class);
或像这样在任何可以做DI的地方:
@Value("#{appProperties[''app.resources.path'']}")
asp.net – 如何在Sitecore中以编程方式创建项目
Home -England -France -Germany
有一个子布局(CommentsForm.ascx),用于所有3个页面.当用户浏览“法国”并提交评论时,“评论”项目应保存在“法国”下,依此类推.
在这种情况下,父项(必须在其下创建新项)是动态的.那么,在这种情况下如何获取父项.它是否正确?
protected void btnSubmit_Click(object sender,EventArgs e) { Sitecore.Data.Database masterDB = Sitecore.Configuration.Factory.GetDatabase("master"); Item parentItem = Sitecore.Context.Item; string name = "Comment_" + Sitecore.DateUtil.IsoNow; TemplateItem template = masterDb.GetTemplate("/sitecore/templates/userdefined/Comment"); using (new Securitydisabler()) { //how to go about here?? //newItem["Author"] = txtAuthor.text; //newItem["CommentText"] = txtComments.Text; //parentItem.Add("name",template); } }
解决方法
编辑和重命名必须在Editing.BeginEdit()事务中进行
Sitecore.Data.Database masterDB = Sitecore.Configuration.Factory.GetDatabase("master"); Item parentItem = Sitecore.Context.Item; string name = "Comment_" + Sitecore.DateUtil.IsoNow; var template = masterDb.GetTemplate("/sitecore/templates/userdefined/Comment"); using (new Sitecore.SecurityModel.Securitydisabler()) { try { Item newItem = parentItem.Add("Name",template); if (newItem!=null) { newItem.Editing.BeginEdit(); newItem["Author"] = txtAuthor.text; newItem["CommentText"] = txtComments.Text; newItem.Editing.EndEdit(); } } catch { newItem.Editing.CancelEdit(); } }
ios – 如何在Swift中以编程方式创建约束?
constraintButtonPlayWidth = NSLayoutConstraint(item: buttonPlay,attribute: NSLayoutAttribute.Width,relatedBy: NSLayoutRelation.Equal,toItem: self.view,multiplier: 1,constant: 100) self.view.addConstraint(constraintButtonPlayWidth)
但按钮拉得太大了;可能是因为toItem:self.view.我尝试修改约束的常量,但这并没有改变任何东西.
如何正确设置此约束,使其实际宽度为100?
解决方法
constraintButtonPlayWidth = NSLayoutConstraint (item: buttonPlay,toItem: nil,attribute: NSLayoutAttribute.NotAnAttribute,constant: 100) self.view.addConstraint(constraintButtonPlayWidth)
java – Spring属性占位符无法正常工作
我使用的以下配置(maven项目结构):
src / main / resources / properties / app.properties文件
#possible values: dev test prod mode: dev
在Spring配置中:
<context:property-placeholder location="classpath:properties/app.properties"/> <import resource="classpath:/spring/db/${mode}-datasource-config.xml"/>
根据${mode}的值,我想导入相应的数据源配置文件.
当我使用mvn clean install tomcat7:run命运行嵌入式tomcat7时,我收到错误:
10,2013 5:52:29 PM org.apache.catalina.core.StandardContext loadOnStartup SEVERE: Servlet /SpringWebFlow threw load() exception java.lang.IllegalArgumentException: Could not resolve placeholder 'mode' in string value "classpath:/spring/db/${mode}-datasource-config.xml"
目标/ classes / properties / app.properties文件存在.
我正在使用IntelliJ IDEA,在编辑器中我可以点击< import resource =“classpath中的”${mode}“:/ spring / db / ${mode} -datasource-config.xml”/>并在属性文件中查看其值.编辑器本身也将${mode}更改为灰色的dev,表明它可以识别属性值.在编辑器中,我看到:< import resource =“classpath:/spring/db/dev-datasource-config.xml”/>
任何想法为什么我得到错误以及如何解决?
解决方法
从版本3.1开始,您可以使用ApplicationContextinitializer将PropertySources添加到Enviroment中,以解决您的问题.
见http://blog.springsource.org/2011/02/15/spring-3-1-m1-unified-property-management/
执行相同操作的其他选项是使用配置文件:http://blog.springsource.org/2011/02/14/spring-3-1-m1-introducing-profile/
编辑
例如:
将初始化程序添加到web.xml
<context-param> <param-name>contextinitializerClasses</param-name> <param-value>foo.bar.AppContextinitializer</param-value> </context-param>
和初始化程序:
public class AppContextinitializer implements ApplicationContextinitializer<ConfigurableWebApplicationContext> { @Override public void initialize(ConfigurableWebApplicationContext applicationContext) { Properties props; try { props = PropertiesLoaderUtils.loadAllProperties("/some/path"); PropertiesPropertySource ps = new PropertiesPropertySource("profile",props); applicationContext.getEnvironment().getPropertySources().addFirst(ps); } catch (IOException e) { // handle error } } }
Spring 3表达式语言如何与属性占位符交互?
Spring 3引入了一种新的表达语言(SpEL),可以在bean定义中使用。语法本身已经很好地指定了。
目前还不清楚SpEL如何与以前版本中已经存在的属性占位符语法进行交互。SpEL是否支持属性占位符,还是我必须结合两种机制的语法并希望它们结合起来?
让我举一个具体的例子。我想使用属性语法${x.y.z}
,但要加上elvis运算符提供的“默认值”语法,以处理${x.y.z}
未定义的情况。
我尝试了以下语法,但没有成功:
#{x.y.z?:''defaultValue''}
#{${x.y.z}?:''defaultValue''}
第一个给我
在类型为“ org.springframework.beans.factory.config.BeanExpressionContext”的对象上找不到字段或属性“ x”
这表明SpEL无法将其识别为财产占位符。
第二个语法抛出异常说占位符不被识别,所以占位符解析器是被调用,但未能如预期,因为没有定义属性。
该文档没有提及这种交互,因此这是不可能的,或者没有记载。
有人设法做到了吗?
好的,我为此提出了一个小型的独立测试用例。所有这些都按原样工作:
首先,bean的定义:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd "> <context:property-placeholder properties-ref="myProps"/> <util:properties id="myProps"> <prop key="x.y.z">Value A</prop> </util:properties> <bean id="testBean"> <!-- here is where the magic is required --> <property name="value" value="${x.y.z}"/> <!-- I want something like this <property name="value" value="${a.b.c}?:''Value B''"/> --> </bean> </beans>
然后,普通的bean类:
包装测试;
public class Bean { String value; public void setValue(String value) { this.value = value; }}
最后,测试用例:
package test;import static org.hamcrest.Matchers.*;import static org.junit.Assert.*;import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfigurationpublic class PlaceholderTest { private @Resource Bean testBean; @Test public void valueCheck() { assertThat(testBean.value, is("Value A")); }}
挑战-在bean文件中提供SpEL表达式,允许我在${x.y.z}
无法解析的情况下指定默认值,并且必须将默认值指定为表达式的一部分,而不是在另一个属性集中外部化。
答案1
小编典典要从SpEL表达式访问属性占位符,可以使用以下语法:#{''${x.y.z}''}
。但是,用elvis运算符和默认值无法解决你的问题,因为它${x.y.z}
在无法解决时会引发异常。
但是你不需要SpEL来声明属性的默认值:
<context:property-placeholder location="..." properties-ref="defaultValues"/><bean id = "defaultValues"> <property name="properties"> <props> <prop key="x.y.z">ZZZ</prop> </props> </property></bean><bean ...> <property name = "..." value = "${x.y.z}" /></bean>
关于如何在Spring中以编程方式解析属性占位符和spring 占位符的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于asp.net – 如何在Sitecore中以编程方式创建项目、ios – 如何在Swift中以编程方式创建约束?、java – Spring属性占位符无法正常工作、Spring 3表达式语言如何与属性占位符交互?等相关知识的信息别忘了在本站进行查找喔。
本文标签: