Rose載體是RoseFilter。
只需在web.xml配置文件中配置它,如filter,如下所示:
& lt過濾器& gt
& ltfilter-name & gt;roseFilter & lt/filter-name & gt;
& ltfilter-class & gt;net . paoding . rose . rose filter & lt;/filter-class & gt;
& lt/filter & gt;
& lt過濾器映射& gt
& lt過濾器名稱& gtroseFilter & lt/filter-name & gt;
& lturl模式& gt/* & lt;/URL-pattern & gt;
& lt調度員& gt請求& lt/dispatcher & gt;
& lt調度員& gt轉發& lt/dispatcher & gt;
& lt調度員& gt包括& lt/dispatcher & gt;
& lt/filter-mapping & gt;
玫瑰過濾器
初始化函數:initFilterBean
初始化rose的上下文容器
WebApplicationContext root context = prepareRootApplicationContext();
Rose繼承了XmlWebApplicationContext,並構造了自己的上下文RoseWebAppContext作為最根級別的ApplicationContext對象。
RoseWebAppContext root context = new RoseWebAppContext(getServletContext(),load,false);
root context . setconfiglocation(contextConfigLocation);
root context . setid(" rose . root ");
root context . refresh();
上面的刷新方法和spring的context的初始化過程壹樣,也就是IOC對象的初始化。
調用refresh方法後,配置文件中的bean將被加載到內存中,並成為BeanDefinition。
在rose下,會有下面的配置文件conventi on/we b-INF/application context *。XML。
全部。這種形式的xml文件將被rose識別並加載到IOC容器中。
//識別Rose程序模塊
this . modules = prepare modules(root context);
Rose使用控制器作為壹個模塊,控制器下的包也會作為壹個模塊。
模塊對象ModuleResource封裝了與控制器相關的資源。
包括模塊路徑、匹配的xml資源等。
在初始化過程中,通過提供程序加載資源。FindModuleResources (load)構建壹個列表
初始化過程會將模塊中的beandefinition類加載到內存中,並將其保存在ModuleResource中。
module.addModuleClass(類。forName(類名));
模塊資源掃描完成後,為每個特定模塊啟動特定的資源配置構建。
列表& lt模塊& gt模塊= modulesBuilder。構建(moduleResources,rootContext)
建立壹個模塊列表,包括rose的各個模塊,也就是初始化rose的各個模塊。
壹個單獨的模塊模塊將建立自己的上下文對象。
servlet context servlet context = parent . getservlet context();
斷言。not null(servlet context);
ModuleAppContext WAC = new ModuleAppContext();
WAC . set parent(parent);
WAC . setservletcontext(servlet context);
WAC . setcontextresources(to resources(contextResources));
WAC . setid(uniqueId);
wac.setNamespace(命名空間);
WAC . setmessagebasenames(messageBasenames);
WAC . refresh();
再看刷新方法,就是初始化模塊的上下文IOC容器。
然後,模塊相關的bean將被註冊到模塊的IOC容器中。
registerbean definitions(moduleContext,module resource . getmoduleclasses());
然後rose會加載解析器攔截器等模塊相關的資源。
//從Spring應用程序環境中找出這個web模塊要使用的ParamValidator、param resolver、controller interceptor和controller error handler。
列表& ltParamResolver & gtcustomer resolvers = findContextResolvers(moduleContext);
列表& ltInterceptorDelegate & gtinterceptors = findcontext interceptors(moduleContext);
列表& ltParamValidator & gtvalidators = findContextValidators(moduleContext);
ControllerErrorHandler error handler = getContextErrorHandler(moduleContext);
找出相關的資源,並將它們加載到模塊中。
module.addCustomerResolver(解析器);
module . addcontrollerinterceptor(攔截器);
module.addValidator(驗證器);
module . set error handler(error handler);
攔截器的加載如下
for(int I = 0;我& lt截擊機。size();i++) {
//首先判斷是否存在同名的攔截器。
InterceptorDelegate temp = interceptors . get(I);
if (temp.getName()。equals(interceptor . getname()){
// rose內部要求攔截器有唯壹的標識符。
//請咨詢這兩個類的提供者,更改類名。不能同時取相同的類名。
//如果名稱是通過@Component等設置的。,不要設置相同。
controller interceptor duplicated 1 = interceptor delegate
。getMostInnerInterceptor(temp);
controller interceptor duplicated 2 = interceptor delegate
。getMostInnerInterceptor(攔截器);
拋出新的IllegalArgumentException(
"這兩個攔截器的攔截器名稱重復: '"
+ duplicated1.getClass() +“”、“+duplicated 2 . getclass()+“””;
}
//加入這個職位?
如果(!添加& amp& ampinterceptor . get priority()& gt;temp.getPriority()) {
這個截擊機。add(我,攔截器);
added = true
}
}
攔截器的優先級由if(!添加& amp& ampinterceptor . get priority()& gt;Temp.getPriority())這個語句來判斷。
然後將攔截器加載到適當的位置。
加載相關資源後,rose初始化控制器,並初始化模塊中的控制器。
for(String bean name:bean factory . getbean definitionnames()){
checkController(moduleContext,beanName,module);
}
在初始化期間,您可以看到以下語句:
path reqmapping annotation = clazz . get annotation(path . class);
if (reqMappingAnnotation!= null) {
controller paths = reqmapping annotation . value();
}
我們在控制器上標記的@Path註釋在這裏被解析為控制器的新路徑。
另外,妳可以看到下面的語句,rose約定,控制器的命名規範。
// TODO:這段代碼用來使從0.9到1.0的判斷更加平滑,201007之後可以刪除。
if(controller name . equals(" index ")| | controller name . equals(" home ")
| | controller name . equals(" welcome ")){
//這個異常的意義是讓大家在index controller/home controller/welcome controller上明確標註@Path(" ")。
拋出new IllegalArgumentException("請將@Path(\"\ ")添加到"+clazz . getname());
}否則{
controller paths = new String[]{ "/"+controller name };
}
然後rose從上下文中獲取控制器實例,它是壹個沒有標註控制器屬性的singleton。
object controller = context . get bean(bean name);
模塊。addController(//
controllerPaths,clazz,controllerName,controller);
將控制器實例添加到模塊中。
於是壹個模塊被填滿了。
在完成資源的加載之後,是另壹個關鍵步驟,即rose的匹配樹的構建過程。
//在每個節點上創建匹配樹和執行邏輯(引擎)。
this . mapping tree = prepareMappingTree(模塊);
Rose構建壹個根節點,然後從根節點添加枝葉。
mapping root mapping = new constant mapping(" ");
mapping node mapping tree = new mapping node(root mapping);
linked engine root engine = new linked engine(null,new root engine(instruction executor),
mapping tree);
mappingTree.getMiddleEngines()。addEngine(ReqMethod。ALL,root engine);
tree builder tree builder = new tree builder();
treeBuilder.create(mappingTree,modules);
構建過程從create方法開始。
整個構造過程從模塊開始,壹個壹個的添加到匹配樹中。
for(模塊模塊:模塊){
addModule(rootNode,module);
}
然後將控制器的路徑添加到匹配樹中。
對於(控制器參考控制器:控制器){
addController(模塊、父模塊、模塊引擎、控制器);
}
最後,將操作方法的路徑添加到匹配樹中。
for (MethodRef操作:操作){
addAction(模塊、控制器、動作、目標、控制器引擎);
}