How to Develop a java open-source framework ----- implement custom Annotation and interceptor in the Jvn framework (Lecture 5) ----- jvnannotation

Source: Internet
Author: User

How to Develop a java open-source framework ----- implement custom Annotation and interceptor in the Jvn framework (Lecture 5) ----- jvnannotation
Preface

I,

The blogger is teaching you how to develop a javaEE framework (Jvn framework). The blog has a complete development video. Every blog post is a knowledge point to help you understand the framework:

This content: http://pan.baidu.com/s/1hq3sng4

1. Why should we use the java framework and what benefits can the framework bring. 2. helps you understand the principles of the Framework.

3. How is the framework implemented. 4. How to develop a java framework of your own.

II,
Currently, the developed framework is called the Jvn framework, and the author's name is "Gossip". Let's get started with the development framework.
The Jvn framework has the following features:
1. the mvc Framework, similar to the web framework StringMvc or struts, does not write servlets and does not need to write servlet configurations in web. xml.

2. The orm framework, similar to hibernate or ibatis, is not worrying about complicated jdbc operations.

3. The spring framework, similar to the spring framework, has never worried about bean management;

4. cache mechanism.

5. Regular scheduling.

6. automatically generate online documents.

7. interceptor

III,
Each blog post has a detailed explanation of the video and the corresponding source code, and the video is continuous. To learn how to develop the Jvn framework, we recommend that you start with the first blog post Jvn.

Content of this blog

Recall: In the previous lesson, we talked about how to obtain the parameters passed by the browser and encapsulate them into our Bean.

In this lesson, we will implement:

1. Custom Annotation.

2. Implement a custom interceptor.

Implementation ideas:

First define a self-defined Annotation: aop, and then define a self-contained Interceptor: JvnInterceptor. Define three methods: before, after, exception

Before the Controller method is executed when the filter is passed, call before and then execute after. An exception occurs when the Controller method is executed.

 

The following is the content video and the source code address:

 

Http://pan.baidu.com/s/1hq3sng4

 

Ps: videos and code will be updated continuously. I hope you will like it and support it a lot.

QQ: 245223343 forever brother eight

 

The code I wrote is attached below:

Here is an Annotation defined

Package com. jvn. annotation; import java. lang. annotation. retention; import java. lang. annotation. retentionPolicy;/*** interceptor annotation * @ author Administrator **/@ Retention (RetentionPolicy. RUNTIME) public @ interface Aop {// interceptor name Class interceptor ();}

This is the interceptor interface.

Package com. jvn. core;/*** interceptor interface * @ author Administrator **/public interface JvnInterceptor {// public void before () before execution; // public void after execution (); // public void exception ();}

Here is where my Framework calls the interceptor.

Package com. jvn. core; import java. lang. reflect. method; import javax. management. runtimeErrorException; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import com. jvn. annotation. aop; /*** Process action * @ author Administrator **/public class ActionHandle {/*** Process action * @ param method * @ param controller * @ param req * @ param resp * /public void handel (String meth OdName, Class controller, HttpServletRequest req, HttpServletResponse resp) {try {Object obj = controller. newInstance ();/*** call setRequest. setResponse injects the req and resp parameters into */controller. getMethod ("setRequest", HttpServletRequest. class ). invoke (obj, req); controller. getMethod ("setResponse", HttpServletResponse. class ). invoke (obj, resp); Method method = controller. getMethod (methodName, null);/*** Step 1: first take a look at me Does thod have @ Aop * Step 2? If yes, check the Clazz * Part 3, create an object, and execute the corresponding method */Aop aop = method. getAnnotation (Aop. class); if (aop! = Null) {JvnInterceptor interceptor = (JvnInterceptor) JvnConfig. beanFactory. createSimpleBean (aop. interceptor ());
// Execute interceptor. before (); try {method. invoke (obj, null);} catch (Exception e ){
// Exception interceptor. exception (); throw new RuntimeException (e);} // execute interceptor after the method is executed. after ();} else {method. invoke (obj, null) ;}} catch (Exception e) {throw new RuntimeException (e );}}}

This is my test Controller

Package com. everxs. test; import com. jvn. annotation. aop; import com. jvn. core. jvnController;/*** user-managed Controller * @ author Administrator **/public class UserController extends JvnController {/*** deletes a user, the annotation */@ Aop (interceptor = MyInterceptor. class) public void delete () {System. out. println ("delete method executed ");}}

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.