1 programming paradigm
1.1 process-oriented
1.2 Object Oriented
1.3 Aspect Oriented Programming
1.4 Functional Programming
1.5 event-driven programming
2 What is the Aspect Oriented Programming
2.1 is a programming paradigm, rather than a programming language
2.2 to solve some specific problems
2.3 As an object-oriented programming by
3 AOP produced the original intention
3.1 Code repeatability solve problems Do not Repeat Yourself
3.2 Separation of concerns to address issues Separation of Concerns
3.2.1 horizontal separation (technically division)
Control Layer -> Layer Services -> persistence layer
3.2.2 vertical separation (module division)
Module division
Separation section 3.2.3 (functionally divided)
Separation functional requirements and non-functional requirements
4 benefits of using AOP
4.1 focus on a particular point of concern, crosscutting logic
4.2 can easily add, delete, concerns
4.3 intrusive reduce, enhance code readability and maintainability
5 AOP usage scenarios
5.1 Access Control
Cache Control 5.2
5.3 transaction control
5.4 Audit Log
5.5 Performance Monitoring
5.6 Distributed Track
5.7 Exception Handling
6 achieve competence verification using traditional methods
6.1 Creating a project springBoot
Download: Click Go
6.2 Create a Product entity class
6.3 analog permission to create a new class
This class is mainly used to set user and obtain the user
cn.test.demo.base_demo.security Package Penalty for; / ** * @author Wang Yang Shuai * @Create 2018-04-29 17:15 * @desc simulate user logs in, the user can set the type of user and access ** / {class CurrentSetHolder public Private Final static the ThreadLocal <String> = new new Holder the ThreadLocal <> (); / ** * Get user * @return * / public static GET String () { return holder.get () == null "Unknown? ": holder.get (); } / ** * set the user * @param user * / public static void sET (String user) { holder.set (user); } }
6.4 Create a new class check permissions
This class is mainly used to determine whether the current user is "admin" user
cn.test.demo.base_demo.service Package Penalty for; Import cn.test.demo.base_demo.security.CurrentSetHolder; Import org.springframework.stereotype.Component; / ** * @author Wang Yang Shuai * @create 2018-04-29 17:19 * @desc permission to check the service class ** / @Component public class AuthService { / ** * permission to check if the user is not "admin" will error * / public void checkAccess () { String = CurrentSetHolder.get the user (); IF {( "ADMIN" .equals (User)!) the throw new new a RuntimeException ( "Not the allow Operation."); } } }
6.5 new class ProductService
This is potentially realize some operation on the Product
6.6 to create a service layer test class
The user set before calling the insert method of ProductService
7 permission to use AOP implementation verification
7.1 Create a new project springBoot
Download: Click Go
7.2 Creating a Product entity class
package cn.test.demo.base_demo.entity; /** * @author 王杨帅 * @create 2018-04-29 17:11 * @desc 商品实体类 **/ public class Product { private Integer id; private String name; public Product() { } public Product(Integer id, String name) { this.id = id; this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Product{" + "id=" + id + ", name='" + name + '\'' + '}'; } }
7.3 Creating a AdminOnly comment
package cn.test.demo.base_demo.security; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface AdminOnly { }
7.4 Create a privilege Simulators
This class is mainly used to set user and obtain the user
cn.test.demo.base_demo.security Package Penalty for; / ** * @author Wang Yang Shuai * @Create 2018-04-29 17:15 * @desc simulate user logs in, the user can set the type of user and access ** / {class CurrentSetHolder public Private Final static the ThreadLocal <String> = new new Holder the ThreadLocal <> (); / ** * Get user * @return * / public static GET String () { return holder.get () == null "Unknown? ": holder.get (); } / ** * set the user * @param user * / public static void sET (String user) { holder.set (user); } }
7.5 Creating a class section
This class is mainly for permission to check all annotated method used @AdminOnly
package cn.test.demo.base_demo.security; import cn.test.demo.base_demo.service.AuthService; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** * @author 王杨帅 * @create 2018-04-29 17:37 * @desc 权限检查的AOP类 **/ @Aspect @Component public class SecurityAspect { @Autowired AuthService authService; @Pointcut("@annotation(AdminOnly)") public void adminOnly(){ } @Before("adminOnly()") public void check(){ authService.checkAccess(); } }
7.6 Create a service class ProductService
delete method of the class with the @AdminOnly notes, so the method will delete permissions check
7.7 Create a test class
productService.delete (id) method have permission to verify, if not the "admin" user error will be implemented] [use AOP