使用构造函数
简单类型
public class BookDaoImpl implements BookDao { private int connectionNum; private String databaseName; public BookDaoImpl() { } public BookDaoImpl(int connectionNum, String databaseName) { this.connectionNum = connectionNum; this.databaseName = databaseName; } @Override public void save() { System.out.println("BookDao is save..." +connectionNum + databaseName); } }
<!-- 标准书写--> <bean id="bookDao" class="com.lihuikang.springboot_03.dao.Impl.BookDaoImpl"> <constructor-arg name="databaseName" value="mysql"></constructor-arg> <constructor-arg name="connectionNum" value="8888"></constructor-arg> </bean>
引用类型
public class BookServiceImpl implements BookService { private BookDao bookDao; public BookServiceImpl() { } public BookServiceImpl(BookDao bookDao) { this.bookDao = bookDao; } @Override public void save() { System.out.println("BookDaoService。。。"); bookDao.save(); } }
<bean id="bookService" class="com.lihuikang.springboot_03.service.Impl.BookServiceImpl"> <!-- name是构造放方法中的形式参数--> <constructor-arg name="bookDao" ref="bookDao"></constructor-arg> </bean>
使用set方法
简单类型
public class BookDaoImpl implements BookDao { private int connectionNum; private String databaseName; public int getConnectionNum() { return connectionNum; } public void setConnectionNum(int connectionNum) { this.connectionNum = connectionNum; } public String getDatabaseName() { return databaseName; } public void setDatabaseName(String databaseName) { this.databaseName = databaseName; } }
简单类型使用的是value属性
<!-- 在ioc容器中添加bean--> <bean id="bookDao" class="com.lihuikang.springboot_02.dao.impl.BookDaoImpl" > <property name="databaseName" value="mysql"></property> <property name="connectionNum" value="10"></property> </bean>
引用类型
public class BookServiceImpl implements BookService { // 删除new对象 // private BookDao bookDao = new BookDaoImpl(); private BookDao bookDao; // 提供set方法 public void setBookDao(BookDao bookDao) { this.bookDao = bookDao; } }
<!-- 在ioc容器中添加bean--> <bean id="bookDao" class="com.lihuikang.springboot_02.dao.impl.BookDaoImpl" > <property name="databaseName" value="mysql"></property> <property name="connectionNum" value="10"></property> </bean>
<bean id="bookService" class="com.lihuikang.springboot_02.service.impl.BookServiceImpl"> <!-- 配置service和dao的关系--> <!-- name表示配置哪一个具体的bean,ref表示参照哪一个bean--> <property name="bookDao" ref="bookDao"></property> </bean>
App类
public class App { public static void main(String[] args) { // 初始化容器 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); // 获取bean BookDao bookDao = (BookDao) ctx.getBean("bookDao"); bookDao.save(); BookService bookService = (BookService) ctx.getBean("bookService"); bookService.save(); } }
自动装配
引用类型
<!-- 在ioc容器中添加bean--> <bean id="bookDao" class="com.lihuikang.springboot_04.dao.impl.BookDaoImpl" > <property name="databaseName" value="mysql"></property> <property name="connectionNum" value="10"></property> </bean> <bean id="bookService" class="com.lihuikang.springboot_04.service.impl.BookServiceImpl" autowire="byType"> </bean>