欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java Spring 工厂类 BeanFactoryAware基本使用 亲测

程序员文章站 2022-05-23 19:52:06
...

1. BaseFactory.java 实现 Spring工厂接口 BeanFactoryAware


import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.stereotype.Component;

@Component("baseFactory")
public class BaseFactory implements BeanFactoryAware {
    private BeanFactory beanFactory;

    public Object getService(String code) {
        return beanFactory.getBean(code);
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }

}

2. SearchFacade.java指定业务接口类


public interface SearchFacade {
    String doSearching(String keywords);
}

3. 业务处理实现类(BaiduImpl.java, GoogleImpl.java, DuckduckgoImpl.java)


import org.springframework.stereotype.Service;

@Service("BAIDU")
public class BaiduImpl implements SearchFacade {
    @Override
    public String doSearching(String keywords) {

        System.out.println("搜索条件: " + keywords);

        /**
         * 在此处一般调用一些第三方接口或爬虫 等!
         * */
        return "百度!!";
    }

}

import org.springframework.stereotype.Service;

@Service("GOOGLE")
public class GoogleImpl implements SearchFacade {
    @Override
    public String doSearching(String keywords) {

        System.out.println("搜索条件: " + keywords);

        /**
         * 在此处一般调用一些第三方接口或爬虫 等!
         * */
        return "谷歌!!";
    }

}

import org.springframework.stereotype.Service;

@Service("DUCKDUCKGO")
public class DuckduckgoImpl implements SearchFacade {
    @Override
    public String doSearching(String keywords) {

        System.out.println("搜索条件: " + keywords);

        /**
         * 在此处一般调用一些第三方接口或爬虫 等!
         * */
        return "鸭鸭去!!";
    }

}

4. FactoryController.java


@Controller
public class FactoryController {
    @Autowired
    private BaseFactory baseFactory;

    @GetMapping("/test2")
    public @ResponseBody Map<String, String> test2() {
        final Map<String, String> result = new HashMap<>();

        /**
         * 调用接口实现类实例
         * */
        final SearchFacade baiduInterface = (SearchFacade) baseFactory.getService("BAIDU");
        final String searchingFromBaidu = baiduInterface.doSearching("世界你好!");
        result.put("BAIDU", searchingFromBaidu);

        final SearchFacade googleInterface = (SearchFacade) baseFactory.getService("GOOGLE");
        final String searchingFromGoogle = googleInterface.doSearching("世界你好!");
        result.put("GOOGLE", searchingFromGoogle);

        final SearchFacade duckduckgoInterface = (SearchFacade) baseFactory.getService("DUCKDUCKGO");
        final String searchingFromDuckduckgo = duckduckgoInterface.doSearching("世界你好!");
        result.put("DUCKDUCKGO", searchingFromDuckduckgo);
        return result;
    }

}

  • http://127.0.0.1:8080/test2

{
	"DUCKDUCKGO": "鸭鸭去!!",
	"BAIDU": "百度!!",
	"GOOGLE": "谷歌!!"
}

如果您觉得有帮助,欢迎点赞哦 ~ 谢谢!!