引用类型
1、在bean中定义引用类型属性并提供可访问的set方法
public class BookServiceImpl implements BookService{
private BookDao bookDao;
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
}
配置中使用property标签ref属性注入引用类型对象 (references)
<bean id="bookService" class="com.demo.service.impl.BookServiceImpl">
<property name="bookDao" ref="bookDao"/>bean>
bean>
<bean id="bookDao" class="com.demo.dao.impl.BookDaoImpl"/>
简单类型
1、在bean中定义简单类型属性并提供可访问的set方法
public class BookDaoImpl implements BookDao {
private int number;
public void setNumber(int number) {
this.number= number;
}
}
2、配置中使用property标签value属性注入简单类型数据
<bean id="bookDao" class="com.demo.dao.impl.BookDaoImpl">
<property name="number" value="10"/>
bean>
引用类型
1、在bean中定义引用类型属性并提供可访问的构造方法
public class BookServiceImpl implements BookService{
private BookDao bookDao;
public BookServiceImpl(BookDao bookDao) {
this.bookDao = bookDao;
}
}
2、配置中使用constructor-arg标签ref属性注入引用类型对象
<bean id="bookService" class="com.demo.service.impl.BookServiceImpl">
<constructor-arg name="bookDao" ref="bookDao"/>
bean>
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"/>
简单注入
1、bean中定义简单类型的属性并提供构造方法方法
public class BookDaoImpl implements BookDao {
private int connectionNumber;
private String databaseName;
public BookDaoImpl(int connectionNumber, String databaseName) {
this.connectionNumber = connectionNumber;
this. databaseName = databaseName;
}
}
2、配置中使用constructor-arg标签value属性注入简单类型对象
<bean id="bookDao" class="com.demo.dao.impl.BookDaoImpl">
<constructor-arg name="connectionNumber" value="10"/>
bean>
自动装配概念
依赖自动装配
<bean id="bookDao" class="com.demo.dao.impl.BookDaoImpl"/>
<bean id="bookService" class="com.demo.service.impl.BookServiceImpl" autowire="byType"/>
依赖自动装配特征
<bean id="bookDao1" class="com.demo.dao.impl.BookDaoImpl">
<constructor-arg type="int" value="10"/>
<constructor-arg type="java.lang.String" value="mysql"/>
bean>
<bean id="bookDao" class="com.demo.dao.impl.BookDaoImpl">
<constructor-arg type="int" value="20"/>
<constructor-arg type="java.lang.String" value="oracle"/>
bean>
<bean id="bookService" class="com.demo.service.impl.BookServiceImpl" autowire="byName">
bean>
注入数组类型数据
<property name="array">
<array>
<value>100value>
<value>200value>
<value>300value>
array>
property>
注入List类型数据
<property name="list">
<list>
<value>itcastvalue>
<value>itheimavalue>
<value>boxueguvalue>
<value>chuanzhihuivalue>
list>
property>
注入Set类型数据
<property name="set">
<set>
<value>itcastvalue>
<value>itheimavalue>
<value>boxueguvalue>
<value>boxueguvalue>
set>
property>
注入Map类型数据
<property name="map">
<map>
<entry key="country" value="china"/>
<entry key="province" value="henan"/>
<entry key="city" value="kaifeng"/>
map>
property>
注入Properties类型数据
<property name="properties">
<props>
<prop key="country">chinaprop>
<prop key="province">henanprop>
<prop key="city">kaifengprop>
props>
property>
**说明:**property标签表示setter方式注入,构造方式注入constructor-arg标签内部也可以写、