- 一个类中的成员变量如果是类所处下spring的其他bean,可以在配置文件中配置,这种操作叫做配置合作者bean
- 显示地配置合作者bean应当在property标签下使用标签来指定合作者bean,操作如下
- 程序结构
-
public class ExampleBean { private Test test; // ExampleBean类拥有Test类型的变量,并且还需要有test变量的set方法 public Test getTest() { return test; } public void setTest(Test test) { this.test = test; } }
public class Test { private String tea; public String getTea() { return tea; } public void setTea(String tea) { this.tea = tea; } }
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * 测试类,测试有没有被赋值成功 */ public class SpringTest { public static void main(String []args){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); ExampleBean exampleBean = (ExampleBean)applicationContext.getBean("exampleField",ExampleBean.class); Test test = (Test)applicationContext.getBean("test", Test.class); System.out.println(test.getTea()); System.out.println(exampleBean.getTest().getTea()); } }
<?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="test" class="Test"> <property name="tea" value="redTea"/> </bean> <bean id="exampleField" class="ExampleBean" > <property name="test" ref="test"/> <!--显示指定合作者bean--> </bean> </beans>
-
使用自动装配注入合作者bean,spring可以自动装配bean和bean之间的一来关系,从而无需使用ref显示指定,而是由spring检查xml配置文件的内容,根据以下两种规则,完成注入
-
byname:如果在bean A里面有setB()方法,而spring配置文件下刚好有一个id为b的bean,那么spring会把b的实例注入到bean A里边
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * 测试类,测试有没有被赋值成功 */ public class SpringTest { public static void main(String []args){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); ExampleBean exampleBean = (ExampleBean)applicationContext.getBean("exampleField",ExampleBean.class); System.out.println(exampleBean.getByNameTest().getTea()); } }
<?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="byNameTest" class="Test"> <!--spring里边有一个id为byNameTest的bean,它就会被注入到exampleField里边--> <property name="tea" value="redTea"/> </bean> <bean id="exampleField" class="ExampleBean" autowire="byName"> </bean> </beans>
public class ExampleBean { private Test byNameTest; // ExampleBean类拥有byNameTest类型的变量,并且还需要有test变量的set方法 public Test getByNameTest() { return byNameTest; } public void setByNameTest(Test byNameTest) { this.byNameTest = byNameTest; } }
-
byType方法,如果bena A有方法setB(B b),而xml配置文件里边刚好有个id为b的bean,那么spring会把bean a注入到bean a里边
public class ExampleBean { private Test test; public Test getTest() { return test; } //bean ExampleBean有setB(B b)方法,xml配置文件按有id为b的bean public void setTest(Test test) { this.test = test; } }
<?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="test" class="Test"> <!--spring里边有一个id为test的bean,它就会被注入到exampleField里边--> <property name="tea" value="redTea"/> </bean> <bean id="exampleField" class="ExampleBean" autowire="byType"/> </beans>
这是我看李刚编著的《轻量级javaEE企业应用实战(第五版)-Struts2+Spring5+Hibernate5/JAP2》后总结出来的。
-
Spring(7) 配置合作者Bean
最新推荐文章于 2025-06-18 02:20:06 发布