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