Java 5.0 基础 - Annotation

本文深入讲解Java注解的定义与使用方法,包括元注解、标记注解等,并通过实例展示如何利用注解映射JavaBean与数据库表的关系。

Reference Doc,

Official Site: http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html

优快云 Site: http://blog.youkuaiyun.com/numberpig/archive/2006/08/18/1095144.aspx

 

Quick Start

 Example: we want to map the JavaBean user with the DB2 table USER

 1) TableInfo Annotation

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface TableInfo {
	String value(); // Element declaration
}

2)  FieldInfo Annotation

// set to Runtime, then we can load those Annotation info by jave reflection
@Retention(RetentionPolicy.RUNTIME)  
@Documented  
public @interface FieldInfo {  
    String FieldName();  
    Class<?> FieldType();
    String FieldOper();
}  

3) JavaBean User.java

@TableInfo("USER")
public class User {
	private String name = null;
	private Date birth = null;
	
	@FieldInfo(FieldName="USERNAME", FieldType=String.class, FieldOper="SET")
	public void setName(String name){
		this.name = name;
	}
	@FieldInfo(FieldName="USERNAME", FieldType=String.class, FieldOper="GET")
	public String getName(){
		return this.name;
	}
	
	@FieldInfo(FieldName="BIRTHDAY", FieldType=Date.class, FieldOper="SET")
	public void setBirth(Date date){
		this.birth = date;
	}
	
	@FieldInfo(FieldName="BIRTHDAY", FieldType=Date.class, FieldOper="GET")
	public Date getBirth(Date date){
		return this.birth;
	}
}

3) Test get Annotation Info  

@Test
public void UserInfoTest() throws ClassNotFoundException, IllegalAccessException, InstantiationException{
  String className = "annotation.User";		
  Class<User> urClass = User.class;
  // for type safe
  User u = urClass.cast( Class.forName(className).newInstance() );
	
  if ( u.getClass().isAnnotationPresent( TableInfo.class ) ){
  	TableInfo tf = urClass.getAnnotation( TableInfo.class );
  	assertTrue("DB table is USER", "USER".equalsIgnoreCase(tf.value()));
  }
		
  Method[] methods = urClass.getMethods();
  for(Method m : methods){
  	FieldInfo finfo = null;
	if(m.isAnnotationPresent( FieldInfo.class ) && m.getName().indexOf("Name") >= 0 ){
	  finfo = m.getAnnotation( FieldInfo.class );
	  assertTrue("DB Field Name is USERNAME", "USERNAME".equals(finfo.FieldName()));
	  assertTrue("DB varchar mapping with String", String.class == finfo.FieldType() );
	  if(m.getName().equals("getName")) 
	     assertTrue("Operation is Set", "GET".equals(finfo.FieldOper()));
	  if(m.getName().equals("setName")) 
	     assertTrue("Operation is Get", "SET".equals(finfo.FieldOper()));
	}
		
	if(m.isAnnotationPresent( FieldInfo.class ) && m.getName().indexOf("Birth") >= 0 ){
	  finfo = m.getAnnotation( FieldInfo.class );
	  assertTrue("DB Field Name is BIRTHDAY", "BIRTHDAY".equals(finfo.FieldName()));
	  assertTrue("DB Date mapping with Date", Date.class == finfo.FieldType() );
	  if(m.getName().equals("getBirth")) 
	      assertTrue("Operation is Set", "GET".equals(finfo.FieldOper()));
	  if(m.getName().equals("SetBirth")) 
	      assertTrue("Operation is Get", "SET".equals(finfo.FieldOper()));
	}
    }
}

 

Annotation Defination,

Annotation Type

/**
 * Describes the Request-For-Enhancement(RFE) that led
 * to the presence of the annotated API element.
 */
public @interface RequestForEnhancement {
    int    id(); // annotation element declearation, id is the name, int is type
    String synopsis();
    String engineer() default "[unassigned]"; 
    String date();    default "[unimplemented]"; 
}

Annotation type declarations are similar to normal interface declarations

a. Each method declaration defines an element of the annotation type.

b. Method declarations must not have any parameters or a throws clause

c. Return types are restricted to primitives, String, Class, enums, annotations, and arrays of the preceding types 

d. Methods can have default values

Annotation

@RequestForEnhancement(
    id = 2868724 // annotate the element declaration
    synopsis = "Enable time-travel",
    engineer = "Mr. Peabody",
    date     = "4/1/3007"
)
public static void travelThroughTime(Date destination) { ... }

An annotation is a special kind of modifier, and can be used anywhere that other modifiers (such as public, static, or final) can be used, and precede other modifiers

a.  Annotations consist of an at-sign (@) followed by an annotation type and a parenthesized list of element-value pairs

b. The values must be compile-time constants.

Marker_Annotation_Type

The Annotation Type with no elements

/**
 * Indicates that the specification of the annotated API element
 * is preliminary and subject to change.
 */
public @interface Preliminary { }
 

 Meta-Annotation

 Meta-Annotation is a special kind of Annotation to declare the Annotation Type

 1. Target Meta-Annotation

     Target Annotation Type Source Code 

@Documented  // Copy the message of this Annotation Type into Java API
@Retention(RetentionPolicy.RUNTIME)  
@Target(ElementType.ANNOTATION_TYPE)  
public @interface Target {  
       ElementType[] value();  
}  

      ElementType Source code     

 public enum ElementType {  
  TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR,  
  LOCAL_VARIABLE, ANNOTATION_TYPE,PACKAGE  
 }  

      it defines the following,

         a. Defined the effective scale(On Method, FIELD......) of the annotation declared  by Target.

      Example     

 @Target(ElementType.METHOD)  
 @Retention(RetentionPolicy.RUNTIME)  
 @Documented  
 public @interface Name {  
     String originate();  
     String community();  
 }  

     The Annotation "Name" can only effect on Method level

     Focus that, if the Annotation didn't specify the Target, the Annotation effect on any level declare in ElementType

 2Rentation Meta-Annotation

     Rentation Source Code

 @Documented  
 @Retention(RetentionPolicy.RUNTIME)  
 @Target(ElementType.ANNOTATION_TYPE)  
 public @interface Retention {  
    RetentionPolicy value();  
 }  

     RetationPolicy Source Code  

 public enum RetentionPolicy {  
  SOURCE,  
  CLASS,  
  RUNTIME  
 }  

     It defines the annotation active scare,

     a.  SOURCE - the annotation only retained in Source code, after complie, it will lost

     b.  CLASS    - the annotation retained in both Souce code and Classes

     c.   RUNTIM - the annotaion reatined in both above, and it will be loaded into JVM. Then we can use the Reflection to retrieve the annotation info.

【电动汽车充电站有序充电调度的分散式优化】基于蒙特卡诺和拉格朗日的电动汽车优化调度(分时电价调度)(Matlab代码实现)内容概要:本文介绍了基于蒙特卡洛和拉格朗日方法的电动汽车充电站有序充电调度优化方案,重点在于采用分散式优化策略应对分时电价机制下的充电需求管理。通过构建数学模型,结合不确定性因素如用户充电行为和电网负荷波动,利用蒙特卡洛模拟生成大量场景,并运用拉格朗日松弛法对复杂问题进行分解求解,从而实现全局最优或近似最优的充电调度计划。该方法有效降低了电网峰值负荷压力,提升了充电站运营效率与经济效益,同时兼顾用户充电便利性。 适合人群:具备一定电力系统、优化算法和Matlab编程基础的高校研究生、科研人员及从事智能电网、电动汽车相关领域的工程技术人员。 使用场景及目标:①应用于电动汽车充电站的日常运营管理,优化充电负荷分布;②服务于城市智能交通系统规划,提升电网与交通系统的协同水平;③作为学术研究案例,用于验证分散式优化算法在复杂能源系统中的有效性。 阅读建议:建议读者结合Matlab代码实现部分,深入理解蒙特卡洛模拟与拉格朗日松弛法的具体实施步骤,重点关注场景生成、约束处理与迭代收敛过程,以便在实际项目中灵活应用与改进。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值