Use XML bean assembly, another bean method call in the bean, the first class to build a Dog and a Cat class
Package Soundsystem; public class Dog {
Private String Cry; // call
// injection setter methods public void setCry (String Cry) { Cry = Cry; }
// define a dog method public void DogCry () { the System.out .println ( "dog:" + Cry); Cat.CatCry (); catEat.CatEating (); } }
Package Soundsystem; public class Cat { Private String Cry; // call
// Constructor injection public Cat (String Cry) { the this .Cry = Cry; } // define a method called cat
public void CatCry () { the System. out.println ( "cat called:" + Cry); } }
A configuration class Bean_DogXML.xml
<?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="soundsystem.Dog"> <property name="Cry" value="汪汪汪~"></property> <property name="Cat" ref="Cat"></property> </bean> <bean id="Cat" class="soundsystem.Cat"> <constructor-arg value="喵~"></constructor-arg> </bean> </beans>
Now start the test
package Test; import org.junit.runner.RunWith; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import soundsystem.Cat; import soundsystem.Dog; @RunWith(SpringJUnit4ClassRunner.class) public class Test { @org.junit.Test public static void main(String[] args) { ApplicationContext ap=new ClassPathXmlApplicationContext("config/Bean_DogXML.xml"); Dog dog=(Dog)ap.getBean("Dog"); dog.DogCry(); } }
Output: