栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Bean的两种类型

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Bean的两种类型

1.普通Bean:在配置文件中定义bean类型就是返回类型

定义一个class

package com.hhm.spring5;

public class User {
    public void add(){
        System.out.println("add.......");
    }
}

定义xml


测试方法:

@Test
public void testAdd() {
    //1.加载spring配置文件
    ApplicationContext context =
            new ClassPathXmlApplicationContext("bean1.xml");
    User user = context.getBean("user", User.class);
    System.out.println(user);
    user.add();
}
2. 工厂Bean:在配置文件定义bean类型可以和返回类型不一样

新建 MyBean类

package com.hhm.spring5;

import org.springframework.beans.factory.FactoryBean;

public class MyBean implements FactoryBean {
    @Override
    public Object getObject() throws Exception {
        return null;
    }

    @Override
    public Class getObjectType() {
        return null;
    }

    @Override
    public boolean isSingleton() {
        return false;
    }
}

定义xml


测试方法:

@Test
public void testMyBean() {
    //1.加载spring配置文件
    ApplicationContext context =
            new ClassPathXmlApplicationContext("bean1.xml");
    MyBean mybean = context.getBean("mybean", MyBean.class);
    System.out.println(mybean);
}

发现会报错:
错误为:

org.springframework.beans.factory.BeanNotOfRequiredTypeException: 
Bean named 'mybean' is expected to be of type 
'com.hhm.spring5.MyBean' but was actually of type 
'org.springframework.beans.factory.support.NullBean'

意思是期望是 com.hhm.spring5.MyBean 可实际上是 org.springframework.beans.factory.support.NullBean

如果MyBean类的代码改成:

@Override
public Object getObject() throws Exception {
    return "hello";
}

又会报错为:

org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'mybean' is expected to be of type 'com.hhm.spring5.MyBean' but was actually of type 'java.lang.String'

期望的是 com.hhm.spring5.MyBean 结果实际上是 java.lang.String

也就是说这边会动态返回你指定的类型。
再次改造一下:注意接口中加入了字符串的类型

public class MyBean implements FactoryBean {
    @Override
    public String getObject() throws Exception {
        return "hello world!";
    }

测试类也需要定义返回的类型:

@Test
public void testMyBean() {
    //1.加载spring配置文件
    ApplicationContext context =
            new ClassPathXmlApplicationContext("bean1.xml");
    String mybean = context.getBean("mybean", String.class);
    System.out.println(mybean);
}

结果输出:

hello world!

============================================================================
其实按默认就好, return返回什么类型,测试的时候强转即可。

public class MyBean implements FactoryBean {
    @Override
    public Object getObject() throws Exception {
        return "hello world!";
    }
@Test
public void testMyBean() {
    //1.加载spring配置文件
    ApplicationContext context =
            new ClassPathXmlApplicationContext("bean1.xml");
    Object mybean = context.getBean("mybean", Object.class);
    System.out.println(mybean);
}

输出:

hello world!

或者:

@Override
public Object getObject() throws Exception {
    return new User(); //实际中不会这么创建
}
@Test
public void testMyBean() {
    //1.加载spring配置文件
    ApplicationContext context =
            new ClassPathXmlApplicationContext("bean1.xml");
    Object mybean = context.getBean("mybean", Object.class);
    User user = (User)mybean;
    user.add();
}

输出:

add.......
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/283983.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号