参考文章:
http://tianya23.blog.51cto.com/1081650/660273/
http://layznet.iteye.com/blog/906619
InitializingBean
spring初始化bean的时候
如果bean实现了InitializingBean接口,会自动调用afterPropertiesSet方法。
如果该bean是实现了InitializingBean接口,并且同时在配置文件中指定了init-method,系统则是先调用afterPropertiesSet方法,然后在调用init-method中指定的方法。
代码示例
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; @Repository public class IDGenerator implements InitializingBean{ private ShardedJedisPool shardedJedisPool; SimpleDateFormat yymmdd=new SimpleDateFormat("YYMMddHHMMSS"); @Autowired private IDGeneratorPoolConfig idgeneratorPoolConfig; public<T extends BaseObject> Long 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);//在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的; 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 = 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); } }
时间: 2024-10-13 21:54:37