美文网首页
Spring IoC/DI 04-XML 通过静态工厂方法注入

Spring IoC/DI 04-XML 通过静态工厂方法注入

作者: 周老一员 | 来源:发表于2020-02-21 17:47 被阅读0次

    XML 通过静态工厂方法注入 Bean

    1. 创建一个 User 类和对应的工厂类 UserFactory

    User.java

    public class User {
        private String name;
        private int age;
    
        public User() {
            System.out.println("调用User类的无参构造方法");
        }
    
        public void sayHello(String name){
            System.out.println("hello, " + name + "!");
        }
    
        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;
        }
    }
    

    UserFactory.java

    public class UserFactory {
        public static User getInstance(){
            return new User();
        }
    }
    

    2. 在 Spring 配置文件通过静态工厂方法注入 User 的 Bean

    <bean class="org.daistudy.UserFactory" factory-method="getInstance" id="user2"/>
    

    3. 初始化 Spring 的 IoC 容器,并获取 User Bean

    User user = (User) context.getBean("user2");
    user.sayHello("王五");
    

    4. 适用场景及示例代码

    在一些第三方框架适用过程中,无法直接通过普通构造方法注入,则不得不适用静态工厂方式或实例工厂方法注入。例如,在使用 OkHttp 时,由于创建 OkHttpClient 对象时需要先创建一个 Builder 来设置相关属性,此时如果想注入 Bean 则不得不使用静态工厂方法注入。

    OkHttpClientFactory.java

    public class OkHttpClientFactory {
        public static OkHttpClient getInstance(){
            OkHttpClient okHttpClient = new OkHttpClient
                    .Builder()
                    .connectTimeout(5, TimeUnit.SECONDS)
                    .readTimeout(5, TimeUnit.SECONDS)
                    .build();
            return okHttpClient;
        }
    }
    

    静态工厂方法注入 OkHttpClient 的 Bean

    <bean class="org.daistudy.OkHttpClientFactory" factory-method="getInstance" id="okHttpClient"/>
    

    应用,下载一个图片

    public class DownloadImage {
        public static void main(String[] args) {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            OkHttpClient okHttpClient = (OkHttpClient) context.getBean("okHttpClient");
    
            Request request = new Request.Builder().url("http://pic.netbian.com/uploads/allimg/180826/113958-153525479855be.jpg").get().build();
            okHttpClient.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
    
                }
    
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    // save image
                    if (response.isSuccessful()) {
                        InputStream inputStream = response.body().byteStream();
                        FileOutputStream fileOutputStream = new FileOutputStream(new File("E:\\1.png"));
                        byte[] buf = new byte[1024];
                        int len = 0;
                        while((len = inputStream.read(buf)) != -1){
                            fileOutputStream.write(buf, 0, len);
                            fileOutputStream.flush();
                        }
                        fileOutputStream.close();
                        inputStream.close();
                    }
                }
            });
        }
    }
    
    

    相关文章

      网友评论

          本文标题:Spring IoC/DI 04-XML 通过静态工厂方法注入

          本文链接:https://www.haomeiwen.com/subject/akqhqhtx.html