spring ioc---bean之间的关系
1,继承;
2,依赖;
3,引用;
package entity; public class Dog { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
package entity; public class People { private int id; private String name; private int age; private Dog dog; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Dog getDog() { Dog dog=new Dog(); //生成新的狗 dog.setName("Jack"); return dog; } public void setDog(Dog dog) { this.dog = dog; } @Override public String toString() { return "People [id=" + id + ", name=" + name + ", age=" + age + ", dog=" + dog.getName() + "]"; } }
package entity; import java.lang.reflect.Method; import org.springframework.beans.factory.support.MethodReplacer; public class People2 implements MethodReplacer { @Override public Object reimplement(Object arg0, Method arg1, Object[] arg2) throws Throwable { Dog dog=new Dog(); dog.setName("Tom"); return dog; } }
package test; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.java1234.entity.People; public class T { private ApplicationContext ac; @Before public void setUp() throws Exception { ac=new ClassPathXmlApplicationContext("beans.xml"); } @Test public void test1() { People people=(People)ac.getBean("people1"); System.out.println(people.getDog().getName()); } }
<?xml version="1.0" encoding="UTF-8"?> <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.xsd"> <bean id="people1" class="entity.People"> <property name="id" value="1"></property> <property name="name" value="张三"></property> <property name="age" value="11"></property> <!-- 用的是people2的狗 --> <replaced-method name="getDog" replacer="people2"></replaced-method> </bean> <bean id="people2" class="entity.People2"></bean> </beans>
package entity; public class Dog { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
package entity; public class People { private int id; private String name; private int age; private String className; private Dog dog; public People() { System.out.println("初始化People"); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public Dog getDog() { return dog; } public void setDog(Dog dog) { this.dog = dog; } @Override public String toString() { return "People [id=" + id + ", name=" + name + ", age=" + age + ", className=" + className + ", dog=" + dog + "]"; } }
package service; public class Authority { public Authority() { System.out.println("获取权限"); } }
package test; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import entity.People; public class T { private ApplicationContext ac; @Before public void setUp() throws Exception { ac=new ClassPathXmlApplicationContext("beans.xml"); } @Test public void test1() { People zhangsan=(People)ac.getBean("zhangsan"); System.out.println(zhangsan); People lisi=(People)ac.getBean("lisi"); System.out.println(lisi); } }
<?xml version="1.0" encoding="UTF-8"?> <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.xsd"> <bean id="dog" class="entity.Dog"> <property name="name" value="jack"></property> </bean> <!-- 定义一个抽象的类 --> <bean id="abstractPeople" class="entity.People" abstract="true"> <!-- 这是他们的共同属性 --> <property name="className" value="高三5班"></property> <property name="age" value="19"></property> </bean> <!-- 继承abstractPeople。depends-on依赖属性,依赖autority,在初始化这个bean之前先初始化依赖属性 --> <bean id="zhangsan" parent="abstractPeople" depends-on="autority"> <!-- 自己的属性 --> <property name="id" value="1"></property> <property name="name" value="张三"></property> </bean> <!-- 继承abstractPeople --> <bean id="lisi" parent="abstractPeople"> <!-- 重写的属性 --> <property name="id" value="2"></property> <property name="name" value="李四"></property> <property name="age" value="20"></property> <!-- 引用属性 --> <property name="dog" ref="dog"></property> </bean> <bean id="autority" class="service.Authority"></bean> </beans>
                    
                
                
            
        
浙公网安备 33010602011771号