1. Purpose: Load the configuration information stored in the database when spring starts, and on the one hand, it is easy to manage
2. Beanpostprocessor is a method provided by spring, which is implemented by implements way
will produce two implementation classes:
@Overridepublic Object Postprocessafterinitialization (Object bean, String beanname) throws beansexception {return bean;} @Overridepublic Object Postprocessbeforeinitialization (Object bean, String beanname) throws Beansexception {return bean ;}
The name and return type are different and can be modified by yourself
3. Explanation:
1) The Beanpostprocessor implementation class is passed when the spring container loads all the configured beans (something similar to the interceptor principle)
2) Object bean: The corresponding Bean's address in memory (which can be strongly converted to the corresponding type) when the entire bean is loaded. String Beanname: The name of the corresponding bean (that is, the ID name in front of Class)
3) Postprocessbeforeinitialization method: Is the operation performed before the bean is loaded. Postprocessafterinitialization method: Is the operation after the bean is loaded
4, the way to load the data is the use of datasource loading completed, connected to the database for data loading. (Tried the sessionfactory way, but for all injection interface error, please big God pointing)
The specific implementation code is for reference only:
Public classProputilImplementsbeanpostprocessor{ Public Staticmap<string, object> map =NewHashmap<string, object>(); @Override PublicObject Postprocessafterinitialization (Object bean, String beanname)throwsbeansexception {if("DataSource". Equals (Beanname)) {DataSource DS=(DataSource) (bean); Connection Conn=NULL;
PreparedStatement pm = null; Try{conn=ds.getconnection (); PM= Conn.preparestatement ("SELECT * FROM Model"); ResultSet RS=Pm.executequery (); while(Rs.next ()) {System.out.println (rs.getstring ("ModelID")); System.out.print (Rs.getstring ("ModelName") ;//This can be done for different data, stored in the corresponding map to do static data loading, convenient for other methods to call}}Catch(SQLException e) {e.printstacktrace (); }finally { Try{Conn.close ();
Pm.close (); } Catch(SQLException e) {e.printstacktrace (); } } } returnBean; } @Override PublicObject Postprocessbeforeinitialization (Object bean, String beanname)throwsbeansexception {returnBean; } }
5. Refresh the data
1) Fixed write a refresh mechanism because of changes in data
2) Add the corresponding mode of operation to the thread, so that only need to start a server, you can implement data and configuration updates
6. Questions:
1) This method will filter all bean loads once, causing the corresponding efficiency startup problem after initializing the startup.
2) I am using the thread to achieve 5 minutes to refresh the static variable update, there is only one load once, the thread does not restart the problem
Application of Spring container initialization data (database) Beanpostprocessor