spring属性注入

一、构造方法注入

1、给Bean添加对应的构造方法。

public class User {
    private String username;
    private String address;
    private Integer id;
    public User(String username, String address, Integer id) {
            this.username = username;
            this.address = address;
            this.id = id;
            }
}

2、在xml文件中注入Bean

<?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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!--    构造方法注入-->
    <bean class="org.javaboy.ioc.model.User" id="user">
        <constructor-arg name="id" value="1"/>
        <constructor-arg name="username" value="javaboy"/>
        <constructor-arg name="address" value="www.javaboy.org"/>
    </bean>
</beans>

二、set方法注入(常用)

 <bean class="org.javaboy.ioc.model.User" id="user2">
        <property name="id" value="2"/>
        <property name="address" value="www.javaboy.org"/>
        <property name="username" value="大家早上好"/>
    </bean>

三、p名称注入

<!-- 通过p名称注入   -->
    <bean class="org.javaboy.ioc.model.User" id="user3" p:username="javaboy"
          p:id="3" p:address="itboyhub.com">
    </bean>

四、外部Bean注入

比如OkHttp原生写法:

首先在pom.xml添加依赖

<dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>5.0.0-alpha.2</version>
</dependency>
public class OkHttpTest {
    public static void main(String[] args) {
        OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
//        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//        OkHttpClient okHttpClient = ctx.getBean("okHttpClient", OkHttpClient.class);
        Request request = new Request.Builder()
                .get()
                .url("http://www.baidu.com")
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
//            失败
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                System.out.println("e.getMessage() = " + e.getMessage());
            }
//成功
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                System.out.println("response.body().string() = " + response.body().string());
            }
        });
    }
}

首先提供一个OkHttpClient的静态工厂

public class OkHttpStaticFactory {
    private static OkHttpClient okHttpClient;
    public static OkHttpClient getInstance(){
        if (okHttpClient == null) {
            okHttpClient=new OkHttpClient.Builder().build();
        }
        return okHttpClient;
    }
}

接着在xml文件中配置

    <bean class="org.javaboy.ioc.OkHttpStaticFactory" factory-method="getInstance" id="okHttpClient"/>

在java代码中获取实例就可以直接使用了

public class OkHttpTest {
    public static void main(String[] args) {
//        OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        OkHttpClient okHttpClient = ctx.getBean("okHttpClient", OkHttpClient.class);
        Request request = new Request.Builder()
                .get()
                .url("http://www.baidu.com")
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
//            失败
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                System.out.println("e.getMessage() = " + e.getMessage());
            }
//成功
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                System.out.println("response.body().string() = " + response.body().string());
            }
        });
    }
}

2、实例工厂注入

实例工厂类:

public class OkHttpFactory {
    private  OkHttpClient okHttpClient;

    public  OkHttpClient getInstance(){
        if (okHttpClient == null) {
            okHttpClient=new OkHttpClient.Builder().build();
        }
        return okHttpClient;
    }

}

在xml文件中提供工厂实例,然后才可以调用方法

<bean class="org.javaboy.ioc.OkHttpFactory" id="okHttpFactory"/>
    <bean class="okhttp3.OkHttpClient" factory-bean="okHttpFactory" factory-method="getInstance"
          id="okHttpClient"/>

ps.自己写的Bean一般不会使用这两种方式注入,需要引入外部jar时有可能需要使用这两种方式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值