Spring的FactoryBean探秘2

继续之前的探测

今天新建了一个mvc项目,里面有一个接口Service1,里面有一个方法sayHello,两个实现类Service1Impl1和Service1Impl2


package com.dragon.springmvcdemo.service;

public interface Service1 {

    String sayHello();
}
package com.dragon.springmvcdemo.service;

public class Service1Impl1 implements Service1{
    @Override
    public String sayHello() {
        return "impl1";
    }
}
package com.dragon.springmvcdemo.service;

public class Service1Impl2 implements Service1{
    @Override
    public String sayHello() {
        return "impl2";
    }
}

 

 最简单的调用controller

package com.dragon.springmvcdemo;

import com.dragon.springmvcdemo.service.Service1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/controller")
public class HelloController {

    @Autowired
    Service1 service1;

    @RequestMapping("/sayhello")
    public String sayHello(){
//        return "++++++++++hello++++++++++++++";
        return service1.sayHello();
    }
}

 

springmvc配置文件 ,声明一个Service类型的Bean service1,首先指定为Service1Impl1

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <context:component-scan base-package="com.dragon.springmvcdemo"> </context:component-scan>
    <mvc:annotation-driven></mvc:annotation-driven>
    <bean id="srevice1" class="com.dragon.springmvcdemo.service.Service1Impl1"></bean>
</beans>

 

访问接口得到结果:impl1

 

然后写一个类继承FactoryBean,在getObject里返回Service1Impl2

package com.dragon.springmvcdemo.service;

import org.springframework.beans.factory.FactoryBean;

public class CommonBean implements FactoryBean<Service1> {
    @Override
    public Service1 getObject() throws Exception {
        return new Service1Impl2();
    }

    @Override
    public Class<?> getObjectType() {
        return Service1.class;
    }

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

 

修改配置文件为:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <context:component-scan base-package="com.dragon.springmvcdemo"> </context:component-scan>
    <mvc:annotation-driven></mvc:annotation-driven>
    <bean id="srevice1" class="com.dragon.springmvcdemo.service.CommonBean"></bean>
</beans>

 

启动成功不报错,并且访问结果返回了:Impl2

说明了是可以用实现了FactoryBean的类来创建任何类型的Bean

 

※扩展联想:1.可以结合动态代理进行功能加强 2.可以没有Service1的实现类,只有一个继承了FactoryBean的Bean来动态生成一个目标接口的类(可以用proxy动态代理),来达到没有实现类也能调用接口的目的(mybatis应该就是这么干的)

 

posted @ 2023-02-16 17:47  龙祭司  阅读(19)  评论(0)    收藏  举报