vendredi 20 novembre 2015

Handling Context In Spring

I'm working on a small project and I'm looking for a good way to handle context in spring. I find myself creating a context holder class to hold my properties using setter injection. The problem I'm having with this is that I'm grabbing a context object and passing it around. I'm looking for a design pattern or something that can help me do this in a cleaner way. As a simple example let's say I'm currently doing something like the below, where the fields are injected through setter injection and I'm looking for a better way to inject the properties Also, pretend I had a large amount of properties, too large to use something like @Value cleanly:

public class MyContext{

    private String configItem1;
    private String configItem2;
    private String configItem3;


    public void setConfigItem1(String configItem1){
        this.configItem1 = configItem1;
    }

    public void setConfigItem2(String configItem2){
        this.configItem2 = configItem1;
    }

    public void setConfigItem3(String configItem3){
        this.configItem3 = configItem1;
    }

}

Sample spring context:

<bean id="appProperties"
       class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:my-app.properties</value>
            </list>
        </property>
    </bean>


<bean id="myContext" class="these.are.not.the.droids.you.are.looking.for.context.MyContext" >
     <property name="configItem1" value="${some.item.1}" />
     <property name="configItem2" value ="${some.item.2}"/>
     <property name="configItem3" value="${some.item.3}" />
</bean>

Aucun commentaire:

Enregistrer un commentaire