spring 的使用流程
- 在 pom.xml 文件中到如 对应的依赖 这个是仅仅针对 bean 的依赖
1 2 3 4 5 6
| <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.6</version> </dependency>
|
创建配置文件 – 这里注意按 pom.xml 导入对应的 jar 包之后在创建会有对应的提示
一般创建名称为 applicationContext.xml , (别的名字也可以, 基本上只用一次 )
编写代码 获取 ioc 容器 , 就一行代码
1 2
| ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
|
- 编写不同的接口, 以及类的实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| public interface BookDao { public void save(); }
public interface BookServiceDao { public void save(); }
public class BookDaoImpl implements BookDao { @Override public void save() { System.out.println("book save"); } }
public class BookServiceImpl implements BookServiceDao {
private BookDao bookDao;
public void setBookDao(BookDao bookDao) { this.bookDao = bookDao; }
@Override public void save() { System.out.println("service save"); this.bookDao.save(); } }
|
- 编写 bean , 在 resource 里面创建 bean 的配置文件 applicationContext.xml
1 2 3 4 5 6 7 8 9 10
| <?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 https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="bookDao" class="com.yuelin.dao.impl.BookDaoImpl"/>
<bean id="service" class="com.yuelin.dao.impl.BookServiceImpl" /> </beans>
|
以上部分是 ioc
开始配置 doc 注入, 实现在 service 里面直接获取 bookDoc
1 2 3 4 5 6 7 8 9 10 11 12
| <?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 https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="bookDao" class="com.yuelin.dao.impl.BookDaoImpl"/>
<bean id="service" class="com.yuelin.dao.impl.BookServiceImpl" > <property name="bookDao" ref="bookDao" /> </bean> </beans>
|
- 在 service 里面实现 setter 方法
- 在配置里面的 设置 property 和 ref (引用)
实现构造的时候注入
- 引用注入
- 简单类型注入
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <?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 class="com.yuelin.impl.BookDaoImpl" id="bookDao" />
<bean class="com.yuelin.impl.BookServiceDaoImpl" id="bookServiceDao" >
<constructor-arg ref="bookDao" name="bookDao"/> <constructor-arg name="counts" value="10" /> </bean> </beans>
|
Author:
微笑城
Permalink:
http://www.yuelin.link/2023/03/13/java/srpingIoc/
License:
Copyright (c) 2019 CC-BY-NC-4.0 LICENSE
Slogan:
Do you believe in DESTINY?