zoukankan      html  css  js  c++  java
  • 3、spring注解注入

    1.写需要注解注入的类:

    Propertie.java

    package study;

    public class Propertie {

        public void show() {

            System.out.print("我是注解注入的!");

        }

    }

    2.Person接口:

    package study;
    public interface Person {
         public void output();
     }
    3.Chinese实现Person接口:
    package study;
    //必要的包
    import javax.annotation.Resource;

    public class Chinese implements Person {
            //注解注入符合@Resource
        @Resource private Propertie p;
        private String name;
            public void output() {
            p.show();
        }
    }
    4.测试类TestPerson.java
    package study;

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    public class TestPerson {

        /**
         * @param args
         */

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            ApplicationContext ctx = new ClassPathXmlApplicationContext("bean3.xml");
            Person p = (Person) ctx.getBean("chinese");
            p.output();
        }

    }
    5.bean3.xml配置文件
    <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-3.0.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-3.0.xsd"
    >
             <context:annotation-config/>
             <bean id="p" class="study.Propertie">
             </bean>
             <bean id="chinese" class="study.Chin
             ese"
    >
                  <property name="name">
                      <value>张敏鹏</value>
                  </property>
             </bean> 
    </beans>
       <!-- 
                          注解所引用的:
             xmlns:context="http://www.springframework.org/schema/context"
             xsi:schemaLocation=" http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
        -->

    6.运行结果:





  • 相关阅读:
    HihoCoder#1052:基因工程
    HihoCoder第十周:后序遍历
    HihoCoder第九周 状态压缩 二 与POJ2411总结
    [百度之星]资格赛:IP聚合
    HihoCoder第八周:状态压缩 一
    HihoCoder#1051:补提交卡
    HihoCoder#1039:字符消除
    HihoCoder第七周:完全背包问题
    HihoCoder第六周:01背包问题
    杭电2502--月之数
  • 原文地址:https://www.cnblogs.com/zmpandzmp/p/3648811.html
Copyright © 2011-2022 走看看