其他分享
首页 > 其他分享> > Spring之IOC(二)

Spring之IOC(二)

作者:互联网

一、IOC操作 Bean 管理(FactoryBean)

public class Course {
    private String cname;

    public void setCname(String cname) {
        this.cname = cname;
    }
}

Mybean 类:

public class MyBean implements FactoryBean<Course> {
    @Override
    public Course getObject() throws Exception {
        Course course = new Course();
        course.setCname("JAVA");
        return course;
    }

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

    @Override
    public boolean isSingleton() {
        return FactoryBean.super.isSingleton();
    }
}

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="myBean" class="com.company.MyBean"></bean>
</beans>

标签:配置文件,Spring,Course,bean,FactoryBean,cname,IOC,public
来源: https://blog.csdn.net/dingd1234/article/details/121366930