Self-implemented simple AOP (4) Automatic initialization of proxy objects, and aop initialization of proxy objects

Source: Internet
Author: User

Self-implemented simple AOP (4) Automatic initialization of proxy objects, and aop initialization of proxy objects

 

The first three articles have completed the core functions of AOP, but the initialization of proxy objects is still a little troublesome. This article will solve this problem.

 

The Demo snippets are as follows:

Public class HomeController: Controller {// <summary> // use Autowired Attribute to automatically initialize the proxy object /// </summary> [Autowired] public Service myService {get; set;} public ActionResult Index () {myService. test (); var msg = myService. errorMsg; Console. writeLine (msg); // Of course, the Code attribute in ServiceException can also be stored in the ServiceAbstract object return View ();}}

 

In the code snippet above, myService is not assigned a value, but is directly used to call the Test method. When is this attribute assigned?

The answer is: when the MVC Framework and Controller are activated.

In the MVC Framework, Controller activation is completed in DefaultControllerFactory. You can rewrite this class and register it to implement it. When the Controller is activated, the proxy object is automatically initialized.

 

The Global. asax code is as follows:

// Note: For instructions on enabling IIS6 or IIS7 classic mode, // visit http://go.microsoft.com/?LinkId=9394801 Public class MvcApplication: System. web. httpApplication {protected void Application_Start () {// register a new Controller factory ControllerBuilder. current. setControllerFactory (new MyBatisControllerFactory (); AreaRegistration. registerAllAreas (); WebApiConfig. register (GlobalConfiguration. configuration); FilterConfig. registerGlobalFilters (GlobalFilters. filters); RouteConfig. registerRoutes (RouteTable. routes);} private class MyBatisControllerFactory: DefaultControllerFactory {public override IController CreateController (RequestContext requestContext, string controllerName) {IController controller = base. createController (requestContext, controllerName); // automatically assemble attributes /// <para> enable proxy for the attribute object and delay initializing the object to be proxy </para> DelayProxyUtil. autowiredProperties (controller); return controller ;}}}

 

Source code: http://files.cnblogs.com/files/08shiyan/AOPDemo.zip

 

(Self-implemented simple AOP temporarily and will be supplemented later)

 

Related Article

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.