GVKun编程网logo

如何在Spring配置文件中为bean的属性分配一个Enum值?(spring bean配置属性)

25

如果您对如何在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配置属性)

如何在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?

java – Spring:如何在Spring配置中注入ENUM?

我有一个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的属性

java – 在Spring EL中引用当前bean的属性

我想从一个类中创建一些bean,所有这些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;
}

解决方法

您可以使用PropertyPlaceholderConfigurer直接设置bean的名称(而不是存储其前缀):
<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项目版本

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配置文件加载属性文件

java – 如何基于spring配置文件加载属性文件

如何创建项目架构以支持多个环境.在 Spring的帮助下,每个环境将具有来自不同属性文件的不同数据源,如(dev-propertiesfile,test-propertyFil,Production-propertyfile)

org.springframework.core.env.Environment;

解决方法

将属性文件放在与application.property相同的位置,然后按照
命名约定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配置文件加载属性文件的相关信息,请在本站寻找。

本文标签: