一、构造方法注入
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时有可能需要使用这两种方式