Spring的内部Bean和注入内部Bean

内部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。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值