1. Tres tipos de propiedades de objeto de inyección de dependencia
establecer inyección , inyección de constructor e inyección de espacio de nombres
<?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 id="UserDao" class="com.itheima.ioc.UserDaoImpl"/>
<bean id="teacher" class="com.itheima.entity.Teacher">
<!--<property name="name" value="张老师"></property>-->
<!--<property name="age" value="13"></property>-->
<constructor-arg value="张老师"></constructor-arg>
<constructor-arg value="13"></constructor-arg>
</bean>
<bean id="course" class="com.itheima.entity.Course" p:courseHour="2" p:courseName="数学" p:teacher-ref="teacher">
<!--<!–相当于person.setId(1);–>-->
<!--<property name="courseHour" value="2"></property>-->
<!--<property name="courseName" value="数学"></property>-->
<!--<!–非简单类型用ref–>-->
<!--<property name="teacher" ref="teacher"></property>-->
</bean>
</beans>
2. Varios tipos de inyección
Nueva clase: AllCollectionType.java
package com.itheima.entity;
import java.util.*;
public class AllCollectionType {
private List<String> list;
private String[] array;
private Set<String> set;
private Map<String, String> map;
private Properties props;
@Override
public String toString() {
return "AllCollectionType{" +
"list=" + list +
", array=" + Arrays.toString(array) +
", set=" + set +
", map=" + map +
", props=" + props +
'}';
}
public AllCollectionType(List<String> list, String[] array, Set<String> set, Map<String, String> map, Properties props) {
this.list = list;
this.array = array;
this.set = set;
this.map = map;
this.props = props;
}
public AllCollectionType() {
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public String[] getArray() {
return array;
}
public void setArray(String[] array) {
this.array = array;
}
public Set<String> getSet() {
return set;
}
public void setSet(Set<String> set) {
this.set = set;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public Properties getProps() {
return props;
}
public void setProps(Properties props) {
this.props = props;
}
}
Inyección de tipo Lista, Matriz, Conjunto, Mapa y Propiedades .
<bean id="collectionDemo" class="com.itheima.entity.AllCollectionType">
<property name="list">
<list>
<value>list足球</value>
<value>list篮球</value>
<value>list乒乓球</value>
</list>
</property>
<property name="array">
<array>
<value>数组足球</value>
<value>数组篮球</value>
<value>数组乒乓球</value>
</array>
</property>
<property name="set">
<set>
<value>map足球</value>
<value>map篮球</value>
<value>map乒乓球</value>
</set>
</property>
<property name="map">
<map>
<entry>
<key><value>foot</value></key>
<value>map足球</value>
</entry>
<entry>
<key><value>bask</value></key>
<value>mao篮球</value>
</entry>
<entry>
<key><value>pingpang</value></key>
<value>map乒乓球</value>
</entry>
</map>
</property>
<property name="props">
<props>
<prop key="foot">prop足球</prop>
<prop key="bask">prop篮球</prop>
<prop key="pingpang">prop乒乓球</prop>
</props>
</property>
</bean>
3. Inyección de valor especial
<value/>和<value></value>的区别
Cita de caracteres especiales de uso común:
赋空值直接写<null>标签
4. Montaje automático (solo apto para tipos de referencia de referencia)
La convención es mejor que la configuración, no apta para inyección de tipo simple.
Montaje automático según id (autowire = "byName").
Montaje automático según tipo. Solo se puede utilizar uno de ellos. Tipo (autowire = "byType")
Montaje automático según constructor (autowire = " constructor")
<bean id="teacher" class="com.itheima.entity.Teacher">
<!--<property name="name" value="张老师"></property>-->
<!--<property name="age" value="13"></property>-->
<constructor-arg value="张老师"></constructor-arg>
<constructor-arg value="13"></constructor-arg>
</bean>
<bean id="course" class="com.itheima.entity.Course" autowire="byName">
<!--<bean id="course" class="com.itheima.entity.Course" p:courseHour="2" p:courseName="数学" p:teacher-ref="teacher">-->
<!--相当于person.setId(1);-->
<property name="courseHour" value="2"></property>
<property name="courseName" value="数学"></property>
<!--非简单类型用ref-->
<!--<property name="teacher" ref="teacher"></property>-->
</bean>
Montaje automático global Montaje
automático local
5. Agregue anotaciones al contenedor ioc
Cree una nueva clase de dao:
StudentDaoImpl.java @Component () es una anotación general, que se puede refinar en lo siguiente:
anotación de la capa de dao: @Repository
service layer annotation: @Service
controller layer annotation: @ControllerConfiguration
Scanner