热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

spring4笔记----spring4设值注入

2个接口packagecom.ij34.servce;publicinterfacepeople{publicvoidcut();}


2个接口

package com.ij34.servce;

public interface people {
       public void cut();
  
}
package com.ij34.servce;

public interface root {
  public String chop();
}

实现上面的接口

package com.ij34.web;

import com.ij34.servce.people;
import com.ij34.servce.root;

public class chinese implements people{
    private root dog;



    public root getDog() {
        return dog;
    }



    public void setDog(root dog) {
        this.dog = dog;
    }

 

package com.ij34.web;

import com.ij34.servce.root;

public class stonechop implements root{

    @Override
    public String chop() {
        // TODO Auto-generated method stub
        return "使用石头";
    }
  
}
package com.ij34.web;

import com.ij34.servce.root;

public class ironchop implements root{

    @Override
    public String chop() {
        // TODO Auto-generated method stub
        return "使用铁斧";
    }

}

beans

xml version="1.0" encoding="GBK"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">

 <bean id="chinese" class="com.ij34.web.chinese"> // 是chinese不是people
 <property name="dog" ref="iron">property>
 bean>
 <bean id="stone" class="com.ij34.web.stonechop">bean>
 <bean id="iron" class="com.ij34.web.ironchop">bean>
beans>

加载bean,实现调用实例

package com.ij34.bean;


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

import com.ij34.servce.people;

public class test {

    
    public static void main(String[] args) throws Exception{
        // TODO Auto-generated method stub
        @SuppressWarnings("resource")
        ApplicationContext app=new ClassPathXmlApplicationContext("beans.xml");
        people p= app.getBean("chinese",people.class);
         p.cut();
    }

}

 


推荐阅读
author-avatar
胡宸宸
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有