1. I've a Maven project.
2. I've created a properties file i.e. message.properties under resources folder.
3. My project structure snapshot is as follows:
4. Contents of message.properties:
5. base-package entry of servlet-context.xml is as follows: [All config files are here]
<context:component-scan base-package="com.controller" />
6. I'll use @PropertySource and @Value annotation.
2. I've created a properties file i.e. message.properties under resources folder.
3. My project structure snapshot is as follows:
4. Contents of message.properties:
index.page=home
salutation.page=welcome
salutation.text=Welcome to Spring MVC framework
name=MY NAME
salutation.page=welcome
salutation.text=Welcome to Spring MVC framework
name=MY NAME
5. base-package entry of servlet-context.xml is as follows: [All config files are here]
<context:component-scan base-package="com.controller" />
6. I'll use @PropertySource and @Value annotation.
7. I'll create a class with @PropertySource annotaion and a bean returning PropertySourcesPlaceholderConfigurer. This step is mandatory to retrieve values from properties file using @Value annotation.
package com.controller.utility;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
@Configuration
@PropertySource("classpath:message.properties")
public class MessageUtil {
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
@Configuration
@PropertySource("classpath:message.properties")
public class MessageUtil {
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}
}
8. Now values are available using @Value annotation from any class.
@Value("${index.page}")
public String INDEX;
@Value("${salutation.page}")
public String SALUTATION;
@Value("${salutation.text}")
public String SALUTATION_TEXT;
@Value("${name}")
public String name;
public String INDEX;
@Value("${salutation.page}")
public String SALUTATION;
@Value("${salutation.text}")
public String SALUTATION_TEXT;
@Value("${name}")
public String name;
9. I've used these values in a controller class for request mapping.
package com.controller.mapping;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class HomeController {
@Value("${index.page}")
public String INDEX;
@Value("${salutation.page}")
public String SALUTATION;
@Value("${salutation.text}")
public String SALUTATION_TEXT;
@Value("${name}")
public String name;
@RequestMapping(value={"/welcome", "/index"}, method={RequestMethod.GET, RequestMethod.POST})
public String salutation(Model model) {
model.addAttribute("text", SALUTATION_TEXT);
model.addAttribute("name", name);
return SALUTATION;
}
}
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class HomeController {
@Value("${index.page}")
public String INDEX;
@Value("${salutation.page}")
public String SALUTATION;
@Value("${salutation.text}")
public String SALUTATION_TEXT;
@Value("${name}")
public String name;
@RequestMapping(value={"/welcome", "/index"}, method={RequestMethod.GET, RequestMethod.POST})
public String salutation(Model model) {
model.addAttribute("text", SALUTATION_TEXT);
model.addAttribute("name", name);
return SALUTATION;
}
}
No comments:
Post a Comment