spring DI依赖注入
2021-04-14 12:29
标签:obb ram rri pre 标签 依赖注入 span oss release 1、官网 https://docs.spring.io/spring/docs/5.2.7.RELEASE/spring-framework-reference/core.html#beans-dependencies 2、构造器注入(前面有) 3、Set方式注入(重点) A、依赖 : bean对象 依赖 容器 创建 B、注入: bean对象的属性,由容器注入 https://docs.spring.io/spring/docs/5.2.7.RELEASE/spring-framework-reference/core.html#beans-factory-properties-detailed 案例 a、pojo Address.java Student.java b、xml文件 c、测试 4、其它方式注入 A、p命名空间 property xml格式 引入p="***" 例子 两个bean标签的效果是一样的 B、c命名空间(有含参构成函数) 构造器注入 xml文件 添加 c="*****" 例子 上述两种情况一致 spring DI依赖注入 标签:obb ram rri pre 标签 依赖注入 span oss release 原文地址:https://www.cnblogs.com/wt7018/p/13337300.htmlpackage com.wt.pojo;
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address=‘" + address + ‘\‘‘ +
‘}‘;
}
}
package com.wt.pojo;
import java.util.*;
public class Student {
private String name;
private Address address;
private String[] books;
private List
xml version="1.0" encoding="UTF-8"?>
beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
bean id="address" class="com.wt.pojo.Address">
property name="address" value="徐州大庙李井村"/>
bean>
bean id="user" class="com.wt.pojo.Student" name="student">
property name="name" value="吴通"/>
property name="address" ref="address"/>
property name="books">
array>
value>西游记value>
value>红楼梦value>
value>家value>
value>兄弟value>
array>
property>
property name="hobbys">
list>
value>看电影value>
value>敲代码value>
list>
property>
property name="card">
map>
entry key="银行卡" value="7458694756870">entry>
entry key="电话卡" value="13057122577">entry>
map>
property>
property name="games">
set>
value>WOWvalue>
value>鬼泣value>
set>
property>
property name="wife">
null/>
property>
property name="info">
props>
prop key="driver">driverprop>
prop key="url">urlprop>
prop key="port">3306prop>
prop key="user">rootprop>
props>
property>
bean>
beans>
import com.wt.pojo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void getStudent(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student userInfo = (Student) context.getBean("student");
System.out.println(userInfo.toString());
}
}
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
https://www.springframework.org/schema/beans/spring-beans.xsd">
beans>
bean id="user" class="com.wt.pojo.User">
property name="name" value="tom"/>
property name="age" value="18"/>
bean>
bean id="user2" class="com.wt.pojo.User" p:name="loss" p:age="34"/>
beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
beans>
bean id="user3" class="com.wt.pojo.User">
constructor-arg name="name" value="gg"/>
constructor-arg name="age" value="45"/>
bean>
bean id="user4" class="com.wt.pojo.User" c:name="死侍" c:age="100"/>