内部Bean的概念:在 Bean 中定义的 Bean 称为内部 Bean。内部 Bean 的定义不需要指定 id ,所以内部Bean总是匿名的,随外部Bean创建而创建。
注入内部Bean的示例
Person类代码如下。
package com.wen.pojo;
public class Person {
private Man man;
public Man getMan() {
return man;
}
public void setMan(Man man) {
System.out.println("在setMan方法内");
this.man = man;
}
public void man() {
man.show();
}
Man类代码如下。
package com.wen.pojo;
public class Man {
private String name;
private int age;
public Man() {
System.out.println("在man的无参构造函数内");
}
public Man(String name, int age) {
System.out.println("在man的有参构造函数内");
this.name = name;
this.age = age;
}
public void show() {
System.out.println("名称:" + name + "\n年龄:" + age);
}
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;
}
}
beans.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="person" class="com.wen.pojo.Person">
<!--定义内部Bean,类型为Man-->
<property name="man">
<bean class="com.wen.pojo.Man">
<property name="name" value="chendewen"/>
<property name="age" value="22"/>
</bean>
</property>
</bean>
</beans>
MyTest类代码如下。
import com.wen.pojo.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
Person person = (Person) context.getBean("person");
person.man();
}
}
运行结果如下。
D:\Application\jdk\jdk14\bin\java.exe "-javaagent:D:\Application\JetBrains\IntelliJ IDEA 2020.1.3\lib\idea_rt.jar=65304:D:\Application\JetBrains\IntelliJ IDEA 2020.1.3\bin" -Dfile.encoding=UTF-8 -classpath D:\program\spring-02\target\test-classes;D:\program\spring-02\target\classes;C:\Users\27207\.m2\repository\org\springframework\spring-webmvc\5.2.0.RELEASE\spring-webmvc-5.2.0.RELEASE.jar;C:\Users\27207\.m2\repository\org\springframework\spring-aop\5.2.0.RELEASE\spring-aop-5.2.0.RELEASE.jar;C:\Users\27207\.m2\repository\org\springframework\spring-beans\5.2.0.RELEASE\spring-beans-5.2.0.RELEASE.jar;C:\Users\27207\.m2\repository\org\springframework\spring-context\5.2.0.RELEASE\spring-context-5.2.0.RELEASE.jar;C:\Users\27207\.m2\repository\org\springframework\spring-core\5.2.0.RELEASE\spring-core-5.2.0.RELEASE.jar;C:\Users\27207\.m2\repository\org\springframework\spring-jcl\5.2.0.RELEASE\spring-jcl-5.2.0.RELEASE.jar;C:\Users\27207\.m2\repository\org\springframework\spring-expression\5.2.0.RELEASE\spring-expression-5.2.0.RELEASE.jar;C:\Users\27207\.m2\repository\org\springframework\spring-web\5.2.0.RELEASE\spring-web-5.2.0.RELEASE.jar MyTest
在man的构造函数内
在setMan方法内
名称:chendewen
年龄:22
Process finished with exit code 0
运行结果表明内部Bean已经创建,并且实现了注入内部Bean。