自定义类似@Required功能的注解

自定义类似@Required功能的注解

   我们知道被@Required标注的方法是必须有值的,否则报异常,我们如何自定义了

  这种非常简单,只需要向RequiredAnnotationBeanPostProcessor类中注册即可

   我的自定义注解类是:

  

  package liusheng.dao;

  import java.lang.annotation.ElementType;
  import java.lang.annotation.Retention;
  import java.lang.annotation.RetentionPolicy;
  import java.lang.annotation.Target;

  @Retention(RetentionPolicy.RUNTIME)
  @Target(value=ElementType.METHOD)
  public @interface MyAnnotation {

  }

 配置文件是:

            

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
    <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor">
        <property name="requiredAnnotationType" value="liusheng.dao.MyAnnotation"/>
    </bean>

    

 
  

    <bean class="liusheng.dao.User">
    <property name="name" value="12"></property>
    <property name="password" value="12"></property>
  </bean>


<context:component-scan base-package="liusheng.dao"/> </beans>

  实体类是:

  

package liusheng.dao;

import org.springframework.beans.factory.annotation.Required;

public class User {
        
        private String name;
        
        private String password;
        public String getName() {
            return name;
        }
        @MyAnnotation
        public void setName(String name) {
            this.name = name;
        }
    
        public String getPassword() {
            return password;
        }
        @MyAnnotation
        public void setPassword(String password) {
            this.password = password;
        }
        public String toString() {
            return "User [name=" + name + ", password=" + password + "]";
        }
        
}

 注意:@Required与@MyAnnotation只有注解在方法上才有效,

 测试类:

  

package liusheng.dao;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/java/applicationContext.xml")

public class SanTest {
    @Autowired
    User user;
    @Test
    public void test(){
        System.out.println(user);
    }

}

 结果:

  

 

  

  虽然这样的用处不是很大,但是Spring中很多对象可以自定义,表示了Spring很灵活

    

    

转载于:https://www.cnblogs.com/SpringStudy/p/8598594.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值