Initializingbean,factorybean detailed

Source: Internet
Author: User

Reference article:

http://tianya23.blog.51cto.com/1081650/660273/

http://layznet.iteye.com/blog/906619

Initializingbean


When spring initializes the bean

If the bean implements the Initializingbean interface, the Afterpropertiesset method is called automatically.


If the bean is implemented with the Initializingbean interface, and Init-method is specified in the configuration file, the system calls the Afterpropertiesset method first and then the method specified in Init-method.


code example

package com.chinaseacom.store.common;import java.text.simpledateformat;import  java.util.arraylist;import java.util.list;import org.apache.commons.lang3.stringutils;import  org.springframework.beans.factory.initializingbean;import  org.springframework.beans.factory.annotation.autowired;import org.springframework.stereotype.repository; import redis.clients.jedis.jedispoolconfig;import redis.clients.jedis.jedisshardinfo;import  redis.clients.jedis.shardedjedis;import redis.clients.jedis.shardedjedispool; @Repositorypublic  class  IDGenerator  implements  InitializingBean{ private ShardedJedisPool  Shardedjedispool; simpledateformat yymmdd=new simpledateformat ("YYMMddHHMMSS");  @Autowired  private IDGeneratorPoolConfig idgeneratorPoolConfig; public<T extends  BASEOBJECT&GT;&NBSP;LONG&NBSP;INCR (Class<t> entityclass)  {  long result = null;  shardedjedis sjedis = null;  try {    result = shardedjedispool.getresource (). INCR (Entityclass.getname ());   }  catch  (exception e)  {   e.printstacktrace ();  }finally{    shardedjedispool.returnresourceobject (Sjedis);   }    if (" Com.chinaseacom.store.order.model.Orders ". EndsWith (Entityclass.getname ())) {   result =new  long ( yymmdd.format ( system.currenttimemillis ())  +   ( result%100000)/10000  +  ( result%10000)/1000 +   (result%1000)/100 +    (result%100 )/10 +   (result%10)/1   );   }  return result; }   public void afterpropertiesset ()  throws Exception {  if  ( Shardedjedispool == null)  {   jedispoolconfig config = new jedispoolconfig ();//      if  (Stringutils.isnumeric (idgeneratorpoolconfig.getmaxtotal))  {     config.setmaxtotal (New integer (Idgeneratorpoolconfig.getmaxtotal ()));//     }   if  (Stringutils.isnumeric (idgeneratorpoolconfig.getmaxidle))  {     config.setmaxidle (New integer (Idgeneratorpoolconfig.getmaxidle ()));//       }   if  (Stringutils.isnumeric (idgeneratorpoolconfig.getminidle))   {    config.setminidle (New integer (Idgeneratorpoolconfig.getminidle ()));//      }   if  (Stringutils.isnumeric ( Idgeneratorpoolconfig.getmaxwaitmillis ())  {    config.setmaxwaitmillis (new  Integer (Idgeneratorpoolconfig      .getMaxwaitmillis ()));     }   config.settestonborrow (true);//When Borrow a Jedis instance, Whether the validate operation is performed in advance, and if true, the resulting Jedis instance is available;    list<jedisshardinfo> jdsinfolist =  new ArrayList<JedisShardInfo> (;   for ) (idgeneratorhostconfig  Mhonstconf : idgeneratorpoolconfig     .getidgeneratorhostconfig ())  {     jedisshardinfo info = new jedisshardinfo (Mhonstconf.getHost (),       mhonstconf.getport ());     if (StringUtils.isNotEmpty ( Mhonstconf.getpassword ())) {     info.setpassword (Mhonstconf.getpassword ());     }    info.setconnectiontimeout (1000000);     Info.setsotimeout (2000000);     jdsinfolist.add (info);   }       shardedjedispool&Nbsp;= new shardedjedispool (config, jdsinfolist);  }   }   Public static void main (String[] arg) {  simpledateformat yymmdd=new  SimpleDateFormat ("Yymmddhhmmss");   long result=123456789l;  system.out.println (new  long ( yymmdd.format ( system.currenttimemillis ())  + ( result%100000)/10000 +   ( result%10000)/1000 +   (result%1000)/100 +    (result%100)/10  +   (result%10)/1   );   system.out.println (New long (  Yymmdd.format ( system.currenttimemillis ()));   system.out.println (Integer.max_value);   system.out.println (long.max_value);  }}


This article is from the "Nothing-skywalker" blog, please be sure to keep this source http://tianxingzhe.blog.51cto.com/3390077/1684303

Initializingbean,factorybean detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.