Spring AnnotationUtils注解公共Util
2018-01-18 16:11
162 查看
static findanotation(Method method, Class annotationType) Method:
通过传入AnnotatedElement和注解类型来查找方法或者类对象上的注解参数method是获取调用类的方法和annotation Type注解Class,例如下部分程序
this.persistentClass = persistentClass; Table table = AnnotationUtils.findAnnotation(persistentClass, Table.class);//获取类注解 if (table == null) {//假如获取不到则抛出异常 throw new DaoException(persistentClass.getName() + "没有定义@table"); } this.tableName = table == null ? persistentClass.getName() : table .name();//获取表名 BeanInfo beanInfo = null; try { beanInfo = Introspector.getBeanInfo(persistentClass); } catch (IntrospectionException e) { throw new DaoException(e); } PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();//获取此类所有方法 for (PropertyDescriptor pd : pds) {//循环所有方法 对我们需要的注解进行解析 Id id = AnnotationUtils .findAnnotation(pd.getReadMethod(), Id.class);//获标有Id注解的方法 if (id != null) { Column idColumn = AnnotationUtils.findAnnotation( pd.getReadMethod(), Column.class);//获取主键对应的数据名 if (idColumn != null) { pk = idColumn.name(); } else { pk = pd.getName(); } GeneratedValue gv = AnnotationUtils.findAnnotation( pd.getReadMethod(), GeneratedValue.class);//主键变化方式 if (gv == null) { strategy = GenerationType.IDENTITY; } else { strategy = gv.strategy(); } } Column column = AnnotationUtils.findAnnotation(pd.getReadMethod(), Column.class); property2ColumnMap.put(pd.getName(), column == null ? pd.getName() : column.name());//{code=code, createTime=create_time, class=class, account=account} column2PropertyMap .put(column == null ? pd.getName() : column.name(), pd.getName());//{code=code, create_time=createTime, class=class, account=account} Transient transient_ = AnnotationUtils.findAnnotation( pd.getReadMethod(), Transient.class);//transient临时字段 不对其进行解析操作 if (transient_ != null) { transientPropertys.add(pd.getName()); } } if ("".equals(this.getPk())) { throw new DaoException(persistentClass.getName() + "中没有在get方法上定义@Id"); }
package com.spring.server.entity; // Generated 2015-8-31 15:24:09 by Hibernate Tools 4.3.1 import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import static javax.persistence.GenerationType.IDENTITY; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; /** * TbWords generated by hbm2java */ @Entity @Table(name = "tb_words") public class TbWords implements java.io.Serializable { private Long twId; private String twCode; private String twName; private String twValue; private Date twAddDate; private Long twAddPerson; private Integer twStatus; public TbWords() { } public TbWords(String twCode, String twName, Integer twStatus) { this.twCode = twCode; this.twName = twName; this.twStatus = twStatus; } public TbWords(String twCode, String twName, String twValue, Date twAddDate, Long twAddPerson, Integer twStatus) { this.twCode = twCode; this.twName = twName; this.twValue = twValue; this.twAddDate = twAddDate; this.twAddPerson = twAddPerson; this.twStatus = twStatus; } @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "tw_id", unique = true, nullable = false) public Long getTwId() { return this.twId; } public void setTwId(Long twId) { this.twId = twId; } @Column(name = "tw_code", nullable = false, length = 10) public String getTwCode() { return this.twCode; } public void setTwCode(String twCode) { this.twCode = twCode; } @Column(name = "tw_name", nullable = false, length = 50) public String getTwName() { return this.twName; } public void setTwName(String twName) { this.twName = twName; } @Column(name = "tw_value", length = 10) public String getTwValue() { return this.twValue; } public void setTwValue(String twValue) { this.twValue = twValue; } @Temporal(TemporalType.TIMESTAMP) @Column(name = "tw_add_date", length = 19) public Date getTwAddDate() { return this.twAddDate; } public void setTwAddDate(Date twAddDate) { this.twAddDate = twAddDate; } @Column(name = "tw_add_person") public Long getTwAddPerson() { return this.twAddPerson; } public void setTwAddPerson(Long twAddPerson) { this.twAddPerson = twAddPerson; } @Column(name = "tw_status", nullable = false) public Integer getTwStatus() { return this.twStatus; } public void setTwStatus(Integer twStatus) { this.twStatus = twStatus; } }
相关文章推荐
- Spring注解配置定时任务<task:annotation-driven/>
- Spring学习3—控制反转(IOC)基于Annotation(注解)的依赖注入实现
- org.springframework.util.ClassUtils.getXXX错误解决方案
- Spring学习3—控制反转(IOC)基于Annotation(注解)的依赖注入实现
- spring注解工具类AnnotatedElementUtils和AnnotationUtils
- Java事务处理全解析(七)—— 像Spring一样使用Transactional注解(Annotation)
- Spring中提供的util CollectionUtils
- SpringAnnotation注解之@PreDestroy,@PostConstruct,@Scope
- 一个简单的基于注解的 Controller (spring 2.5 annotation 方式实现mvc )
- Spring 常用注入注解(annotation)和其对应xml标签
- org.springframework.util.StringUtils 使用
- Spring对注解(Annotation)处理源码分析2——解析和注入注解配置的资源
- Spring @Autowired注解在utils静态工具类非controller普通类中使
- Spring 用注解Annotation注入 abstract 抽象父类的属性
- Spring注解配置定时任务<task:annotation-driven/>
- Spring中提供的util CollectionUtils
- spring注解基于Annotation的依赖注入配置笔记
- spring 事务 @Transactional注解参数 tx:annotation-driven
- Spring 注解<context:annotation-config> 和 <context:component-scan>的作用与区别
- Spring 注解 Annotation