作者:胡宸宸 | 来源:互联网 | 2023-08-19 14:06
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();
}
}