Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Application Properties in Spring-MVC

New to Spring-MVC.

I want to store two properties (uploadFolder=.., downloadFolder=..) in a .properties file and access it in HomeController class (automatically created by the MVC template).

Can you please guide me how..

1) Created an app.properties file with the above and placed it under /src/main/resources. is this correct or should it go under /webapp/resources?

2) Placed a bean in servlet-context.xml in the following manner. is this correct?

<beans:bean id="messageSource"
   class="org.springframework.context.support.ResourceBundleMessageSource">
   <beans:property name="basename" value="app" />
</beans:bean>

3) Now how do I access this in Java controllers?

4) How do I access these in a JSP?

I can't tell how much I will thank you.

like image 973
srini.venigalla Avatar asked Apr 06 '12 14:04

srini.venigalla


People also ask

Where is application properties in spring MVC?

Spring Boot Framework comes has a built-in mechanism for application configuration using a file called the 'application. properties'. It is located in the src/main/resources folder, as shown below in the following figure. The Spring Boot framework provides various properties that can be configured in the 'application.

How do I view spring application properties?

Another way to read application properties in the Spring Boot application is to use the @ConfigurationProperties annotation. To do that, we will need to create a Plain Old Java Object where each class field matches the name of the key in a property file.

How read properties file in spring MVC?

Spring framework gives us two annotation @PropertySource and @Value which make the reading properties file values super easy. @PropertySource annotation is used to define the properties file location and @Value annotation is used to inject the properties file values into the bean.

How do I get application properties in spring boot?

Spring Boot Framework comes with a built-in mechanism for application configuration using a file called application. properties. It is located inside the src/main/resources folder, as shown in the following figure. Spring Boot provides various properties that can be configured in the application.


1 Answers

There are a few different ways to do it. I do the following. In app context:

<util:properties id="myProps" location="classpath:app.properties" />

Make sure you have the following at the top of your file to include the "util" namespace:

xmlns:util="http://www.springframework.org/schema/util" 
xsi:schemaLocation= "... http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"

I usually put my properties files in src/main/resources. As long as they're on the classpath, you're good. Then, in your controller, you can annotate your field/method with:

@Controller
public class MyController {

    @Value("#{myProps['uploadFolder']}")
    private String uploadFolder

    @Value("#{myProps['downloadFolder']}")
    private String downloadFolder

    @RequestMapping("myPage")
    public String loadPage(ModelMap m) {
        m.addAttribute("uploadFolder", uploadFolder);
        m.addAttribute("downloadFolder", downloadFolder);
        return "myPage";
    }

    public void setUploadFolder(String uploadFolder) {
        this.uploadFolder = uploadFolder;
    }
    public void setDownloadFolder(String downloadFolder) {
        this.downloadFolder = downloadFolder;
    }
}

Then, in your JSP:

Download folder: ${downloadFolder} <br/>
Upload folder: ${uploadFolder}

HTH. Let me know if you have any questions.

like image 182
stephen.hanson Avatar answered Sep 25 '22 00:09

stephen.hanson