setter注入
要在类里提供dao对象的入口(一个set方法), 需要的类不要自己手动new
留一个空位
public class BookServiceImpl implements BookService {
//不要再直接new dao层的对象了!
private BookDao bookDao;
//提供dao对象进入的入口
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
@Override
public void bookSave() {
System.out.println("bookservice save...");
bookDao.save();
}
}
spring配置文件中需要指定去哪新建注入bean, 以及注入什么bean
<bean id="bookDao" name="dao booqidao" class="com.itheima.dao.impl.BookDaoImpl"></bean>
<bean id="bookService" class="com.itheima.service.impl.BookServiceImpl" scope="prototype">
<property name="bookDao" ref="booqidao"/>
</bean>
注入基本数据类型: 把ref换成value, 其他相同
构造器注入标准写法
首先预留bean进入的入口(构造方法)
package com.itheima.service.impl;
import com.itheima.dao.BookDao;
import com.itheima.dao.impl.BookDaoImpl;
import com.itheima.service.BookService;
public class BookServiceImpl implements BookService {
//不要再直接new dao层的对象了!
private BookDao bookDao;
//构造方法
public BookServiceImpl(BookDao bookDao) {
this.bookDao = bookDao;
}
@Override
public void bookSave() {
System.out.println("bookservice save...");
bookDao.save();
}
}
再修改配置文件
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"></bean>
<bean id="bookService" class="com.itheima.service.impl.BookServiceImpl" scope="prototype">
<constructor-arg name="bookDao" ref="bookDao"/>
</bean>
基本数据类型就把 ref 改成 value
构造器注入解耦写法
自动装配
最常用的就是byType
<bean id="bookService" class="com.itheima.service.impl.BookServiceImpl" scope="prototype" autowire="byType"/>
还有个byName, 但是bean的id一定要和setter的名字一样, 会造成耦合
<bean id="bookService" class="com.itheima.service.impl.BookServiceImpl" scope="prototype" autowire="byName"/>
集合注入
//首先bean里需要预留setter入口
public class YourClass {
private List<String> listProperty;
private Set<String> setProperty;
private Map<String, String> mapProperty;
private String[] arrayProperty;
// Setter方法
public void setListProperty(List<String> listProperty) {
this.listProperty = listProperty;
}
public void setSetProperty(Set<String> setProperty) {
this.setProperty = setProperty;
}
public void setMapProperty(Map<String, String> mapProperty) {
this.mapProperty = mapProperty;
}
public void setArrayProperty(String[] arrayProperty) {
this.arrayProperty = arrayProperty;
}
// Getter方法(视需要可选)
public List<String> getListProperty() {
return listProperty;
}
public Set<String> getSetProperty() {
return setProperty;
}
public Map<String, String> getMapProperty() {
return mapProperty;
}
public String[] getArrayProperty() {
return arrayProperty;
}
}
<bean id="exampleBean" class="com.example.YourClass">
<property name="listProperty">
<list>
<value>Item 1</value>
<value>Item 2</value>
< 3</value>
</list>
</property>
</bean>
<bean id="exampleBean" class="com.example.YourClass">
<property name="setProperty">
<set>
<value>Item A>Item B</value>
<value>Item C</value>
</set>
</property>
</bean>
<bean id="exampleBean" class="com.example.YourClass">
<property name="mapProperty">
<map>
<entry key="key1" value="value1" />
<entry key="key2" value="value2" />
<entry key="key3" value="value3" />
</map>
</property>
</bean>
<bean id="exampleBean" class="com.example.YourClass">
<property name="arrayProperty">
<array>
<value>Element 1</value>
<value>Element 2</value>
<value>Element 3</value>
</array>
</property>
</bean>
加载properties文件
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 解析properties文件-->
<context:property-placeholder location="classpath*:jdbc.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
</beans>