• spring自定义标签,功能与@required一样


    第一步:新建项目 SecondSpring

    项目结构如下:

    第二步:导入spring相对应的jar包

    过程略...

    第三步:创建@Mandatory 接口

    package com.xuzhiwen.spring92;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    public @interface Mandatory {
        
    }

    第四步:新建House类

    package com.xuzhiwen.spring92;
    
    public class House {
        
        private String name;
        private String address;
        
        @Mandatory
        public void setName(String name) {
            this.name = name;
        }
        
        @Mandatory
        public void setAddress(String address) {
            this.address = address;
        }
        
        @Override
        public String toString() {
            return "House [name=" + name + ", address=" + address + "]";
        }
    }

    第五步:新建spring配置文件

    common.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
        <import resource="xmlfolder/app1.xml" />
        <import resource="xmlfolder/innerbean.xml" />
        <import resource="xmlfolder/singleton.xml" />
        <import resource="xmlfolder/annotation.xml" />
        <import resource="xmlfolder/gather.xml" />
        <import resource="xmlfolder/date.xml" />
        <import resource="xmlfolder/db.xml" />
        <import resource="xmlfolder/parent.xml" />
        <import resource="xmlFile/abstract.xml" />
        <import resource="xmlFile/cat.xml" />
        <import resource="xmlFile/required.xml" />
        
        <import resource="xmlFile/mandatory.xml" />
    </beans>    

    mandatory.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
        
        
        <!-- 将@Mandatory注释到"RequiredAnnotationBeanPostProcessor"类中 -->
        <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor">
            <property name="requiredAnnotationType" value="com.xuzhiwen.spring92.Mandatory"/>
        </bean>    
        
        <bean id="house" class="com.xuzhiwen.spring92.House">
            <property name="name" value="tom" />
            <property name="address" value="china" />
        </bean>
        
    </beans>    

    第六步:新建测试类

    Test.java

    package com.xuzhiwen.spring92;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test {
        public static void main(String[] args) {
            ApplicationContext app = new ClassPathXmlApplicationContext("common.xml");
            House house = (House) app.getBean("house");
            System.out.println(house);
        }
    }

    第七步:运行结果如下

  • 相关阅读:
    IBatis简介
    cntlm代理使用
    bash快捷键你知道几个?
    django的Form中添加属性
    EMACS 中文显示为方框
    git合并子树
    算法 排序 python 实现堆排序
    android org.eclipse.wst.sse.core 0.0.0' but it could not be found
    我的EMACS配置
    python 输入# 自动跳到行首
  • 原文地址:https://www.cnblogs.com/beibidewomen/p/7411916.html
Copyright © 2020-2023  润新知