zoukankan      html  css  js  c++  java
  • Java笔记Spring(三)

    spring-beans和spring-context

    一、注解

    1、自定义一个注解

    @Target({ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyAnnotation {
        String value() default "";
    }

    2、使用注解

    public class MyClass {
        @MyAnnotation("注解参数")
        public String get() {
            return "呵呵";
        }
    }

    3、注解被调用,测试

    public class AnnotationTest {
        @Test
        public void get() throws NoSuchMethodException {
            MyAnnotation myAnnotation = MyClass.class.getDeclaredMethod("get", null).getAnnotation(MyAnnotation.class);
            System.out.println(myAnnotation.value());
        }
    }

    4、结果

    二、 Assert(断言,JDK1.4)

    public class AnnotationTest {
    
        @Test
        public void get() throws NoSuchMethodException {
            MyAnnotation myAnnotation = MyClass.class.getDeclaredMethod("get", null).getAnnotation(MyAnnotation.class);
            Assert.assertEquals(myAnnotation.value(), "注解参数");
            System.out.println("程序正常");
        }
    }

    三、Spring中的注解

    常用的:

    spring-context包下:

    @Component

    @Repository

    @Service

    @Controller

    @Bean

    @ComponentScan

    @Configuration

    @EnableScheduling

    @Scheduled

    spring-beans包下:

    @Autowired

    @Configurable

    @Qualifier

    @Value

  • 相关阅读:
    分享一份Java架构师学习资料,2019年最新整理!
    Spring Boot 最核心的 25 个注解,都是干货!
    推荐一款接口 API 设计神器!
    题库
    杂乱的知识点
    mysql查询疯狂41例
    mysql你问我答
    可能出现的面试题
    SQLALchemy
    基于蓝图的完整的Flask项目
  • 原文地址:https://www.cnblogs.com/tq1226112215/p/9071859.html
Copyright © 2011-2022 走看看