[英]declare spring bean of string type one line
I want to declare a bean of String type in an spring xml context in the shortest way possible. 我想以最短的方式在spring xml上下文中声明String类型的bean。 So far this is the shortest way I've found
到目前为止,这是我发现的最短方法
<bean id="bean1" class="java.lang.String">
<constructor-arg type="String" value="someword" />
</bean>
However, for readability, I would like to 'stack' multiple of these neatly, kinda like so, and I can't just put all the code on one line because of team standards about line code length: 但是,出于可读性考虑,我想像这样整齐地“堆叠”多个代码,并且由于团队关于行代码长度的标准,我不能将所有代码都放在一行上:
<bean id="bean1" class="java.lang.String">blah blah </bean>
<bean id="bean2" class="java.lang.String">blah blah </bean>
<bean id="bean3" class="java.lang.String">blah blah </bean>
For readability I would still prefer keeping it all in the context file and also I can redeploy the context file separately to the classpath without having to redeploy the whole .jar
为了提高可读性,我仍然希望将其全部保留在上下文文件中,而且我可以将上下文文件分别重新部署到类路径,而不必重新部署整个.jar
If you want to keep everything in the context file and want to simplify it then you should be using PropertyPlaceholderConfigurer
instead of String
beans. 如果要将所有内容保留在上下文文件中并希望对其进行简化,则应使用
PropertyPlaceholderConfigurer
而不是String
bean。
Here is an example, 这是一个例子
project.properties
. project.properties
的属性文件。 Put all your Strings
inside that file as {key=value}
pair. 将所有
Strings
作为{key=value}
对放入该文件中。 For Example, 例如,
myStrings.cnst1=FOO myStrings.cnst2=BAR myStrings.cnst3=FOOBAR
Now, declare a PropertyPlaceholderConfigurer
inside your context file as follows: 现在,在上下文文件中声明一个
PropertyPlaceholderConfigurer
,如下所示:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>project.properties</value> </property>
Now, you can easily access the values as follows: 现在,您可以轻松访问以下值:
@Value("${myStrings.cnst1}") private String someString;
Refer this link and this link for more information. 请参阅此链接和此链接以获取更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.