Getting started 08 - Do not use XML definition file
The class format of the XML file is very suitable for configuration settings, and therefore almost all open source projects use XML as a preset configuration definition, but it is often provided by non-XML definition files, like attribute files .properties Spring also allows you to use the property file definition bean:
Hellobean.class = Onlyfun.caterpillar.Hellobean
Hellobean.Helloword = Hello! Justin!
The Hellobean name is the alias of the bean. Class is used to specify the category source, and the other properties are like. Helloword, the name of the setter, we can use org.springframework.beans.Factory.support.propertiesBeandefinitionReader to read the property file, one example as follows:
Springtest.java
Package online.
Import org.springframework.beans.factory.support.beandefinitionregistry;
Import org.springframework.beans.Factory.support.defaultListableBeanfactory;
Import org.springframework.beans.Factory.support.propertiesbeandefinitionReader;
Import org.springframework.beans.Factory.BeanFactory;
Import org.springframework.core.io.classpathResource;
Public class springtest {
Public static void main (String [] args) {
Beandefinitionregistry reg = new defaultListableBeanFactory ();
PropertiesBeandefinitionReader Reader = New PropertiesBeandefinitionReader (REG);
Reader.LoadBeandefinitions ("Bean.Properties");
Beanfactory factory = (beanfactory) reg;
Hellobean Hello = (Hellobean) Factory.getBean ("Hellobean");
System.out.println (Hello.getHelloword ());
}
}
In addition to using the XML or property file, you can also program the properties directly in the program, set the property with the category of the bean to org.springframework.beans.Factory.Support.RootBeandefinition, And register to org.springframework.beans.factory.support.beandefinitionRegistry, not using any file is the advantage, the client is isolated from the definition file, they cannot touch the contents of the definition, directly to see an example:
Springtest.java
Package online.
Import org.springframework.beans.Factory.support.beandefinitionregistry; import org.springframework.beans.factory.support.defaultListableBeanfactory;
Import org.springframework.beans.factory.support.rootbeandefinition;
Import org.springframework.beans.Factory.BeanFactory;
Import org.springframework.beans.mutablePropertyValues;
Public class springtest {
Public static void main (String [] args) {
// Set the properties
MutablePropertyValues Properties = New MutablePropertyValues ();
Properties.addpropertyValue ("Hello! Justin!");
/ / Set bean definition
Rootbeandefinition definition = new rootbeandefinition (Hellobean.class, Properties);
// Register bean definition with bean alias
Beandefinitionregistry reg = new defaultListableBeanFactory ();
Reg.registerBeandefinition ("Hellobean", Definition;
Beanfactory factory = (beanfactory) reg;
Hellobean Hello = (Hellobean) Factory.getBean ("Hellobean");
System.out.println (Hello.getHelloword ());
}
}
As long as there is Spring-Core.jar, Commons-Logging.jar can operate with the above programs and do not require any other files.