J2EE之java的注解
例子:
JDK自带的注解:
Person.java:
package com.ann.test;
public interface Person {
public String name();
public int age();
@Deprecated
//表示方法过时
public void sing();
}
Child.java:
package com.ann.test;
public class Child implements Person{
@Override
// 表示被重写
public String name() {
// TODO Auto-generated method stub
return null;
}
@Override
public int age() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void sing() {
// TODO Auto-generated method stub
}
}
Test.java:
package com.ann.test;
public class Test {
// @SuppressWarnings("deprecation"):表示忽略警告
@SuppressWarnings("deprecation")
public void sing(){
Person p=new Child();
//表示sing()方法已经过时了
p.sing();
}
}
常见的第三方注解的优势,以@Autowired注解为例:
UserManagerImpl.java:
package com.ann.test;
//类的实现
public class UserManagerImpl implements UserManager{
private UserDao userDao;
public void setUserDao(UserDao userDao){
this.userDao=userDao;
}
....
}
//配置文件
<bean id="userManagerImpl" class="com.ann.test.UserManagerImpl">
<property name="userDao" ref="userDao" />
</bean>
<bean id="UserManager" class="com.ann.test.UserManager">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>
//引入@Autowired注解
public class UserManagerImpl implements UserManager{
@Autowired
private UserDao userDao;
...
}
使用第三方注解会使代码变得简洁
注解的分类:
自定义注解:
自定义注解的说明:
元注解的说明:
使用自定义注解:
代码:
Description.java:
package com.ann.test;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//METHOD:指定方法就只能用在方法上,TYPE:指定类只能用在类上
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Description {
// 两个参数
// String desc();
// String author();
// int age() default 18;
String value();
}
Child.java:
package com.ann.test;
public class Child implements Person{
@Override
// @Description(desc="the name method" ,author="mooc boy") 一个参数
@Description("mooc boy")
// 表示被重写
public String name() {
// TODO Auto-generated method stub
return null;
}
@Override
public int age() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void sing() {
// TODO Auto-generated method stub
}
}
**
解析注解:
**
代码:
Person.java:
package com.ann.test;
@Description("i am interface")//会被继承
public interface Person {
@Description("i am interface method")//不会被继承
public String name();
public int age();
@Deprecated
//表示方法过时
public void sing();
}
Child.java:
package com.ann.test;
//@Description("I am class annotation")
public class Child implements Person{
@Override
// @Description(desc="the name method" ,author="mooc boy") 一个参数
// @Description("I am method annotation")
// 表示被重写
public String name() {
// TODO Auto-generated method stub
return null;
}
@Override
public int age() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void sing() {
// TODO Auto-generated method stub
}
}
Description.java:
package com.ann.test;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//METHOD:指定方法就只能用在方法上,TYPE:指定类只能用在类上
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
//@Retention(RetentionPolicy.SOURCE)源码的时候存在,编译后看不在注解
//@Retention(RetentionPolicy.CLASS)也取不到
@Inherited//继承
@Documented
public @interface Description {
// 两个参数
// String desc();
// String author();
// int age() default 18;
String value();
}
PersonAnn.java:
package com.ann.test;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
public class PersonAnn {
public static void main(String[] args) {
//1.使用类加载器加载类
try {
Class c=Class.forName("com.ann.test.Child");
//2.找到类上面的注解
boolean isExist=c.isAnnotationPresent(Description.class);
if(isExist){
//3.拿到注解实例
Description d=(Description)c.getAnnotation(Description.class);
System.out.println(d.value());
}
//4.找到方法上的注解
Method[] ms=c.getMethods();
for (Method method : ms) {
boolean isMExist=method.isAnnotationPresent(Description.class);
if(isMExist){
Description d=(Description)method.getAnnotation(Description.class);
System.out.println(d.value());
}
}
//另外一种解析方法
for (Method method : ms) {
Annotation[] as=method.getAnnotations();
for (Annotation annotation : as) {
if(annotation instanceof Description){
Description d=(Description)annotation;
System.out.println(d.value());
}
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}