Code Snippet

Just another Code Snippet site

[Spring] Access properties file from java code

Properties file :

## boolean param
sendAttachment=false

## String param
senderName=Joe Black

Java code :

@Component
public class MailConfig {

    @Value("#{mailProperties['sendAttachment']}")
    private boolean sendAttachment;

    @Value("#{mailProperties['senderName']}")
    private String senderName;
}

Spring config :

    <util:properties id="mailProperties" location="classpath:/config/mail.properties" />

    <!-- Optional : Scan java package to allow Autowired usage-->
    <context:component-scan base-package="com.eryos.lapoboco.config">
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Component"/>
    </context:component-scan>

Java usage :

public class SendMail {
    @Autowired
    private MailConfig config;

    public void test() {
        String name = config.getSenderName();
    }
}

,


Comments are currently closed.