Está en la página 1de 21

Properties in Java and

Spring

By Martin Nad
martin_m_nad@yahoo.com
Properties in Java and Spring

OVERVIEW

This documentation show to how you can properties in java


(pojo) and Spring
We are using java, and we are developer. We should know
how we can use different methods, and tools in different
enviroment.
Here I will show you that two the different kind of properties
that uses in Java and if we use the Spring we should Springs’
frame works if we want to use the properties.
Certently there is some cases that we should use pojo
methods even in Spring if we want to use properties.

Page 2
Properties in Java and Spring

Table of context:

WHAT IS .PROPERTIES
WHY AND WHEN TO USE
PROPERTIES FORMATES IN JAVA
PROPERTIES FORMAT IN PLAIN-TEXT
EXAMPLE XML-BASED
SINGLETON
SINGLETON OR NOT
SPRING PROPERTIES-BEAN
SPRING PROPERTIES

MY OTHER DOCUMENTATION
XML-BASED PROPETIES

Page 3
Properties in Java and Spring

Page 4
Properties in Java and Spring

Properties in Java and


Spring
WHAT IS .PROPERTIES

“Properties are a file extension for files mainly used in Java


related technologies to store the configurable parameters of
an application. “
If we have constant-globles variables, that we need to use in
our application.
And it should much better to have those variables outside of
the source cods and define in seperate file, text file, xml file,
or in case of Spring use Spring bean-application-context to
have them.
If we want change those constant-variables, we don’t need to
go to the source code and change there and complie it, and
make package and put it on the server. You just modify the
xml, file, application-context, and when you updated it, your
application want to read the new values.
Unfortuently if you are using J2EE, and using for example
Spring, you should make a new package and restart your
application. But advantages here, is: just you don’t need to
modify your source.

Page 5
Properties in Java and Spring

WHY AND WHEN TO USE

You define your constant-variables in a text file, or xml file, i


prefer to use xml, it should be very nicer than plane test file.
Maybe it is because i use xml in very early already from 2000
and i will recomend to use xml-formate too. And it can also be
used for storing strings for localization;
these are known as Property Resource Bundles.

PROPERTIES FORMATES IN JAVA(POJO)


POJO stands for Pland Old Java Object.
Each parameter is stored as a pair of strings, one storing the
name of the parameter (called the key), and the other storing
the value.

.PROPERTIES FORMAT IN PLAIN-TEXT

Each line in a .properties file normally stores a single property.

key=value
Several formats are possible for each line, including
key=value, key = value, key:value, and key value.
number sign (#) or the exclamation mark (!) as the first non
blank character in a line to denote that all text following it is a
comment.

Page 6
Properties in Java and Spring

The backwards slash is used to escape a character

EXAMPLE AS PLAIN-TEXT

a = a string
b = a string with escape sequences \t \n \r \\ \" \' \ (space) \u0123
c = a string with a continuation line \ continuation line
d.e.f = another string

EXAMPLE CONT.

Properties properties = new Properties();


try { properties.load(new FileInputStream("filename.properties")); }
catch (IOException e) { } // Write properties file.
try { properties.store(new FileOutputStream("filename.properties"),
null); }
catch (IOException e) { }
Plain text example 2
import java.util.*;
import java.io.*;
public class LoadSample {
public static void main(String args[]) throws Exception {
prop = new Properties();
FileInputStream fis = new FileInputStream("sample.properties");
prop.load(fis);
prop.list(System.out);

Page 7
Properties in Java and Spring

System.out.println("\nThe foo property: " + prop.getProperty("foo"));


}
}

Page 8
Properties in Java and Spring

XML-BASED PROPETIES

Xml is very nice formate to define any document.


As usual begin with

<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE properties SYSTEM
"http://java.sun.com/dtd/properties.dtd">

And follows by
<properties>

To comments
<comment>Hi</comment>

To define properties use of


<entry key="foo">bar</entry>

Page 9
Properties in Java and Spring

EXAMPLE XML-BASED

<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE properties SYSTEM
"http://java.sun.com/dtd/properties.dtd">
<properties> <comment>Hi</comment>
<entry key="foo">bar</entry>
<entry key="fu">baz</entry>
</properties>

AND HERE THE JAVA CODE:

import java.util.*;
import java.io.*;
public class LoadSampleXML {
public static void main(String args[]) throws Exception {
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("sampleprops.xml");
prop.loadFromXML(fis);
prop.list(System.out);
System.out.println("\nThe foo property: " + prop.getProperty("foo"));
}
}

Page 10
Properties in Java and Spring

SINGLETON

The Singleton pattern is effective for limiting the maximum


number of instances of a class to exactly one.
In this case, if more than one object needs to use an instance
of the Singleton class, those objects share the same Singleton
class instance.
A class that implements the Singleton pattern is called a
Singleton class.

SINGLETON OR NOT

Not necessary to have singleton on Java-property file.


You should have singletone on some class that you shouldn’t
have more than one instance of it, as Calender. You should
have just one calender instance in your application.
If you are using the Spring, in spring every classes are
singleton by default.

Page 11
Properties in Java and Spring

SPRING PROPERTIES

In spring you need think in some different way than POJO.


You should have a bean class for all element you need. And
define them in your xml-baserade (main xml-file, you can
rename it to whatever you like)application-context.xml
It doesn’t neccessery to put all in application-context.xml
You can write your own xml as properties.xml and just import
it into the application-context.xml

<bean id="serviceController" class="org.martin.ServiceController">


<property name="statusProperties" ref="statusProperties" />
<property name="statisticDAO" ref="statisticDAO"/>
</bean>

If you like this documentation and it was


helpful you can just donate 7$ by using this
https://www.paypal.com/cgi-bin/webscr?cmd=_s-
or use this to put
xclick&hosted_button_id=5814019
any amont you like to donate
https://www.paypal.com/cgi-bin/webscr?cmd=_s-
xclick&hosted_button_id=5813954 and if it doesn’t work copy
the link and put it on your browser.

if it doesn’t work copy the link and put it on your browser.

Page 12
Properties in Java and Spring

SPRING PROPERTIES-BEAN

/**
*
*/
package org.martin.beans;

/**
* @author mn
*
*/
public class StatusPropertiesBean {

private String CMD_IS_NULL;


private String CMD_IS_OK;

private String getModels_problem;


private String msisdn_missing;
private String venders_missing;
private String vendors_problem;

private String model_missing;

Spring properties-bean
private String startpage_problem;
private String settingname_missing;

Page 13
Properties in Java and Spring

private String originator_missing;


private String msisdn_problem;
private String callresult_missing;
private String callresult_problem;
private String unkown_command;
private String country_missing;

//and your geter and seter

public String getCMD_IS_NULL() {


return CMD_IS_NULL;
}
public void setCMD_IS_NULL(String cmd_is_null) {
CMD_IS_NULL = cmd_is_null;
}
public String getCMD_IS_OK() {
return CMD_IS_OK;
}
public void setCMD_IS_OK(String cmd_is_ok) {
CMD_IS_OK = cmd_is_ok;
}

public String getGetModels_problem() {


return getModels_problem;
}
public void setGetModels_problem(String getModels_problem) {
this.getModels_problem = getModels_problem;
}
Spring properties-bean
public String getMsisdn_missing() {
return msisdn_missing;

Page 14
Properties in Java and Spring

}
public void setMsisdn_missing(String msisdn_missing) {
this.msisdn_missing = msisdn_missing;
}
public String getVenders_missing() {
return venders_missing;
}
public void setVenders_missing(String venders_missing) {
this.venders_missing = venders_missing;
}
public String getModel_missing() {
return model_missing;
}
public void setModel_missing(String model_missing) {
this.model_missing = model_missing;
}
public String getStartpage_problem() {
return startpage_problem;
}
public void setStartpage_problem(String startpage_problem) {
this.startpage_problem = startpage_problem;
}
public String getSettingname_missing() {
return settingname_missing;
}
public void setSettingname_missing(String settingname_missing) {
this.settingname_missing = settingname_missing;
}
public String getOriginator_missing() {
return originator_missing;
}
public void setOriginator_missing(String originator_missing) {

Page 15
Properties in Java and Spring

this.originator_missing = originator_missing;
}
public String getMsisdn_problem() {
return msisdn_problem;
}
Spring properties-bean
public void setMsisdn_problem(String msisdn_problem) {
this.msisdn_problem = msisdn_problem;
}
public String getCallresult_missing() {
return callresult_missing;
}
public void setCallresult_missing(String callresult_missing) {
this.callresult_missing = callresult_missing;
}
public String getCallresult_problem() {
return callresult_problem;
}
public void setCallresult_problem(String callresult_problem) {
this.callresult_problem = callresult_problem;
}
public String getUnkown_command() {
return unkown_command;
}
public void setUnkown_command(String unkown_command) {
unkown_command = unkown_command;
}
public void setCountry_missing(String country_missing) {
this.country_missing = country_missing;
}
public String getCountry_missing() {
return country_missing;

Page 16
Properties in Java and Spring

}
public void setVendors_problem(String vendors_problem) {
vendors_problem = vendors_problem;
}
public String getVendors_problem() {
return vendors_problem;
}

}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public Properties getProperties() {
return properties;
}

SPRING PROPERTIES

Page 17
Properties in Java and Spring

In this way you define your bean in the application-


context.xml

<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="statusProperties"
class="dk.unwire.projects.ota.beans.StatusPropertiesBean">
<property name="CMD_IS_OK">
<value>1000</value>
</property>
<property name="callresult_missing">
<value>2000</value>
</property>
<property name="vendors_problem">
<value>3001</value>
</property>
<property name="getModels_problem">
<value>3002</value>
</property>
<property name="callresult_problem">
<value>3003</value>
Spring properties
</property>
<property name="unkown_command">
<value>4000</value>
</property>
<property name="CMD_IS_NULL">
<value>4000</value>

Page 18
Properties in Java and Spring

</property>
<property name="venders_missing">
<value>4203</value>
</property>
<property name="model_missing">
<value>4205</value>
</property>
<property name="startpage_problem">
<value>4206</value>
</property>
<property name="msisdn_missing">
<value>4207</value>
</property>
<!--
<property
name="venders_problem"><value>4203</value></property>
should look at here too
-->
<property name="settingname_missing">
<value>4208</value>
</property>
<property name="originator_missing">
<value>4209</value>
</property>
<property name="msisdn_problem">
<value>4303</value>
</property>

<!-- <property name=""><value></value></property>-->


<!-- <property name=""><value></value></property>-->
<!-- <property name="">
<value></value>

Page 19
Properties in Java and Spring

</property>-->
</bean>
</beans>

And you are done, so simple.

DONATION

For donation you can just donate 7$ by using


this
https://www.paypal.com/cgi-bin/webscr?cmd=_s-
or use this to put
xclick&hosted_button_id=5814019
any amont you like to donate
https://www.paypal.com/cgi-bin/webscr?cmd=_s-
xclick&hosted_button_id=5813954 and if it doesn’t work copy
the link and put it on your browser.

if it doesn’t work copy the link and put it on your browser.

Page 20
Properties in Java and Spring

MY OTHER DOCUMENTATION

Properties in Java and Spring by Martin Nad


Spring and Cxf by Example
Jboss and Perm Gen Error
Using Ftp Service With Spring
JunIt
How to use Maven
ReFactoring
Maven and Custome Archetype
How to Write Effective Code

Page 21

También podría gustarte