好久没更新笔记了,有人会抱怨:小冯啊,你是否是在偷懒啊,没有学习了。老哥,真的冤枉:我以为我本身很菜,还在努力学习呢,正在学习Vue.js作管理系统呢。即使这样,我仍是不忘更新Spring的知识,这不就来了吗?java
我想把类交给Spring,让他帮我建立对象,这应该怎么作?git
一、类github
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 }
二、Controllerweb
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"; } }
三、浏览器访问spring
四、控制台 app
五、关于测试ide
有人说,测试时这样写的吗?学习
不!我只是喜欢这样,仅此而已。测试
一、业务
在这里,我用 @Service
表明咱们要处理的业务: 吃饭
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("吃饭了"); } }
二、AOP
分析:吃饭以前咱们须要洗手,吃饭以后咱们要擦嘴
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("吃饭以后,擦嘴"); } }
三、测试代码
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"; } }
四、浏览器请求
五、控制台
一、注解
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(); }
二、接口
package com.fengwenyi.learn.java.restructure.ann; /** * @author Wenyi Feng */ public interface Persion { String name(); String age(); @Deprecated void sind(); }
三、写一个类实现接口,并使用注解
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() { } }
四、注解解析
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(); } } }
五、效果
本节代码已上传至Github,点击下面的工程名,便可进入:JavaLearnProject