How to read properties file in Spring.

13 Comments



If you need to read properties file in your Spring application all you need is to configure a PropertyPlaceholderConfigurer bean in your application context.
Following example shows how to read property values from a properties file named config.properties. This file needs to be in your classpath so Spring can find it.

Let’s begin by creating a simple Java class that will use the properties values from the file.

package com.zparacha.spring.examples.config;

public class CreateUser {
	//Spring will populate these fields through Dependency Injection.
        private String name;
	private String email;
	private String URL;

	public CreateUser(){}
	public String getName() {
	  return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getURL() {
		return URL;
	}

	public void setURL(String url) {
		URL = url;
	}
	public void displayUser() {
		System.out.println("Name=" + this.name);
		System.out.println("Email="+ this.email);
		System.out.println("URL=" + this.URL);
	}
}

More

article clipper vert How to read properties file in Spring.
 

How to configure multiple handlers in a Spring MVC web application

4 Comments


In Spring MVC, DispatcherServlet relies on handler mapping to determine which controller the request should be sent to. All handler mapping classes in Spring implement org.springframework.web.servlet.HandlerMapping interface. Spring distribution contains following four implementation of HandlerMapping interface.

  • BeanNameUrlHandlerMapping
  • SimpleUrlHandlerMapping
  • ControllerClassNameHandlerMappign
  • CommonsPathMapHandlerMapping

BeanNameUrlHandlerMapping is the simplest of all and DispatcherServlet looks for this mapping by default.

You can use any one of these handler mappings in your application by just configuring a bean in your application context file. For e.g; to use BeanNameUrlHandlerMapping you will have a bean declaration similar to



But can you use more than one handler mappings in an application? More

article clipper vert How to configure multiple handlers in a Spring MVC web application