锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
public static boolean hasLength(String str)
{
return str != null && str.length() > 0;
}
public static String trimRight(String source){
if(!hasLength(source))
return source;
if(source.trim().length()==0)
return "";
int index=0;
for(int i=source.length()-1;i>=0;i--){
if(Character.isWhitespace(source.charAt(i))){
index=i;
}else{
break;
}
}
return index!=0 ? source.substring(0,index): source;
}
public static String trimLeft(String source){
if(!hasLength(source))
return source;
if(source.trim().length()==0)
return "";
int index=0;
for(int i=0;i<source.length();i++){
if(Character.isWhitespace(source.charAt(i))){
index=i+1;
}else{
break;
}
}
return index!=0 ? source.substring(index): source;
}
]]>
public interface a {
聽public void sayHello();
}
---------------------------
絎竴縐嶆儏鍐墊槸褰?br />aImpl瀹炵幇綾諱負榛樿鐨勬瀯閫犳柟娉曪細
鏍規嵁綾誨悕aImpl鍔ㄦ佸緱鍒板疄渚?br />Class c=Class.forName("aImpl");
a instance=(a)c.newInstance();
a.sayHello();
錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞
絎簩縐嶆儏鍐靛綋
aImpl涓哄甫鍙傛暟鐨勬瀯閫犳柟娉曟椂錛?br />Class o=Class.forName("aImpl");//aImpl涓鴻瀹炰緥鍖栫殑渚嬪悕錛屽彲浠ヤ粠閰嶇疆鏂囦歡褰撲腑鑾峰彇
聽聽聽Constructor cous = null;
聽聽聽cous=o.getConstructor(new Class[]{String.class});//鏋勯犲櫒鐨勫弬鏁扮被鍨?br />聽聽聽a ao=(a) cous.newInstance(new Object[]{"xxx"});//"xxx涓轟紶鍏ョ殑鍙傛暟鍊?br />聽聽聽ao.sayHello();
絎笁縐嶅氨鏄瀯閫犲櫒涓虹鏈夌殑鏃跺欙紝鎴戜滑閫氳繃鏂規硶鑾峰彇瀹炰緥getInstance()(鑷繁瀹氱殑)
Class o=Class.forName("aImpl");
聽聽聽Method method=o.getMethod("getInstance",new Class[]{String.class});
//getInstance涓鴻繑鍥炰負aImpl瀹炰緥鐨勬柟娉曞悕
聽聽聽 a ao=(a) method.invoke(null,new Object[]{"xxx"});//娉ㄦ剰浜嗗墠闈㈢殑鍙傛暟涓簄ull錛屾槸鏈?br />//鏉′歡鐨?灝辨槸瑕佹眰getInstance涓簊tatic綾誨瀷鐨勶紝鎴戞兂澶у閮戒細榪欐牱璁捐鐨?br />聽聽聽聽聽聽 ao.sayHello();
鍋氳繛鎺ユ睜鐨勮瘽錛屼竴縐嶆柟寮忓氨鏄嬌鐢ㄥ姩鎬佷唬鐞嗙被錛屽綋Connection璋冪敤close()鏂規硶鏃訛紝鎴戜滑鍙互鍒╃敤鏂規硶鎷︽埅鍣紝鍦ㄨ皟鐢╟lose鏂規硶鐨勬椂鍊欐妸榪炴帴鏀懼埌緙撳瓨閲岄潰鍘?渚涗互鍚庡啀嬈″埄鐢紟錛?br />a ao=(a)Proxy.newProxyInstance(aImpl.class.getClassLoader(),aImpl.class.getInterfaces(),
聽聽聽聽聽new InvocationHandler()
聽聽聽聽聽{
聽聽聽聽聽聽public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
聽聽聽聽聽聽聽Object re=null;
聽聽聽聽聽聽聽System.out.println("before--------"+method.getName());
//鍦ㄨ繖閲屾垜浠坊鍔犺嚜宸辯殑涓浜涘鐞嗘柟娉曪紝濡傛妸榪炴帴鏀懼埌緙撳瓨鍘繪拻!
聽聽聽聽聽聽聽//re=method.invoke(new aImpl(),args);
聽聽聽聽聽聽聽return re;
聽聽聽聽聽聽}
聽聽聽聽
聽聽聽聽聽});
聽聽聽ao.sayHello();