2017年

/**
 * 应用完全启动后, Spring执行自定义初始化 此处初始化避免了初始化static时 bean还没注入的问题
 */
spirng mvc xml 文件
<!-- spring环境都初始化完成后执行的一些操作 -->
<bean id="instantiationListener" class ="com.xxx.xxx.web.base.InstantiationListener" />



import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; /** * 应用完全启动后, Spring执行自定义初始化 此处初始化避免了初始化static时 bean还没注入的问题 */ public class InstantiationListener implements InitializingBean { private static final Logger logger = LoggerFactory.getLogger(InstantiationListener.class); @Autowired private WechatAccessTokenService wechatAccessTokenService;// 这里注入不会有生存周期的问题 // ==>继承于InitializingBean 的afterPropertiesSet()方法是初始化方法. @Override public void afterPropertiesSet() { try { System.out.println("init.....初始化微信获取accessToken service...."); JwTokenAPI.wechatAccessTokenService = wechatAccessTokenService; //JwTokenAPI类的静态属性是wechatAccessTokenService } catch (Exception e) { logger.error("初始化微信获取accessToken service [wechatAccessTokenService] 异常"); } } }

  

	/**
	 * 功能:将http://example.com重定向至http://www.example.com
	 * */
	private static final String DOMAIN = "aaa.com";

	/**
	 * 在DispatcherServlet之前执行
	 * */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, 
			Object handler) throws Exception {

		HttpServletRequest httpServletRequest = (HttpServletRequest) request;
		HttpServletResponse httpServletResponse = (HttpServletResponse) response;
		// 获取域名
		String serverName = request.getServerName();
		// 获取请求路径
		String path = httpServletRequest.getRequestURI();
		//queryString 支持多参数 ?a=1&b=2#&c=22## 等
		String queryString = (httpServletRequest.getQueryString() == null ? "" : "?"+httpServletRequest.getQueryString());   // 获取路径中的参数
		int end = serverName.indexOf(DOMAIN);
		if (end!= -1 && end == 0) {  //判断是否是example.com,如果是通过ip地址访问暂时不处理 //end == -1 || end == 0(不是本域名都跳转)
			httpServletResponse.setStatus(301);
			httpServletResponse.setHeader( "Location", "http://www."+DOMAIN+path+queryString);
			httpServletResponse.setHeader( "Connection", "close" );
			return false;
		}


		return true;
	}

  

	JsonObject(com.alibaba.fastjson)返回值修改后,源对象的值也发生改变解决方法
	public  static  JSONObject   getObj(String keyParam){
		JSONObject res = =Map1.get(keyParam);
		if(res!=null){
		    //使用clone方法防止影响静态变量的值
			res = (JSONObject)res.clone(); //之所以 要clone是因为上层调用若修改返回的jsonObject里面的值,会影响map (静态变量的值) 
		}
		return  res;
	}

  

Using ArrayUtils.removeElement(Object[],Object)from org.apache.commons.lang is by far the easiest way to do this.int[] numbers = {1,2,3,4,5,6,7};//removing number 1
numbers =(int[])ArrayUtils.removeElement(numbers, 1); //1 是实际数组里面的值

当然也可以根据数组的索引进行移除,详情参考下方api



commons.apache.org library:Javadocs

maven引入
<!-- java执行 linux 命令  -->
<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>262</version>
</dependency>

import org.springframework.core.io.ClassPathResource; import org.springframework.util.ResourceUtils; import java.io.*; import java.util.ArrayList; import java.util.List; import ch.ethz.ssh2.*; import ch.ethz.ssh2.StreamGobbler; /** * Created by Administrator on 2017/8/18 018. */ public class Test { public static List<String> l = new ArrayList<String>(); public static void main(String args[]) { exportSvnCommend();//初始化list String hostname = "172.1.1.1"; String username = ""; String password = ""; try { /* Create a connection instance * * pom add <dependency> <groupId>ch.ethz.ganymed</groupId> <artifactId>ganymed-ssh2</artifactId> <version>262</version> </dependency> * * * */ Connection conn = new Connection(hostname); /* Now connect */ conn.connect(); /* Authenticate. * If you get an IOException saying something like * "Authentication method password not supported by the server at this stage." * then please check the FAQ. */ boolean isAuthenticated = conn.authenticateWithPassword(username, password); if (isAuthenticated == false) throw new IOException("Authentication failed."); //************************ for循环执行命令*******************start*********************** /** * throw new IOException("A remote execution has already started.");所以如果需要执行多条命令 * ,则把多条命令柔和为1条命令,命令之前用回车\n连接,这样就可以成功执行,拿到所有的控制台输出内容 */ String cmd = ""; // cmd = "whereis java"; //test // cmd = "svnadmin create /usr/local/Repositories/item1"; for (int i =0;i<l.size();i++){ String itemName = l.get(i); //创建资源库并导入 cmd += "svnadmin create /home/SvnRepositories/"+itemName+"\n";//可以一次执行多个命令 cmd += "svnadmin load /home/SvnRepositories/"+itemName+" < /home/SvnRepositories/svnbak/svnbak/"+itemName+".dump"+"\n";//可以一次执行多个命令 execLinuxCommend(cmd,conn); } //************************ for循环执行命令************end****************************** /* Close the connection */ conn.close(); } catch (Exception e) { e.printStackTrace(); e.printStackTrace(System.err); System.exit(2); } } public static void execLinuxCommend(String cmd,Connection conn){ try { /* Create a session */ Session sess = conn.openSession(); sess.execCommand(cmd); System.out.println("Here is some information about the remote host:"); /* * This basic example does not handle stderr, which is sometimes dangerous * (please read the FAQ). */ InputStream stdout = new StreamGobbler(sess.getStdout()); BufferedReader br = new BufferedReader(new InputStreamReader(stdout)); while (true) { String line = br.readLine(); if (line == null) break; System.out.println(line); } /* Show exit status, if available (otherwise "null") */ System.out.println("ExitCode: " + sess.getExitStatus()); /* Close this session */ sess.close(); }catch (Exception e){ e.printStackTrace();; } } //批量导出windows visualSVN的资源文件备份命令 public static void exportSvnCommend(){ try{ // File cfgFile = ResourceUtils.getFile("classpath:1.txt"); File file = ResourceUtils.getFile("classpath:1.txt"); BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(file)); String tempString = null; int line = 1; // 一次读入一行,直到读入null为文件结束 while ((tempString = reader.readLine()) != null) { // 显示行号 // System.out.println("line " + line + ": " + tempString); System.out.println("svnadmin dump F:\\Repositories\\" + tempString + " > F:\\svnbak\\"+tempString+".dump"); l.add(tempString); line++; } reader.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (Exception e1) { e1.printStackTrace(); } } } }catch (Exception e){ e.printStackTrace(); } } }

 

免责声明
本博客部分内容来自于互联网,不代表作者的观点和立场,如若侵犯到您的权益,请联系[email protected]。我们会在24小时内进行删除。