Spring's AOP and inheritance methods

Source: Internet
Author: User

1.spring AOP ignores methods that the slice class inherits from the parent class, unless the parent class method overrides @override in the Slice class, which is the subclass. 

Package Com.skyline.dao;
@Repository
publicclass a{public void A () {
Implements ....
}
public void aa () { //implements ... }

}
Package com.skyline.service;
@Service
publicclassimplements a{ @Autowired
Private a A; Public void B (){//Repeat call A.A ()
} }


<expression= "Execution (* com.skyline.service.*.* (..))" ID= "Log_service"/><pointcut-ref= "Log_ Service " method=" Dobefore "/>

As in the above example, the class that inherits the DAO layer with the service layer class is to avoid code duplication by not having to write the DAO layer method again at the service layer.

One of the problems is that AOP only cuts into the methods defined by the service layer itself. such as B ().

If you change the section as follows:

<expression= "Execution (* com.skyline.dao.*+.* (..))"      ID= "Log_service"/><pointcut-ref= "Log_service"  method= "Dobefore"/>

The match expression is changed to match the DAO layer, and + is the " and subclass ", which means matching all the classes of the DAO layer and all the methods of the subclass.

Now all methods will be cut, for example in calling New B (). AA () This method will only cut into the service layer and not cut into the DAO layer.

But the problem is, for example, the B () method in Class B, which calls the parent Class A () repeatedly, and this a is also constantly being cut into.

I hope that only when the service layer is called to be cut into, and want the service method of the parent method is not cut-in, just into the service layer is good.

Then change to the following:

<expression= "Execution (* com.skyline.dao.*+.* (..)) and  !execution (* Com.skyline.dao.*.* (..)) "       ID= "Log_service"/><pointcut-ref= "Log _service "  method=" Dobefore"/>

If you want to exclude the DAO layer method, just cut into the subclass method. But actually the class method of the DAO layer of the parent class is not cut, the method of inheriting subclass is not cut in, and the effect of cutting service layer at the beginning is the same.

That is, do not cut into the parent class, you can not cut into the sub-class ...

At last...... Tears ran .... Still did not find the satisfactory method ...

Spring's AOP and inheritance methods

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.