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
本文版权归作者和博客园共有,来源网址:http://www.cnblogs.com/tq1226112215/
欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。