First, Spring Foundation
1, core
(1) IOC / DepENDENCY INJECTION
l IOC / Dependency Injection: Beans does not depend on the framework; container is injectable
l Lightweight Spring Container: Configuration and Management Beans
(2) BeanFactory
l Lightweight bean container
l Load bean definition, including:
? Id / name
Class
Singleton or prototype
? Attributes
? Construct parameters
? Initialization method
? Destructure method
(3) XMLBeanFactory
l beanfactory implementation
l Beans definition:
beans>
l Use example:
InputStream INPUT = New FileInputStream ("Beans.xml");
Beanfactory factory = new xmlbeanfactory (input);
EXAMPLEBEAN EB = (Examplebean) Factory.getBean ("Examplebean");
EXAMPLEBEANTWO EB2 = (ExampleBeantwo) Factory.getBean ("Anotherexample");
May throw NosuchBeandefinitionException;
ExampleBean EB = (Examplebean) Factory.getBean ("EXAMPLEBEAN", ExampleBebean.class);
BEANNNOTOFREQUIREDTYPEEXCEPTION may throw
(4) Bean collaborators
l Other Beans required for your bean work
Package eg;
Public class examplebean {
PRIVATE ANOTHERBEAN Beanone;
Private Yetanotherbean Beantwo;
Public void setBeanone (anotherbean b) {beanone = b;}
Public void setBeantwo (YETANOTHERBEAN B) {beantwo = b;}
}
(5) Bean property
l Set the bean property
Package eg;
Public class examplebean {
PRIVATE STRING S;
Private INT i;
Public void setstringproperty (string s) {this.s = s;}
Public void setintegerProperty (int i) {this.i = i;}
}
Java.awt.color is initialized with RGB value
(8) Spring attribute editor
l example:
Property>
Property>
Property>
Property>
(9) Customization attribute editor
l Date example:
DateFormat FMT = New SimpleDateFormat ("D / M / YYYY");
CustomDateEditor DateEditor = New CustomDateEditor (FMT, FALSE);
Beanfactory.register (java.util.date.class, dateeditor);
l StringTrimming Example: Trim string, converted a null value
StringTrimmereditor Trimmer = new stringtrimmereditor (true);
Beanfactory.registercustomeditor (java.lang.string.class, trimmer);
(10) java.util.properties
l Properties example:
FOO = 1
Bar = 2
Baz = 3
value>
Property>
prOPs> Property>
(11) java.util.list
l List:
list>
Property>
(12) java.util.set
l Set:
set>
Property>
(13) java.util.map
l Map example:
entry>
entry>
map>
Property>
(14) Construction method
l Example of the construction method:
Public class examplebean {
PRIVATE ANOTHERBEAN Beanone;
Private Yetanotherbean Beantwo;
Private INT i;
Public ExampleBean (Anotherbean B1, YetAnotherbean B2, INT I) {
THIS.Beanone = B1;
This.beantwo = b2;
THIS.I = I;
}
}
bean>
l Beans can be initialized by Factory when used for the first time.
Public class examplebean {
Public void init () {
// Do some initialization work
}
}
l Beans can be cleared when it is no longer used
Public class examplebean {
Public void cleanup () {
// do some destruction work
}
}
(16) PropertyPlaceHolderConfigurer
l Input from the external Properties file
Class = "org.apache.commons.dbcp.basicdataasource" Destroy-method = "close"> Property> bean> JDBC.Properties Jdbc.driverclassname = org.hsqldb.jdbcdriver JDBC.URL = JDBC: HSQLDB: HSQL: // Production: 9002 JDBC.USERNAME = SA Jdbc.password = root l Install Configurer InputStream INPUT = New FileInputStream ("Beans.xml"); Xmlbeanfactory factory = new xmlbeanfactory (Input); Properties PROPS = New Properties (); ProPs.Load (New FileInputStream ("JDBC.Properties"); PropertyPlaceHolderConfigurer cfg = new profmentholderconfigurer (); Cfg.SetProperties (PrOPS); Cfg.PostProcessBeanFactory (Factory); DataSource DS = (Datasource) Factory.getBean ("Datasource"); (17) MethodInvokingFactoryBean l Exposure to use Singleton mode Bean Package eg; Public class mysingleton { Private static mysingleton instance = new mysingleton (); Private mysingleton () {} Public static mysingleton getInstance () { Return Instance; } } Class = "... beans.factory.config.MethodInvokingFactoryBean"> Property> bean> FactoryBean creates beans on behalf of another class (18) FactoryBean reference l Get FactoryBean's own reference: MYSINGLETON SINGLETON = (MySingleton) CTX.getBean ("MySingleton"); Get Bean created by Factory FactoryBean Factory = (FactoryBean) CTX.getBean ("& mysingleton"); Get Factory (19) Advanced features l single case / prototype l Autowiring: For Type, require each type of type with a single instance; for Name, the bean name (for non-simple attributes) of each attribute name is required. l depending on the inspection l beanwrapper l INITIALIZINGBEAN / DISPOSABLEBEAN interface l beanfactoryaware / beannameaware interface