zoukankan      html  css  js  c++  java
  • 13_注解05_注解的继承

    【工程截图】

    【SuperPerson.java】

    package com.HigginCui.annotation;
    
    import org.springframework.stereotype.Component;
    
    @Component("superPerson")
    public class SuperPerson {
        public void superPersonSay(){
            System.out.println("super Person!!!");
        }
    }

    【Person.java】

    package com.HigginCui.annotation;
    
    import javax.annotation.Resource;
    import org.springframework.stereotype.Component;
    
    @Component("person")
    public class Person {
        @Resource(name="superPerson")
        private SuperPerson supperPerson;
        
        public void personSay(){
            this.supperPerson.superPersonSay();
        }
    }

    【Studnet.java】

    package com.HigginCui.annotation;
    
    import org.springframework.stereotype.Component;
    
    @Component("student")
    public class Student extends Person{
        public void studentSay(){
            this.personSay();
        }
    }

    【applicationContext.xml】

    <?xml version= "1.0" encoding ="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:context="http://www.springframework.org/schema/context"
           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
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
        <!-- 把一个类放入到Spring容器中,该类就是一个component,此时不需要声明对应的bean -->
        <context:component-scan base-package="com.HigginCui.annotation"></context:component-scan>
    </beans>

    【testPerson.java】

    package com.HigginCui.test;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import com.HigginCui.annotation.Student;
    
    public class testPerson {
        @Test
        public void test(){
            ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
            Student student=(Student) context.getBean("student");
            student.studentSay();
        }
    }

    【运行结果】

    super Person!!!
  • 相关阅读:
    SVN 怎么让文件脱离 版本控制
    WEB开发中使用和理解 .net中的认证与授权
    三层,师姐把我点透了
    三层与养猪,加入自己的理解。
    Asp.net的登录验证方法Web.config访问权限配置
    <%=%> 引发的aspx文件、.aspx.cs文件和.aspx.designer.cs的一些说明
    bin。obj Properties文件夹
    JS得到对应字段 的值。遍历
    C#中页面传值的方法。转载
    $ is not function   和JQUERY 命名 冲突的解说 Jquer问题 (
  • 原文地址:https://www.cnblogs.com/HigginCui/p/5579297.html
Copyright © 2011-2022 走看看