bean的依赖注入
【摘要】
使用构造函数
简单类型
public class BookDaoImpl implements BookDao {
private int connectionNum;
private...
使用构造函数
简单类型
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);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
<!-- 标准书写-->
<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>
- 1
- 2
- 3
- 4
- 5
引用类型
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();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
<bean id="bookService" class="com.lihuikang.springboot_03.service.Impl.BookServiceImpl">
<!-- name是构造放方法中的形式参数-->
<constructor-arg name="bookDao" ref="bookDao"></constructor-arg>
</bean>
- 1
- 2
- 3
- 4
- 5
使用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;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
简单类型使用的是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>
- 1
- 2
- 3
- 4
- 5
引用类型
public class BookServiceImpl implements BookService {
// 删除new对象
// private BookDao bookDao = new BookDaoImpl();
private BookDao bookDao;
// 提供set方法
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
<!-- 在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>
- 1
- 2
- 3
- 4
- 5
<bean id="bookService" class="com.lihuikang.springboot_02.service.impl.BookServiceImpl">
<!-- 配置service和dao的关系-->
<!-- name表示配置哪一个具体的bean,ref表示参照哪一个bean-->
<property name="bookDao" ref="bookDao"></property>
</bean>
- 1
- 2
- 3
- 4
- 5
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();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
自动装配
引用类型
<!-- 在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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
文章来源: blog.csdn.net,作者:花花叔叔,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq_52077949/article/details/126916486
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)