EDITORIAL words
Long time no update notes, someone will complain: Xiao Feng ah, you are not lazy ah, do not learn. Brother, really wrong: I feel that I am cooking, still trying to learn it, we are learning management system Vue.js do it. Even so, I still did not forget to update the Spring of knowledge, that does not come in yet?
IOC
I want to like Spring, so he helped me create an object, how this should be done?
1, class
package com.fengwenyi.learn.java.springioc;
import org.springframework.stereotype.Component;
/**
* @author Wenyi Feng
*/
@Component
public class Person {
private String name;
public Person() {}
public void sayHello() {
System.out.format("%s说:Hello.", name);
}
// getter and setter
}
2、Controller
package com.fengwenyi.learn.java.springioc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Wenyi Feng
*/
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
private Person person;
@GetMapping("/hello")
public String hello() {
person.setName("Wenyi Feng");
person.sayHello();
return "s";
}
}
3, browser access
4, the console
5, about the test
It is said that such a written test it?
Do not! I just like it, that's it.
AOP
1. Business
Here, I @Service
on behalf of the business we are dealing with: dinner
package com.fengwenyi.learn.java.springaop.service;
import org.springframework.stereotype.Service;
/**
* @author Wenyi Feng
*/
@Service
public class EatService {
public void eat() {
System.out.println("吃饭了");
}
}
2、AOP
Analysis: we need to wash their hands before meals, after dinner we have to wipe your mouth
package com.fengwenyi.learn.java.springaop;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
* @author Wenyi Feng
*/
@Component
@Aspect
public class Clean {
// @Pointcut("execution(* com.fengwenyi.learn.java.springaop.service..*.*(..))")
@Pointcut("execution(* com.fengwenyi.learn.java.springaop.service.EatService.eat())")
public void eat() {
}
/**
* 方法执行之前
*/
@Before("eat()")
public void doBefore() {
System.out.println("吃饭之前,吃手");
}
/**
* 方法执行之后
*/
@After("eat()")
public void doAfter() {
System.out.println("吃饭之后,擦嘴");
}
}
3, test code
package com.fengwenyi.learn.java.springaop;
import com.fengwenyi.learn.java.springaop.service.EatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Wenyi Feng
*/
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
private EatService eatService;
@GetMapping("/eat")
public String eat() {
eatService.eat();
return "s";
}
}
4, the browser requests
5, Console
annotation
1 comment
package com.fengwenyi.learn.java.restructure.ann;
import java.lang.annotation.*;
/**
* @author Wenyi Feng
*/
@Target({ElementType.METHOD, ElementType.TYPE}) // 方法 / 类 或者 接口 / [filed 字段]
@Retention(RetentionPolicy.RUNTIME) // 运行时
@Inherited // extends class 有效(接口 抽象类 都无效)
@Documented
public @interface Description {
String value();
}
2, the interface
package com.fengwenyi.learn.java.restructure.ann;
/**
* @author Wenyi Feng
*/
public interface Persion {
String name();
String age();
@Deprecated
void sind();
}
3, write a class that implements the interface, and use annotations
package com.fengwenyi.learn.java.restructure.ann;
/**
* @author Wenyi Feng
*/
@Description("Class Ann")
public class Child implements Persion {
@Override
@Description("Method Ann")
public String name() {
return null;
}
@Override
public String age() {
return null;
}
@Override
public void sind() {
}
}
4, parsing comment
package com.fengwenyi.learn.java.restructure.ann;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
/**
* @author Wenyi Feng
*/
public class PerseAnn {
public static void main(String[] args) {
try {
// 使用类加载器加载类
Class c = Class.forName("com.fengwenyi.learn.java.restructure.ann.Child");
// 找到类上的注解
boolean isExistClassAnn = c.isAnnotationPresent(Description.class);
if (isExistClassAnn) {
// 拿到注解实例
Description d = (Description) c.getAnnotation(Description.class);
System.out.println(d.value());
}
// 找到方法上的注解
Method[] methods = c.getMethods();
for (Method method : methods) {
boolean isExistMethodAnn = method.isAnnotationPresent(Description.class);
if (isExistMethodAnn) {
Description d = method.getAnnotation(Description.class);
System.out.println(d.value());
}
}
// 另一种解析方法
for (Method method : methods) {
Annotation [] annotations = method.getAnnotations();
for (Annotation annotation : annotations) {
if (annotation instanceof Description) {
Description d = (Description) annotation;
System.out.println(d.value());
}
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
5, the effect
data
This section of code has been uploaded to Github, click on the following project name, you can enter: JavaLearnProject
Reproduced in: https: //my.oschina.net/fengwenyi/blog/1834389