`
确实比较男
  • 浏览: 112422 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

J2EE开发框架搭建(5) - Java项目开发常用工具类

阅读更多

工具类下项目中的目录位置:

1. 中文转化成拼音、首字母 ,ChineseCharToPinYin,使用这个类的时候必须要加入pinyin.jar,pinyin.jar已经放到hqhop-framework-web项目的lib目录中;

使用方式:

ChineseCharToPinYin只提供了两个方法:

public static String getPinYin(String src) {.....} 将汉字转换为全拼

public static String getPinYinHeadChar(String str){.......} 提取每个汉字的首字母

2.类型转换辅助工具类TypeCaseHelper ,提供了常用类型之间的相互转化

3. 在Java包security下面的类都是常用的加密的工具类

4. CloseUtil主要是用来关闭一些连接的工具类:

 

public class CloseUtil {
	private static final Log log = LogFactory.getLog(CloseUtil.class);
	/**
	 * 关闭给定的输入流. <BR>
	 * 
	 * @param inStream
	 */
	public static void close(InputStream inStream) {
		if (inStream != null) {
			try {
				inStream.close();
			} catch (IOException e) {
				log.error("error on close the inputstream.", e);
			}
		}
	}
	/**
	 * 关闭给定的输出流. <BR>
	 * 
	 * @param outStream
	 */
	public static void close(OutputStream outStream) {
		if (outStream != null) {
			try {
				outStream.close();
			} catch (IOException e) {
				log.error("error on close the outputstream.", e);
			}
		}
	}

	/**
	 * 关闭给定的输出流. <BR>
	 * 
	 * @param writer
	 */
	public static void close(Writer writer) {
		if (writer != null) {
			try {
				writer.close();
			} catch (IOException e) {
				log.error("error on close the outputstream.", e);
			}
		}
	}
	/**
	 * 关闭给定的Socket.
	 * 
	 * @param socket
	 *            给定的Socket
	 */
	public static void close(Socket socket) {
		if (socket != null) {
			try {
				socket.close();
			} catch (IOException e) {
				log.error("fail on close socket: " + socket, e);
			}
		}
	}
	public static void close(Reader reader) {
		if (reader != null) {
			try {
				reader.close();
			} catch (IOException e) {
				log.error("error on close the Reader.", e);
			}
		}
	}
	public static void close(Connection conn) {
		if (conn != null) {
			try {
				conn.close();
			} catch (Exception e) {
				log.error("error on close java.sql.Connection.", e);
			}
		}
	}
	public static void close(PreparedStatement ps) {
		if (ps != null) {
			try {
				ps.close();
			} catch (Exception e) {
				log.error("error on close java.sql.PreparedStatement.", e);
			}
		}
	}
	public static void close(ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (Exception e) {
				log.error("error on close java.sql.ResultSet.", e);
			}
		}
	}
	public static void close(Statement st) {
		if (st != null) {
			try {
				st.close();
			} catch (SQLException e) {
				log.error("error on close java.sql.Statement.", e);
			}
		}
	}
}

5. Utils类里面包含了更多的判断方法,常用的:

1)判断对象是否为空isEmpty(),isNotEmpty()

2) 根据ip地址回去客户端地址;获取用户ip地址

3) 钱转化为大写方式展示

4) 身份证验证

......

6. SpringUtils类主要是从spring容器中回去对象和获取Resource

 

public final class SpringUtils<span style="color:#ff0000;"> implements BeanFactoryPostProcessor, ResourceLoaderAware </span>{

	private static ConfigurableListableBeanFactory beanFactory; // Spring应用上下文环境
	private static ResourceLoader resourceLoader;

	<span style="color:#ff0000;">@Override
	public void setResourceLoader(ResourceLoader resourceLoader) {
		SpringUtils.resourceLoader = resourceLoader;
	}

	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		SpringUtils.beanFactory = beanFactory;
	}</span>

	/**
<span style="color:#ff0000;">	 * 1. classpath: classpath:com/myapp/config.xml , 加载classpath下的资源
	 * 2. file: file:/data/config.xml Loaded as a URL, 加载文件系统下的资源
	 * 3. http: http://myserver/logo.png Loaded as a URL. 加载url下面的资源
	 * 4. (none) /data/config.xml , 根据当前的applicationContext的类型来加载资源</span>
	 * 
	 * @param location
	 * @return
	 */
	public static Resource getResource(String location) {
		return resourceLoader.getResource(location);
	}

	/**
	 * 获取对象
	 * 
	 * @param name
	 * @return Object 一个以所给名字注册的bean的实例
	 * @throws org.springframework.beans.BeansException
	 * 
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getBean(String name) throws BeansException {
		return (T) beanFactory.getBean(name);
	}

	//省略后面方法......
	

SpringUtils实现了BeanFactoryPostProcessor 和 ResourceLoaderAware ,就可以获取到beanFactory和resourceLoader对象,从而可以获取spring容器中的对象,资源

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics