Using SpEL is easy for externalise the value. This simple example demonstrate how to use spring expression language.
Create a POJO MySetting
@Component("mySetting") public class MySetting { private String username = "admin"; private String password = "p@ssword1"; private String database = "test"; private String driver = "com.mysql.driver"; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getDatabase() { return database; } public void setDatabase(String database) { this.database = database; } public String getDriver() { return driver; } public void setDriver(String driver) { this.driver = driver; } }
Create a service class – MyDatabase
@Service("myDatabase") public class MyDatabase { @Value("#{mySetting.username}") private String username; @Value("#{mySetting.password}") private String password; @Value("#{mySetting.database}") private String database; @Value("#{mySetting.driver}") private String driver; private void print(){ System.out.println(this.username); System.out.println(this.password); System.out.println(this.database); System.out.println(this.driver); } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getDatabase() { return database; } public void setDatabase(String database) { this.database = database; } public String getDriver() { return driver; } public void setDriver(String driver) { this.driver = driver; }
Create a client file to run
package com.eweol.example; import org.springframework.context.support.GenericXmlApplicationContext; public class MyClient { public static void main(String[] args) { GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(); ctx.load("classpath:/applicationcontext.xml"); ctx.refresh(); MyDatabase db = ctx.getBean(MyDatabase.class); db.print(); } }
applicationContext.xml
First scan codebase for dependency requirement, then scan the code for injectable bean under the package.
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <context:annotation-config /> <context:component-scan base-package="com.eweol.example"/> </beans>
Using Spring Expression Language with value