First, aware related interface
For applications, you should minimize the coupling to the Sping API, but sometimes it is necessary to let the bean know the details of how the spring container manages it, in order to take advantage of some of the features provided by spring, such as letting the bean know that the container is managed by that name. or let the bean know the existence of beanfactory or applicationcontext, that is, the bean can get beanfactory or applicationcontext instances, if the bean can be aware of these objects, Then you can do some actions such as event publishing when some of the bean's actions occur.
1.1. Spring provides some aware interfaces:
Beannameaware Interface: If a bean needs to access the ID attribute of its own bean in the configuration file, the Bean class provides the ability to callback itself, after the dependency is determined, before initializing the method, by implementing the interface. Thus obtaining the id attribute of the bean itself, which provides the void Setbeanname (String name) method implementation, it should be noted that the name parameter of the method is the id attribute of the bean. The Setbeanname method allows the bean to obtain its own ID attribute
Beanfactoryaware Interface: The bean that implements the Beanfactoryaware interface can access the spring container directly through beanfactory, and when the bean is created by the container, There will be an instance reference to the corresponding beanfactory, which has a method void Setbeanfactory (Beanfactory beanfactory) method that creates its beanfactory instance from the parameters of this method, By implementing the Beanfactoryaware interface, the bean has the ability to access the spring container. disadvantage: It is not recommended to cause code to be coupled with spring's API.
Applicationcontextaware Interface: after the Bean class is initialized, it will be injected into the ApplicationContext instance, which has a method, Setapplicationcontext ( ApplicationContext context), using its parameter context to create its ApplicationContext instance, disadvantage: It is not recommended to cause code to be coupled with the spring API.
1.2.Beannameaware interface:
Package Com.pb.entity;import org.springframework.beans.factory.beannameaware;/* * Entity class implements the Init method and Beannameaware interface */ public class Hello implements beannameaware{ @Override public void Setbeanname (String arg0) { System.out.println ("Callback Setbeanname method id attribute is" +arg0); } public void init () { System.out.println ("Executing initialization method init");} }
Applicationcontext.xml
<bean id= "Hello" class= "Com.pb.entity.Hello" init-method= "Init" ></bean>
Test class:
Package Com.pb.demo;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Com.pb.entity.hello;public Class hellotest {public static void Main (string[] args) { ApplicationContext context=new Classpathxmlapplicationcontext ("Applicationcontext.xml"); Hello Hello=context.getbean ("Hello", Hello.class); }}
Results:
Callback Setbeanname method ID property is Hello executing initialization method init
1.3.Beanfactoryaware Interface:
Package Com.pb.entity;import Org.springframework.beans.beansexception;import Org.springframework.beans.factory.beanfactory;import Org.springframework.beans.factory.beanfactoryaware;import org.springframework.beans.factory.beannameaware;/* * Entity classes implement the Init method and the Beannameaware interface */public class Hello implements beannameaware,beanfactoryaware{ private beanfactory BF; @Override public void Setbeanname (String arg0) { System.out.println ("callback Setbeanname method id attribute is" +arg0); } public void init () { System.out.println ("Executing initialization method init"); } /* * Override Setbeanfactory Method * @see org.springframework.beans.factory.beanfactoryaware#setbeanfactory ( org.springframework.beans.factory.BeanFactory) * /@Override public void Setbeanfactory ( Beanfactory arg0) throws beansexception { this.bf=arg0; } Public Beanfactory GETBF () { return bf; }}
The configuration file does not change
Test class:
Package Com.pb.demo;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Com.pb.entity.hello;public Class hellotest {public static void Main (string[] args) { ApplicationContext context=new Classpathxmlapplicationcontext ("Applicationcontext.xml"); Hello Hello=context.getbean ("Hello", hello.class); System.out.println ("Get Beanfactory Object" +HELLO.GETBF ());} }
Results:
The callback Setbeanname method ID property is Hello executing initialization method init gets beanfactory object org.s[email protected]3dc0bb:defining beans [Hello]; Root of Factory hierarchy
1.4.Applicationcontextaware Interface:
Package Com.pb.entity;import Org.springframework.beans.beansexception;import Org.springframework.beans.factory.beanfactory;import Org.springframework.beans.factory.beanfactoryaware;import Org.springframework.beans.factory.beannameaware;import Org.springframework.context.applicationcontext;import org.springframework.context.applicationcontextaware;/* * Entity classes implement the Init method and the Beannameaware interface */public class Hello implements beannameaware,beanfactoryaware,applicationcontextaware{private beanfactory BF; Private ApplicationContext context; @Override public void Setbeanname (String arg0) {System.out.println ("callback Setbeanname Method id attribute is" +arg0); } public void Init () {System.out.println ("Executing initialization method init"); }/* * Override setbeanfactory Method * @see org.springframework.beans.factory.beanfactoryaware#setbeanfactory (org.spri ngframework.beans.factory.BeanFactory) */@Override public void Setbeanfactory (Beanfactory arg0) throws Beansexc eption {this.bf=arg0; } public beanfactory GETBF () {return bf; } @Override public void Setapplicationcontext (ApplicationContext arg0) throws beansexception {thi s.context=arg0; } public ApplicationContext GetContext () {return context; }}
The configuration file does not change
Test class
Package Com.pb.demo;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Com.pb.entity.hello;public Class hellotest {public static void Main (string[] args) { ApplicationContext context=new Classpathxmlapplicationcontext ("Applicationcontext.xml"); Hello Hello=context.getbean ("Hello", hello.class); System.out.println ("Get Beanfactory Object" +HELLO.GETBF ()); System.out.println ("Get ApplicationContext object:" +hello.getcontext ());} }
Results:
The callback Setbeanname method ID property is Hello executing initialization method init gets beanfactory object org.s[email protected]3dc0bb:defining beans [Hello]; Root of factory hierarchy obtained ApplicationContext object: Org[email protected]1d04653:startup date [Wed Apr 00:43:06 CST 2015]; Root of context Hierarchy
Ii. Beanpostprocessor class and Beanfactorypostprocessor
Processing beans in a container
Implementing the Beanpostprocessor Interface Bean Post processor
public Object Postprocessafterinitialization (object arg0, String arg1), operation after Bean initialization
public Object Postprocessbeforeinitialization (object arg0, String arg1), operation before Bean initialization
The first parameter initializes the bean the second argument is the name of the bean instance
Container after the processor is instantiated at the end of the container, the container for additional processing
The Beanfactorypostprocessor interface must be implemented,
public void Postprocessbeanfactory (Configurablelistablebeanfactory arg0)
Spring pinch provides a lot of back-window processors such as:
Propertyplaceholderconfigurer: Property Placeholder Configurator
Propertyoverrideconfigurer: Another property placeholder Configurator
2 kinds of difference in with the later one has the property of covering
2.1,propertyplaceholderconfigurer
Is the spring built-in Propertyedito, which is the implementation class of Beanfactorypostprocess
Function: Read the properties configuration file
The implementation class allows you to configure some of the spring configuration file property values into the properties file, so that the spring configuration file can be changed as long as the file is in the properties.
Use 1. When you modify a section's properties, you do not need to open the spring configuration file to ensure that new errors are not introduced into spring's propertyplaceholderconfigure configuration file.
Pros: You can detach some of the configuration information from the main XML configuration file,
Multiple profiles can be supported, and profiles can be split into multiple profiles, reducing the risk of modifying configuration files.
2.2,propertyoverrideconfigurer
Overwrites the configuration information in the XML file with the configuration in the. Properties property file as the primary
if not configured Propertyoverrideconfigurer uses the configuration information in XML
Attribute format:
Beanname.property=value
Where Beanname is the Springxml configuration convenient for the Bean ID property in the vicinity, value is the values of the property, for multiple properties files, through the Locations property to specify
Third, custom attribute editor
Application Scenarios:
Type not recognized, such as date, etc.
Realize
Inherit PropertyEditorSupport
Overriding the Setastext () method
Package Com.pb.entity;import Java.util.date;public class Appdate { private date date; Public Date getDate () { return date; } public void SetDate (date date) { this.date = date; } }
Custom Editor
Package Com.pb.entity;import Java.beans.propertyeditorsupport;import Java.text.parseexception;import Java.text.simpledateformat;public class Customerproperty extends PropertyEditorSupport { private String format; @Override public void Setastext (String text) throws IllegalArgumentException { SimpleDateFormat sdf=new SimpleDateFormat (format); Super.setastext (text); try { //Convert object, can SetValue method re-assign Value This.setvalue (sdf.parse (text)); } catch (ParseException e) { E.printstacktrace (); } } Public String GetFormat () { return format; } public void SetFormat (String format) { This.format = format; } }
Applicationcontext.xml
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.1.xsd "> <!--defines a bean's class as a custom editor--<bean class=" Org.springframework.beans.factory.config.CustomEditorConfigurer > <!--Properties--<property name= cu Stomeditors "> <!--Map--<map> <!--key is date-- <entry key= "Java.util.Date" > <!--Configure the value of map and <bean class= "COM.PB. Entity. Customerproperty "> <!--Configuration Properties--<property name=" format "value=" Yyyy-mm-dd "></property> </bean> </entry> </map> </property> </bean> <!--configuring appdate bean--><bean id= "appdate" class= "Co M.pb.entity.appdate "><property name=" date "><value>2014-4-8</value></property></ Bean></beans>
Test class:
Package Com.pb.demo;import Java.text.simpledateformat;import Org.springframework.context.ApplicationContext; Import Org.springframework.context.support.classpathxmlapplicationcontext;import Com.pb.entity.appdate;public Class Demo { /** * @param args * /public static void main (string[] args) { ApplicationContext Context=new classpathxmlapplicationcontext ("Applicationcontext.xml"); Appdate Appdate=context.getbean ("Appdate", appdate.class); SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy-mm-dd"); System.out.println (Sdf.format (Appdate.getdate ()));} }
Spring (c) Bean continue to get started