一、Bean设置基础类型
1.1、BookDao接口和实现类
java
public interface BookDao {
void save();
}
public class BookDaoImpl implements BookDao {
private int connectionNum;
private String databaseName;
public void setConnectionNum(int connectionNum) {
this.connectionNum = connectionNum;
}
public void setDatabaseName(String databaseName) {
this.databaseName = databaseName;
}
public void save(){
System.out.println("book dao save......" + databaseName+","+ connectionNum);
}
}
1.2、配置文件
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="bookDao" class="com.itheima.dao.impl.BookDaoImpl" scope="prototype">
<!-- 基本类型-->
<property name="databaseName" value="mysql"/>
<property name="connectionNum" value="10"/>
</bean>
</beans>
1.3、总结
- bean中基本数据类型是设置property中value属性
二、 Bean设置引用类型
2.1、BookService接口和实现类
java
public interface BookService {
void save();
}
public class BookServiceImpl implements BookService, InitializingBean, DisposableBean {
private BookDao bookDao;
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void save(){
// System.out.println("book service save..");
bookDao.save();
userDao.save();
}
// 提供对应的set方法
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
public void destroy() throws Exception {
System.out.println("service destroy");
}
public void afterPropertiesSet() throws Exception {
System.out.println("service init");
}
}
2.2、配置文件
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="bookDao" class="com.itheima.dao.impl.BookDaoImpl" scope="prototype">
<!-- 基本类型使用value-->
<property name="databaseName" value="mysql"/>
<property name="connectionNum" value="10"/>
</bean>
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl" scope="singleton"/>
<bean id="bookService" name="service" class="com.itheima.service.impl.BookServiceImpl">
<!-- 引用类型使用ref -->
<property name="bookDao" ref="bookDao"></property>
<property name="userDao" ref="userDao" />
</bean>
</beans>
2.3、总结
- 基本数据类型使用的是value
- 引用类型使用的是ref