Spring教程 - Spring内部Bean注入
2018-01-09 19:06 更新
Spring教程 - Spring内部Bean注入
以下部分显示如何使用Spring内部bean。
内在Java豆
在下面我们有Customer Bean。 在客户对象中我们有一个自定义对象Person。
package com.www.w3cschool.cnmon; public class Customer { private Person person; public Customer() { } public Customer(Person person) { this.person = person; } public void setPerson(Person person) { this.person = person; } @Override public String toString() { return "Customer [person=" + person + "]"; } }
这里是Person类的定义。
package com.www.w3cschool.cnmon; public class Person { private String name; private String address; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person [address=" + address + ",age=" + age + ", name=" + name + "]"; } }
参考内部Java Bean
将Person Java bean注入Customer bean的一种方法是使用ref属性,如下所示。
<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-2.5.xsd"> <bean id="CustomerBean" class="com.www.w3cschool.cnmon.Customer"> <property name="person" ref="PersonBean" /> </bean> <bean id="PersonBean" class="com.www.w3cschool.cnmon.Person"> <property name="name" value="java2s" /> <property name="address" value="address1" /> <property name="age" value="28" /> </bean> </beans>
Download Java2s_Spring_Ref_Attr.zip
Person bean在Customer bean中定义在同一个级别,我们使用ref属性引用Person bean。
如果Person bean只用在Customer bean中,我们可以嵌套Person的定义bean里面的Customer bean。
<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-2.5.xsd"> <bean id="CustomerBean" class="com.www.w3cschool.cnmon.Customer"> <property name="person"> <bean class="com.www.w3cschool.cnmon.Person"> <property name="name" value="java2s" /> <property name="address" value="address1" /> <property name="age" value="28" /> </bean> </property> </bean> </beans>
Download Java_Spring_Nest_Bean.zip
以下xml代码显示了如何使用内部bean进行构造函数注入。
<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-2.5.xsd"> <bean id="CustomerBean" class="com.www.w3cschool.cnmon.Customer"> <constructor-arg> <bean class="com.www.w3cschool.cnmon.Person"> <property name="name" value="java2s" /> <property name="address" value="address1" /> <property name="age" value="28" /> </bean> </constructor-arg> </bean> </beans>
Download Java2s_Spring_Inner_Bean_Constructor.zip
以下代码显示了如何运行上面的配置。
package com.www.w3cschool.cnmon; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"SpringBeans.xml"}); Customer cust = (Customer)context.getBean("CustomerBean"); System.out.println(cust); } }
输出
以上内容是否对您有帮助:
更多建议: