[required for development, interview question for Huawei Java senior engineer

If there is such a demand:

In the first version of our application, there was no authorization function. The current requirement is to add an authorization function.

In the above example, before we operate the order information (whether querying or modifying), we add a function to check whether the user is admin.

One way to do this is to create the Authorization class and isAuthorized method to check whether the user is authorized. Then we need to update all methods of OrderServiceImpl and check the Authorization by calling the isAuthorized method.

@Component
public class OrderServiceImpl {
     public Order queryOrder() {
         //Judge whether the current login user has permission to query
         if (Authorization.isAuthorised(Session.getUserId())){
             System.out.println("Reading from collectionA");
             return new Order("1","2019-07-11");
         }
     }
    public int updateOrder() {
         //Judge whether the current login user has permission to modify
         if (Authorization.isAuthorised(Session.getUserId())){
             System.out.println("Writing to collectionA");
             return 1;
         }
     }
}

However, this method has disadvantages. We need to modify each method of the OrderServiceImpl class and repeatedly call the same code of the isAuthorized method in all methods to check the authorization.

If you do not change the existing class, can you complete the authorization check function?

Of course, AOP can be implemented.

Now define an AuthorizationCheck class to create a pointcut for the OrderServiceImpl class. In the suggestion, we check whether the user is an administrator, and then we continue; Otherwise, we will not operate.

No changes need to be made in the OrderServiceImpl class.

The code is as follows:

@Aspect
@Configuration
public class AuthorizationCheck {
     //Use the Around aspect to intercept all methods of the OrderServiceImpl class, and automatically check the permissions when calling the OrderServiceImpl method
     @Around("execution(**com.enjoy.aop.OrderServiceImpl.*(..))")
     public void check (ProceedingJoinPoint jp) {
          try {
               if(Authorization.isAuthorised(Session.getUserId())) {
                     jp.proceed();//Manually executing the target method is equivalent to calling a method of OrderServiceImpl
               }
               else {
                     System.out.println("User not Authorised");
               }
          } catch (Throwable e) {
                    e.printStackTrace();
          }
     }
}

Now, for any call to any method of the OrderServiceImpl class, the order related method is called only when it is satisfied.

As can be seen above, we have completed the authorization function without changing the existing class. Of course, in addition to this use mode, we can also monitor the performance, such as the execution time of the methods in the OrderServiceImpl class. How to operate? see below

Create a custom annotation named TurnAroundTime to check the performance and time of the target method execution.

Here is an example: first, we need to customize an annotation.

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public@interface TurnAroundTime {
}

The object we want to check is a method, so the target type here is method, @ Target(ElementType.METHOD).

RetentionPolicy is runtime because we want to apply them at runtime. Next, we define the section.

@Aspect
@Configuration
public class PerformanceCheck {
    //Block com enjoy. For all methods under the AOP package, when the method declares the TurnAroundTime annotation,
    //You need to execute the watchPerformance aspect method
     @Around("execution(** com.enjoy.aop.*.*.*(..))&& @annotation(TurnAroundTime)")
     public void watchPerformance(ProceedingJoinPoint jp) {
          Instant start = Instant.now();//Timing before adjusting method
          try {
              jp.proceed();//Manually executing the target method is equivalent to calling a method of OrderServiceImpl
          } catch (Throwable e) {
              e.printStackTrace();
          }
          Instant finish = Instant.now();//Timing after adjustment method
          long timeElapsed = Duration.between(start,finish).toMillis();//How long does it take to call a method
          System.out.println("Total Turnaround time:" + timeElapsed);
     }
}

We use pointcuts to define an aspect that we use to listen to the execution of the target method. Record the time difference between before and after the execution of the target method.

Therefore, if we want to measure the performance of the target method, we need to annotate the method with the custom annotation @ TurnAroundTime.

The code is as follows:

summary

The interview inevitably makes people anxious. Everyone who has experienced it knows. But it's much easier if you anticipate the questions the interviewer will ask you in advance and come up with appropriate answers.

In addition, it is said that "the interview makes a rocket, and the work screws". For friends preparing for the interview, you only need to understand one word: brush!

Brush me, brush hard! Since I'm here to talk about the interview today, I have to come to the real interview questions. It didn't take me 28 days to do a "collection of analysis of interview questions for high posts in large Java front-line factories: Java foundation - intermediate - Advanced interview + SSM framework + distributed + performance tuning + microservice + concurrent programming + Network + design pattern + data structure and algorithm, etc."

Data collection method: click here to download for free

And in addition to simply brushing questions, You also need to prepare a copy [JAVA advanced core knowledge manual]: JVM, JAVA collection, JAVA multithreading concurrency, JAVA foundation, Spring principle, microservice, Netty and RPC, network, log, Zookeeper, Kafka, RabbitMQ, Hbase, MongoDB, Cassandra, design pattern, load balancing, database, consistency algorithm, JAVA algorithm, data structure, encryption algorithm, distributed cache, Hadoop, Spark , Storm, YARN, machine learning and cloud computing are best used to check leaks and fill vacancies.

RabbitMQ, Hbase, MongoDB, Cassandra, design pattern, load balancing, database, consistency algorithm, JAVA algorithm, data structure, encryption algorithm, distributed cache, Hadoop, Spark, Storm, YARN, machine learning and cloud computing are best used to check leaks and fill deficiencies.

Keywords: Java Back-end Interview Programmer

Added by TitanKing on Fri, 24 Dec 2021 20:31:13 +0200