What is AOP in the Java programmer interview test book? A programmer interview test book

Source: Internet
Author: User

What is AOP in the Java programmer interview test book? A programmer interview test book

AOP (Aspect-Oriented Programming) is a supplement to object-oriented development, it allows developers to dynamically modify the model without changing the original model to meet new requirements. For example, you can dynamically add log, security, or exception handling functions without changing the original business logic model.

The following is a simple example of using AOP programming in Spring.

(1) create an interface and the class that implements this interface. The content of TestAOPIn. java is as follows.

Public interface TestAOPIn {

Public void doSomething ();

}

The content of TestAOPImpl. java is as follows.

Public class TestAOPImpl implements TestAOPIn {

Public void doSomething (){

System. out. println ("TestAOPImpl: doSomething ");

}

}

(2) Configure SpringConfig. xml so that the instantiated object of this class can be injected into the Test class using this object.

<? Xml version = "1.0" encoding = "UTF-8"?>

<! DOCTYPE beans PUBLIC "-// SPRING // dtd bean // EN" "http://www.springframework.org/dtd/spring-beans.dtd">

 

<Beans>

<Bean id = "testAOPBean" class = "org. springframework. aop. framework. ProxyFactoryBean">

<Property name = "target">

<Bean class = "testAOPIn" singleton = "false"/>

</Property>

</Bean>

</Beans>

(3) After completing the configuration file, write the test code as follows.

Import org. springframework. context. ApplicationContext;

Import org. springframework. context. support. FileSystemXmlApplicationContext;

Public class Test {

Public static void main (String [] args ){

ApplicationContext ctx = new FileSystemXmlApplicationContext ("SpringConfig. xml ");

TestAOPIn t = (TestAOPIn) ctx. getBean ("testAOPBean ");

T. doSomething ();

}

}

The program output result is:

TestAOPImpl: doSomething

After writing this module, developers need to track the doSomething () method call, that is, to track when the method is called and when the call ends. Of course, the traditional method can also be used to implement this function, but it will produce additional overhead, that is, the existing module needs to be modified. Therefore, you can use the following method to implement this function.

Public class TestAOPImpl implements TestAOPIn {

Public void doSomething (){

System. out. println ("beginCall doSomething ");

System. out. println ("TestAOPImpl: doSomething ");

System. out. println ("endCall doSomething ");

}

}

In this case, you can use the AOP method. It can complete the same function without modifying the original module. Shows the implementation principle.

To do this, you need to provide a class to track function calls. The content of the traceBeforeCall. java file is as follows.

Public class traceBeforeCall implements MethodBeforeAdvice {

Public void beforeCall (Method arg0, Object [] arg1, Object arg2) throws Throwable {

System. out. println ("beginCall doSomething ");

}

}

The content of the traceEndCall. java file is as follows.

Import java. lang. reflect. Method;

Import org. springframework. aop. AfterReturningAdvice;

Public class traceEndCall implements AfterReturningAdvice {

Public void afterCall (Object arg0, Method arg1, Object [] arg2, Object arg3) throws Throwable {

System. out. println ("endCall doSomething );

}

}

You only need to configure in the configuration file to call the traceBeforeCall () method of the traceBeforeCall class before calling the doSomething () method. After calling the doSomething () method, you need to call the afterCall method of the traceEndCall class, the Spring container will automatically call the corresponding method before and after the doSomething () method is called according to the configuration file, through the beforeCall () method and afterCall () method () the code added to the method can meet the tracing requirements for doSomething () method calls, and does not need to change the originally implemented code module.


From the official website of the new book "Java programmer interview test book"


How can I answer aop during an interview with a java programmer?

Interview is to install B
Aop is only a basic concept for Aspect-Oriented Programming. Beginners who have read some tutorials know it.
Therefore, we must deepen the process. You must tell him that the implementation principle of aop is actually java Dynamic proxy, but the dynamic proxy of jdk must implement interfaces. Therefore, spring's aop is implemented using the cglib library, cglib uses the asm framework to directly manipulate bytecode, so dynamic proxy can be completed without implementing interfaces.
I 'd better give him two examples by hand with a sheet of paper, and then he will have nothing to ask.

In java, if someone asks you what is aop, how do you answer?

Aspect-Oriented Programming.

The main implementation is through the dynamic proxy of jdk.
Proxy and InvocationHandler
Or is implemented based on inheritance.

This problem mainly refers to your understanding of dynamic proxy.

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.