AOP Series (ii)--creating proxies for eligible target objects

Source: Internet
Author: User

Then, in the previous article, we will take a new approach to dynamic proxies.

Objectnameautoproxycreator Creating an Agent

Implementation ideas

Depending on the configuration in the configuration file, the spring container creates proxies for eligible objects based on this configuration.

Specific code

Configuration file

<?xml version= "1.0"?><configuration>    <configsections>        <sectiongroup name="Spring">            < section name="context" type=" Spring.Context.Support.ContextHandler, Spring.core "/>            < section name= "objects" type=" Spring.Context.Support.DefaultSectionHandler, Spring.core "/>        </sectiongroup>    </configsections>    <spring>        <context>            <resource uri="config://spring/objects"/>        </Context>        <objects xmlns="Http://www.springframework.net">            <object id="Beforeadvice" type=" Aopexample.logbeforeadvice,aopexample "/>            <!--configured in the container, which is required so that the object can be found in the container based on the suffix service- -            <object id="UserService" type="Aopexample.userservice,aopexample" />            <!--automatically create an AOP agent with Objectnameautoproxycreator -                               <object id="Iserviceproxy" type=" Spring.Aop.Framework.AutoProxy.ObjectNameAutoProxyCreator, SPRING.AOP ">                < property name="Objectnames">                    <list>                        <!--as long as the suffix of the object is called service, the agent is automatically created for them --                        <value>*service</value>                    </list>                </Property >                < property name="Interceptornames">                    <list>                        <value>Beforeadvice</value>                    </list>                </Property >            </Object>        </Objects>    </Spring>    </configuration>

Client

    class Program    {        staticvoid Main(string[] args)        {            new User() {                 Name ="Danny",                Age=15            };            IApplicationContext   context = ContextRegistry.GetContext();            IUserService userService = (IUserService)context.GetObject("userService");            userService.GetUserInfo(enUser);        }    }
Summarize
    • Advantage : In this example, the spring container generates a proxy class for multiple target objects ending with a name service.
    • Pros : Only need to be configured once in the configuration file, not once for each target object.
    • Advantage : The client's code does not have to be changed, the interface is called, not the proxy class.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

AOP Series (ii)--creating proxies for eligible target objects

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.