1.构造器注入
上一章节已经说过了:
<?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="user" class="com.kuang.pojo.User">-->
<!-- <constructor-arg index="0" value="基基基基基基"/>-->
<!-- </bean>-->
<!-- 第二种方式:类型赋值,最好别用 -->
<!-- <bean id="user" class="com.kuang.pojo.User">-->
<!-- <constructor-arg type="java.lang.String" value="鸡哥"/>-->
<!-- </bean>-->
<bean id="userT" class="com.kuang.pojo.User"/>
<!-- 第三种方式:直接通过参数名来设置 -->
<bean id="user" class="com.kuang.pojo.User">
<constructor-arg name="name" value="鸡哥"/>
</bean>
<alias name="user" alias="sada"/>
</beans>
2.通过set方式注入【重点】
p对属性 c对构造器。。。
- 依赖注入:Set注入!
- 依赖:bean对象的创建依赖于容器!
- 注入:bean对象中的所有属性,由容器来注入!
【环境搭建】
1.复杂类型
package com.kuang.pojo;
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
- 2 真实对象
package com.kuang.pojo;
import java.util.*;
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbies;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobbies() {
return hobbies;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
public Map<String, String> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbies=" + hobbies +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
1.3 beans.xml
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="com.kuang.pojo.Student">
<!-- 第一种方式 :普通值注入,value -->
<property name="name" value="基基"/>
</bean>
</beans>
DI是实现控制反转的一种设计模式,依赖注入就是将实例变量传入到一个对象中去
1.4测试类
package mytest;
import com.kuang.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.getName());
}
}
完善注入信息
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.kuang.pojo.Address">
<property name="address" value="广西省"/>
</bean>
<bean id="student" class="com.kuang.pojo.Student">
<!-- 第一种方式 :普通值注入,value -->
<property name="name" value="基基"/>
<!-- 第二种方式 : Bean注入, ref -->
<property name="address" ref="address"/>
<!-- 数组注入 -->
<property name="books">
<array>
<value>红楼梦</value>
<value>水浒传</value>
<value>三国演义</value>
<value>西游记</value>
</array>
</property>
<!-- 集合注入 -->
<property name="hobbies">
<list>
<value>听歌</value>
<value>敲代码</value>
<value>看电影</value>
</list>
</property>
<!-- Map注入 -->
<property name="card">
<map>
<entry key="身份证" value="21312312312131"/>
<entry key="银行卡" value="42121312321312123"/>
</map>
</property>
<!-- Set注入 -->
<property name="games">
<set>
<value>LOL</value>
<value>COC</value>
<value>BOB</value>
</set>
</property>
<!-- null注入 -->
<property name="wife">
<null/>
</property>
<!-- Properties类注入 -->
<property name="info">
<props>
<prop key="driver">com.mysql.jdbc</prop>
<prop key="url">localhost:3306/?</prop>
<prop key="username">root</prop>
<prop key="password">2001</prop>
</props>
</property>
</bean>
</beans>
P命名空空间注入
对应着Property :Set注入
<?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">
<!-- P命名空间注入 Set方法注入 Properties -->
<bean id="user" class="com.kuang.pojo.User" p:name="秦疆" p:age="19" p:sponse-ref="user1"/>
<bean id="user1" class="com.kuang.pojo.User" p:name="花" p:age="21" />
</beans>
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("Userbeans.xml");
User user = context.getBean("user", User.class);
System.out.println(user);
}
C命名空空间注入
对应着construct :构造器注入
<?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:c="http://www.springframework.org/schema/c"
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">
<!-- C命名空间注入:construct-args -->
<bean id="user2" class="com.kuang.pojo.User" c:age="18" c:name="狂神" c:sponse-ref="user1"/>
<bean id="user1" class="com.kuang.pojo.User" p:name="花" p:age="21" />
</beans>
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("Userbeans1.xml");
User user = context.getBean("user2", User.class);
System.out.println(user);
}
3.拓展方式注入
我们可以使用P命名空间和C命名空间进行注入
<?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:c="http://www.springframework.org/schema/c"
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">
<!-- C命名空间注入:construct-args -->
<bean id="user2" class="com.kuang.pojo.User" c:age="18" c:name="狂神" c:sponse-ref="user1"/>
<bean id="user1" class="com.kuang.pojo.User" p:name="花" p:age="21" />
</beans>
<?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">
<!-- P命名空间注入 property set 方式注入 -->
<bean id="user" class="com.kuang.pojo.User" p:name="秦疆" p:age="19" p:sponse-ref="user1"/>
<bean id="user1" class="com.kuang.pojo.User" p:name="花" p:age="21" />
</beans>
p命名和C命名不能直接使用,需要导入xml约束以后才能使用
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"