How to Develop a java open-source framework ----- implementation of the Jvn framework Replace the configuration file with annotations, open-source ----- jvn
Content of this blog:
Scenario: When we use many frameworks, we will find it annoying to write configuration files. For example, to write a small function, we need to write several configuration files.
For this type of problem, many frameworks have the annotation function. How can we replace the configuration file with annotation?
What we need to do today is scan the Class and find all our Controller classes. Next we will discuss this issue.
Solution:
First, let's look back at Spring's practice. Spring must write a ScanClass statement in the configuration file (the main configuration file here) when using annotation,
This statement indicates that the scan is started, and then you will be asked to tell him the scan path, so our idea is there. We need to replace the configuration file with annotations, the same is true for scanning.
Such a thing. The completion steps are as follows:
Step 1: first create the @ Controller annotation to identify whether it belongs to the Controller class. The attribute space () indicates the access path.
Step 2: scan all the files under WebRoot/WEB-INF/classes/because of the path to Classes in the Web standard.
Step 3: filter out files not ended with. class;
Step 4: replace "/" with ".". For example, convert com/everxs/JvnConfig. class to com. everxs. JvnConfig. class.
Step 5: Determine whether the class is annotated by @ Controller. If yes, it belongs to Controller. Add it to map and save it.
After completing the above steps, we can get all the Controller classes.
Sample Code:
1. Find the names of all files in the specified path.
/*** Return all files in the path and add 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 non-ending with. class and replace "/" "/",
/*** Return all files in the path and add 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 yes. if (s. endsWith (". class ") {// s = s. substring (filePath. length (); String ss = s. substring (filePath. length ()-1); // obtain the full class name and change com \ everxs \ JvnConfig to com. everxs. jvnConfigString prefix = ss. substring (0, ss. length ()-6 ). replace ("\\",". "); // Add it to listclassList. add (prefix) ;}} return classList ;}
3. Find the class annotated by @ Controller and save it in map.
/*** Scan class ** @ author Administrator **/public class ScanKit {public static void scanClass (Constant constant) {// get the classes absolute path String path = ScanKit. class. getClassLoader (). getResource (""). getPath (); // obtain the full name of the class, for example, con. everxs. test. testController. classList <String> listClass = FileKit. listClassFileAbsolutePath (path); for (String clazzStr: listClass) {try {// find the ClassClass clazz = Class with the full name of this Class. forName (clazzStr); I F (clazz! = Null) {Controller 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 scanning at startup
/*** Scan all Classes */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 ");}}
Summary:
This completes the process of replacing the configuration file with annotations.
About Jvn:
The framework is named Jvn. There are continuous development videos in the blog. Each blog post is a knowledge point. For introduction and learning about the framework, you can start from the first lecture on my blog;
The content of the source code and video: http://pan.baidu.com/s/1mgIKhXe
Jvn framework QQ chat group: 399603805
Blog home: http://www.cnblogs.com/everxs/
Forever brother eight...