What this blog says:
Scenario: When we use a lot of frames, we slowly find it annoying to write a configuration file, such as writing a small function to write a few configuration files.
For this kind of problem, so many frameworks have the function of annotation, how to use annotations instead of configuration files?
What we are going to do today is to scan class and find all of our controller classes, and then we'll talk about that.
Solution Ideas:
First of all, let's think about spring's approach, and spring's use of annotations must be written in a scanclass in the configuration file (in this case, the main configuration file).
This statement is to start the scan, and then let you tell him the path of the scan is which, so we have the idea, we have to do with annotations instead of configuration files, it is also to do the scan
Such a thing. Complete the steps as follows:
First step: Create @Controller This annotation to identify whether it belongs to the Controller class, and the attribute space () represents the path of access.
Step Two: Scan all the files under Webroot/web-inf/classes/, because the path classes in the WEB standard.
Step three: Filter out files that do not end in. class;
Fourth step: Replace "/" with ".". Like turning the com/everxs/jvnconfig.class into a com.everxs.JvnConfig.class.
Fifth step: Determine if the class is @Controller annotated, if it is, it belongs to the controller. Add it to the map and save it.
Having done the above steps, we can get all the controller classes.
code example:
1. Find the file name of all files under the specified path
/** * Returns all the file under the path and joins the List * @return */public static list<string> Listfileabsolutepath (String filepath,list< String> list) {file root = new File (filePath); file[] files = root.listfiles (); for (File file:files) { if (file.isdirectory ()) { / * * * * Recursive call * /Listfileabsolutepath ( File.getabsolutepath (), list); } else{ List.add (File.getabsolutepath ()); } } return list;}
2, filter out not end with. Class and use "." Replace the "/",
/** * Returns all the file under the path and joins the list * @return */public static list<string> Listclassfileabsolutepath (String filePath) {List <String> list = new arraylist<string> (); list<string> classlist = new arraylist<string> () Listfileabsolutepath (FilePath, list); for (String s:list) {//If the file ends with a. Class, then the class file if (S.endswith (". Class")) {//s = S.substring (Filepath.length ()); String ss = s.substring (Filepath.length ()-1);//Get the full class name change com\everxs\jvnconfig to com.everxs.JvnConfigString prefix = Ss.substring (0,ss.length ()-6). replace ("\ \", "."); /Add into Listclasslist.add (prefix);}} return classlist;}
3, find the @controller annotated class, stored in the map
/** * Scan class * @author Administrator * */public class Scankit {public static void Scanclass (Constant Constant) {//Get classes Absolute Road Hard String path = ScanKit.class.getClassLoader (). GetResource (""). GetPath ();//Get the full name of the class for example: con.everxs.test.testcontroller.classlist<string> listclass= Filekit.listclassfileabsolutepath (path); String Clazzstr:listclass) {try {///find Classclass clazz = Class.forName (CLAZZSTR) for this class full name; if (clazz!=null) {Controller Controller= (Controller) clazz.getannotation (Controller.class), if (controller!=null) {Constant.setroute ( Controller.space (), clazz);}}} catch (Exception e) {System.out.println ("class file not Found");}}}
4, start the scan at boot time
/** * Scans all class */public void Sacnclass () {scankit.scanclass (CONSTANT);}
5, test
@Controller (space = "/pile") public class Pilecontroller extends jvncontroller{public void Add () {renderstring ("Add Method"); public void Delete () {renderstring ("Delete method");}}
Summarize:
This completes the process of using annotations instead of configuration files.
about JVN:
Framework named JVN, the blog has a continuous development of video, each blog is a knowledge point, about the framework of the introduction and learning, from my blog first start to look at;
The source and video of this content:http://pan.baidu.com/s/1mgIKhXe
JVN frame QQ Exchange Group:399603805
Blog Home : http://www.cnblogs.com/everxs/
Forever Starling ...
How to develop a Java open source framework-----The implementation of JVN framework using annotations instead of configuration files