The advantages of learning notes-AOP

Source: Internet
Author: User
Tags aop bind exception handling jboss
Note-taking method programming is not meant to replace the face object programming, but to improve it. AOP programmers are generally 90% using OOP to solve problems
And 10% is using AOP to solve problems that OOP cannot solve.

Crosscutting concerns (cross-cutting concerns)
Most of the time you find that your class doesn't have a very clear and explicit table to the functional meaning you want to express, because your real code
Are mostly surrounded by other code. If you want to be good at expanding or integrating the functional meaning you want to express, you'd better use the aspect
thought to consider it.

Layered in development (layering Based on deployment)
Another useful part of AOP is that it can be used to tier your application. A lot of times you want some special applications or
Classes can be well configured, but at the same time hope that these things are not bloated and can be extended. AOP provides a good way to
Layering of these complex things. JBOSS AOP provides the mechanism for XML configuration to configure each aspect of development. The best example is
Caching service, which provides a different locking mechanism. These cache locking mechanisms can be well woven into your inner, without affecting your class's
Code so that your class is a good extensibility.

Transparency (transparency)
Most of the time you want to focus your program on business applications and application logic, not on middleware development.
AOP allows you to transparently apply middleware without getting your code contaminated again. A good example is the one in JBoss AOP
User authentication above.


Exception handling
Handling exceptions is another useful thing that AOP offers us. For example, the SqlException exception contains the SQL statement's
Information such as exception information or deadlock of a database, but this information uses different error codes and information. AOP allows you to intercept
SQL statement information and classify the database deadlock information.

public class Invalidsqlexception extends SQLException
{
Invalidsqlexception (SQLException ex)
{
Super (Ex.getmessage (), Ex.getsqlstate (), Ex.geterrorcode ());
}
}


Aspects of Use
public class Sqlexceptionaspect
{
Public Object handlesqlexception (invocation invocation) throws Throwable
{
Try
{
return Invocation.invokenext ();
}
catch (SQLException ex)
{
if (Isvendorinvalidsqlerrorcode (Ex.geterrorcode ())) throw new Invalidsqlexception (ex);
if (Isvendordeadlockerrorcode (Ex.geterrorcode ()) throw new Sqldeadlockexception (ex);
... and so on ...
}
}
... impl of IsVendor methods ...
}

The configuration used
<aspect class= "Sqlexceptionaspect" scope= "PER_VM"/>
<bind pointcut= "Call (* $instanceof {java.sql.statement}->execute* (..))" >
<advice name= "handlesqlexception" aspect= "Sqlexceptionaspect"/>
</bind>




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.