如果您对如何在Spring配置文件中为bean的属性分配一个Enum值?和springbean配置属性感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解如何在Spring配置文件中为bean的属性
如果您对如何在Spring配置文件中为bean的属性分配一个Enum值?和spring bean配置属性感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解如何在Spring配置文件中为bean的属性分配一个Enum值?的各种细节,并对spring bean配置属性进行深入的分析,此外还有关于java – Spring:如何在Spring配置中注入ENUM?、java – 在Spring EL中引用当前bean的属性、java – 在Spring配置文件中访问maven项目版本、java – 如何基于spring配置文件加载属性文件的实用技巧。
本文目录一览:- 如何在Spring配置文件中为bean的属性分配一个Enum值?(spring bean配置属性)
- java – Spring:如何在Spring配置中注入ENUM?
- java – 在Spring EL中引用当前bean的属性
- java – 在Spring配置文件中访问maven项目版本
- java – 如何基于spring配置文件加载属性文件
如何在Spring配置文件中为bean的属性分配一个Enum值?(spring bean配置属性)
我定义了一个独立的枚举类型,如下所示:
package my.pkg.types;public enum MyEnumType { TYPE1, TYPE2}
现在,我想将该类型的值注入bean属性:
<bean name="someName"> <property name="type" value="my.pkg.types.MyEnumType.TYPE1" /></bean>
…那行不通:(
我应该如何将枚举注入spring bean?
答案1
小编典典你是否尝试过“ TYPE1”?我想Spring还是要使用反射来确定“类型”的类型,因此完全限定的名称是多余的。Spring通常不接受冗余!
java – Spring:如何在Spring配置中注入ENUM?
package com.myorg.sparrow.s3Environment; import javax.annotation.Nonnull; public enum DocumentType { Document("document/",".xml.gz","binary/octet-stream","gzip",true); private final String path; private final String suffix; private final String contentType; private final String contentEncoding; private final Boolean compress; private DocumentType(@Nonnull final String path,@Nonnull final String suffix,@Nonnull final String contentType,@Nonnull final String contentEncoding,@Nonnull final Boolean compress) { this.path = path; this.suffix = suffix; this.contentType = contentType; this.contentEncoding = contentEncoding; this.compress = compress; } @Nonnull public String getPath() { return path; } @Nonnull public String getSuffix() { return suffix; } @Nonnull public String getContentType() { return contentType; } @Nonnull public String getContentEncoding() { return contentEncoding; } @Nonnull public Boolean isCompress() { return compress; } }
我想在Spring配置文件中注入DocumentType.Document的这个值
<bean id="s3Service"https://www.jb51.cc/tag/nes/" target="_blank">ness.xml.persist.S3Service"> <constructor-arg ref="awsCredentials" /> <constructor-arg value="**DocumentType.DOCUMENT**" /> // how do I inject it here? <constructor-arg value="${com.myorg.sparrow.s3EnvironmentConfiguration.S3EnvironmentConfigurator-destBucketName}" /> </bean>
我如何注入此值
<constructor-arg value="**DocumentType.DOCUMENT**" /> // how do I inject it here?
我是Spring框架的新手,不知道如何实现这一点
谢谢
解决方法
<bean id="s3Service"https://www.jb51.cc/tag/nes/" target="_blank">ness.xml.persist.S3Service"> <constructor-arg ref="awsCredentials" /> <constructor-arg value="Document" /> // We love Spring because it is simpler than we expect <constructor-arg value="${com.myorg.sparrow.s3EnvironmentConfiguration.S3EnvironmentConfigurator-destBucketName}" /> </bean>
java – 在Spring EL中引用当前bean的属性
XML配置:
<bean id="bean1"> <property name="prefix" value="bean1"/> </bean> <bean id="bean2"> <property name="prefix" value="bean2"/> </bean> <bean id="bean3"> <property name="prefix" value="bean3"/> </bean>
属性文件:
bean1.name=alfred bean2.name=bobby bean3.name=charlie
类:
class Mybean { @Value("${#{prefix}.name}") String name; }
主要类别:
public class Main { @Autowired List<MyBean> mybeans; }
解决方法
<beanhttps://www.jb51.cc/tag/fig/" target="_blank">fig.PropertyPlaceholderConfigurer"> <property name="location" value="app.properties"/> </bean> <bean id="bean1"> <property name="name" value="${bean1.name}"/> </bean> <bean id="bean2"> <property name="name" value="${bean2.name}"/> </bean> <bean id="bean3"> <property name="name" value="${bean3.name}"/> </bean>
java – 在Spring配置文件中访问maven项目版本
我想在页面中显示我当前运行的Web应用程序版本.该项目基于Maven,Spring和Wicket.
我想以某种方式获得maven的${project.version}的价值并在我的spring XML文件中使用它,类似于我使用Spring PropertyPlaceholderConfigurer读取属性文件以获取我在我的应用程序中使用的设置的方式.
如果我在我的Spring配置中将maven project.version作为变量提供,我可以这样做:
我怎样才能做到这一点?
最佳答案
您可以按照建议使用Maven filtering.
或者您可以直接使用Spring读取由Maven在meta-inf目录下创建的pom.properties文件:
meta-inf/groupId/artifactId/pom.properties" />
并使用豆.
后一种方法的唯一缺点是pom.properties是在包阶段时创建的(并且在测试期间不会出现).
java – 如何基于spring配置文件加载属性文件
org.springframework.core.env.Environment;
解决方法
命名约定application- {profile} .properties like
application-dev.properties,application-test.properties,
application-prod.properties
在application.properties中设置spring.profiles.active = dev,test等
关于如何在Spring配置文件中为bean的属性分配一个Enum值?和spring bean配置属性的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于java – Spring:如何在Spring配置中注入ENUM?、java – 在Spring EL中引用当前bean的属性、java – 在Spring配置文件中访问maven项目版本、java – 如何基于spring配置文件加载属性文件的相关信息,请在本站寻找。
本文标签: