摘要: 概念: java中單例模式是一種常見的設(shè)計(jì)模式,單例模式分三種:懶漢式單例、餓漢式單例、登記式單例三種。 單例模式有一下特點(diǎn): 1、單例類只能有一個(gè)實(shí)例。 2、單例類必須自己自己創(chuàng)建自己的唯一實(shí)例。 3、單例類必須給所有其他對(duì)象提供這一實(shí)例。 單例模式確保某個(gè)類只有一個(gè)實(shí)例,而且自行實(shí)例化并向整個(gè)系統(tǒng)提供這個(gè)實(shí)例。在計(jì)算機(jī)系統(tǒng)中,線程池、緩存、日志對(duì)象、對(duì)話框、打印機(jī)、顯卡的驅(qū)動(dòng)程序?qū)?..
閱讀全文
所謂值傳遞,就是說僅將對(duì)象的值傳遞給目標(biāo)對(duì)象,就相當(dāng)于copy;系統(tǒng)將為目標(biāo)對(duì)象重新開辟一個(gè)完全相同的內(nèi)存空間。
所謂引用,就是說將對(duì)象在內(nèi)存中的地址傳遞給目標(biāo)對(duì)象,就相當(dāng)于使目標(biāo)對(duì)象和原始對(duì)象對(duì)應(yīng)同一個(gè)內(nèi)存存儲(chǔ)空間。此時(shí),如果對(duì)目標(biāo)對(duì)象進(jìn)行修改,內(nèi)存中的數(shù)據(jù)也會(huì)改變。
值傳遞,例如:
class TestT1
{
public static void main(String[] args)
{
int i = 5;
int j = 6;
System.out.println("before exchange i = "+i);//交換前
exchange(i, j);
System.out.println("after exchange i = "+i);//交換后
}
public static void exchange(int a,int b)
{
int k;
k = a;a = b; b = k;
}
}
程序的結(jié)果是5!!!
這說明,原始數(shù)據(jù)類型是按值傳遞的,這個(gè)按值傳遞也是指的是進(jìn)行賦值時(shí)的行為。
Java語言明確說明取消了指針,因?yàn)橹羔樛窃趲矸奖愕耐瑫r(shí)也是導(dǎo)致代碼不安全的根源,同時(shí)也會(huì)使程序的變得非常復(fù)雜難以理解,但這只是在Java語言中沒有明確的指針定義,實(shí)質(zhì)上每一個(gè)new語句返回的都是一個(gè)指針的引用。
引用傳遞,例如:
class TestT2
{
public static void main(String[] args)
{
StringBuffer s= new StringBuffer("good");
StringBuffer s2=s;
s2.append(" afternoon.");
System.out.println(s);
}
}
對(duì)象s和s2指向的是內(nèi)存中的同一個(gè)地址因此指向的是同一個(gè)對(duì)象。
這里的意思是進(jìn)行對(duì)象賦值操作是傳遞的是對(duì)象的引用,因此對(duì)象是按引用傳遞的。
程序運(yùn)行的輸出是:
good afternoon.
這說明s2和s是同一個(gè)對(duì)象。
總結(jié):
大家都知道,在JAVA中變量有以下兩種:
基本類型變量,包括boolean、byte、char、short、int、long、float、double。
引用類型變量,包括類、接口、數(shù)組(基本類型數(shù)組和對(duì)象數(shù)組)。
對(duì)于基本類型和基本類型變量被當(dāng)作參數(shù)傳遞給方法時(shí),是值傳遞。在方法實(shí)體中,無法給原變量重新賦值,也無法改變它的值。
而對(duì)象作為參數(shù),如果在方法中把對(duì)象作為參數(shù),方法調(diào)用時(shí),參數(shù)傳遞的是對(duì)象的引用,即在方法調(diào)用時(shí),實(shí)際參數(shù)把對(duì)對(duì)象的引用傳遞給形式參數(shù)。這是實(shí)際參數(shù)與形式參數(shù)指向同一個(gè)地址,即同一個(gè)對(duì)象,方法執(zhí)行時(shí),對(duì)形式參數(shù)的改變實(shí)際上就是對(duì)實(shí)際參數(shù)的改變,這個(gè)結(jié)果在調(diào)用結(jié)束后被保留了下來。
形參和實(shí)參有以下顯著的區(qū)別:
1、形參不能離開方法。形參只有在方法內(nèi)才會(huì)發(fā)生作用,也只有在方法中使用,不會(huì)在方法外可見。而實(shí)參可以再程序的任何地方都使用。
2、形參代表一個(gè)合集,具有不確定性,而形參代表一個(gè)獨(dú)立事物,具有確定性(即使是為null)。也就是說,形參不能代表具體的對(duì)象,只能代表這些對(duì)象共同的屬性(比如超類、各種其他自定義屬性等等),而實(shí)參則是具體的對(duì)象(比如超類的實(shí)例)。
3、形參的值在調(diào)用時(shí)根據(jù)調(diào)用者更改,實(shí)參則用自身的值更改形參的值(指針、引用皆在此列)
僅對(duì) @requestMapping("/xxxx/其他參數(shù)") 的格式
package cc.wshao.springmvc.util;
import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import cc.wshao.springmvc.service.RightService;
public class RegRightUtils {
private static final String rootUrl = "/spring";
private static RightService rightService;
public static void main(String[] args) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
rightService = context.getBean(RightService.class);
ClassLoader classLoader = RegRightUtils.class.getClassLoader();
URL url = classLoader.getResource("com/er07/book/springmvc/controller");
String rootPath = url.getPath().toString();
File rootFile = new File(rootPath);
File [] files = rootFile.listFiles();
for(File f: files) {
System.err.println(f.getName());
String className = f.getName().substring(0, f.getName().indexOf(".class"));
Class clazz = Class.forName("com.er07.book.springmvc.controller."+className);
parseClass(clazz); //解析 出url
}
}
public static void parseClass(Class clazz) {
if(clazz.isAnnotationPresent(Controller.class)) { //是否為一個(gè)controller ?
String classUrl ="";
if(clazz.isAnnotationPresent(RequestMapping.class)) {
RequestMapping requestMapping_clazz = (RequestMapping) clazz.getAnnotation(RequestMapping.class);
classUrl = requestMapping_clazz.value()[0];
if(classUrl.equals("/")) { //如果是"/" 的話 制空
classUrl ="";
}
}
Method [] ms = clazz.getDeclaredMethods();
for(Method m :ms) {
if(m.isAnnotationPresent(RequestMapping.class)) {
RequestMapping requestMapping_method = m.getAnnotation(RequestMapping.class);
String methodUrl = requestMapping_method.value()[0];
int index = methodUrl.indexOf("/");
index = methodUrl.indexOf("/",index+1);
if(index!=-1) { //如果存在 則進(jìn)行截取前面的url
methodUrl = methodUrl.substring(0, index);
}
String resultUrl = rootUrl+classUrl+methodUrl;
rightService.appendRightByUrl(resultUrl);
}
}
}
}
}
//實(shí)現(xiàn)方法:
public void appendRightByUrl(String url) {
//查詢 此url 在數(shù)據(jù)庫中是否存在 此權(quán)限
String hql = "from Right where rightUrl =?";
Right r = (Right) this.rightDao.uniqueResult(hql, url);
if(r==null) { //不存在 , 插入數(shù)據(jù)庫中
Right newRight = new Right();
newRight.setRightUrl(url);
this.saveOrUpdateRight(newRight);
}
}