[Reprint] spring refining the basic concepts

The basic concept of refined spring

HTTPS: // www.jianshu.com/p/3c30279d58cd 

jdk8.0 and annotation mode after spring5.0 have used java does not require the use of xml configuration files

and the spring switch to the principles agreed about configuration.

 

1. Interpretation of the concept

Inversion of Control: The original object is created by the new constructor to create an object becomes handed over to spring.
Dependency injection: spring properties of the object has been injected good value can be used directly.
Aspect Oriented Programming: The idea is that the function is divided into core business functions and peripheral functions and peripheral functions is defined as a section. During development, the core business functions and cut development function independently, and then 'knit' together again, this is called AOP.

2. Declare bean method

xml configuration file statement

<context:annotation-config/>//开启注解方式 <context:component-scan base-package="com.how2java.pojo"/>//扫描注解方式声明的bean <bean name="c" class="com.how2java.pojo.Category"> <property name="name" value="category 1" /> </bean> <bean name="p" class="com.how2java.pojo.Product"> <property name="name" value="product1" /> <!-- <property name="category" ref="c" /> --> </bean> 

Comment way to declare a bean

@Component("p")
public class Product { } //p就是此bean的名称 Product就是 class 

3. Properties way of injection

//这种方式会根据类型来注入Category
@Autowired
    private Category category; //会根据名称来注入 category @Resource private Category category; 

Example 4. AOP

spring boot added

<dependency>
    <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> 

Core business code

package com.how2java.service; @Component("s") public class ProductService { public void doSomeService(){ System.out.println("doSomeService"); } } 

section

@Aspect
@Component
public class LoggerAspect { // * 返回任意类型 (..) 参数是任意数量和类型 @Around(value = "execution(* com.how2java.service.ProductService.*(..))") public Object log(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("start log:" + joinPoint.getSignature().getName()); //执行核心业务代码 Object object = joinPoint.proceed(); System.out.println("end log:" + joinPoint.getSignature().getName()); return object; } } 

application.xml added

<context:component-scan base-package="com.how2java.aspect"/> <context:component-scan base-package="com.how2java.service"/> <aop:aspectj-autoproxy/> 

Use detailed dots, define pointcut
taken from  https://www.cnblogs.com/bigben0123/p/7779357.html

@Aspect  
@Component  
public class LogAspect { @Pointcut("execution(public * com.example.controller.*.*(..))") public void webLog(){} @Before("webLog()") public void deBefore(JoinPoint joinPoint) throws Throwable { // 接收到请求,记录请求内容 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); // 记录下请求内容 System.out.println("URL : " + request.getRequestURL().toString()); System.out.println("HTTP_METHOD : " + request.getMethod()); System.out.println("IP : " + request.getRemoteAddr()); System.out.println("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()); System.out.println("ARGS : " + Arrays.toString(joinPoint.getArgs())); } @AfterReturning(returning = "ret", pointcut = "webLog()") public void doAfterReturning(Object ret) throws Throwable { // 处理完请求,返回内容 System.out.println("方法的返回值 : " + ret); } //后置异常通知 @AfterThrowing("webLog()") public void throwss(JoinPoint jp){ System.out.println("方法异常时执行....."); } //后置最终通知,final增强,不管是抛出异常或者正常退出都会执行 @After("webLog()") public void after(JoinPoint jp){ System.out.println("方法最后执行....."); } //环绕通知,环绕增强,相当于MethodInterceptor @Around("webLog()") public Object arround(ProceedingJoinPoint pjp) { System.out.println("方法环绕start....."); try { Object o = pjp.proceed(); System.out.println("方法环绕proceed,结果是 :" + o); return o; } catch (Throwable e) { e.printStackTrace(); return null; } } }

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/11880838.html