01Spring基于xml的IOC配置--入门
01Spring基于xml的IOC配置
1、创建一个普通的maven工程
1.1 选择maven,不用骨架,点击下一步。
1.2 填写GroupId、ArtifactId、Version。填完点击下一步
1.3 确认项目路径,确认完成点击finish
2、模拟三层创建包和类
2.1 创建service接口
package com.yuank.service; public interface IAccountService { /** * 模拟保存账户 */ void saveAccount(); }
2.2 创建service接口的实现类
package com.yuank.service.impl; import com.yuank.dao.IAccountDao; import com.yuank.dao.impl.AccountDaoImpl; import com.yuank.service.IAccountService; /** * @author PC-Black * @version v1.0 * @date 2019/7/21 21:33 * @description TODO **/ public class AccountServiceImpl implements IAccountService { //手动初始化 private IAccountDao accountDao = new AccountDaoImpl(); public void saveAccount() { accountDao.saveAccount(); } }
2.3 创建dao持久层接口
package com.yuank.dao; public interface IAccountDao { void saveAccount(); }
2.4 创建dao持久层实现类
package com.yuank.dao.impl; import com.yuank.dao.IAccountDao; /** * @author PC-Black * @version v1.0 * @date 2019/7/21 21:38 * @description TODO **/ public class AccountDaoImpl implements IAccountDao { public void saveAccount() { System.out.println("保存账户"); } }
2.5 创建web表现层
import com.yuank.service.IAccountService; import com.yuank.service.impl.AccountServiceImpl; /** * @author PC-Black * @version v1.0 * @date 2019/7/21 21:39 * @description TODO **/ public class Client { public static void main(String[] args) { IAccountService accountService = new AccountServiceImpl(); accountService.saveAccount(); } }
2.6 测试运行
3、创建配置文件
3.1 在resources下创建:applicationContext.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"> </beans>
3.2 在pom文件中添加依赖,解决配置文件约束报红
<dependencies> <!--IOC相关依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.6.RELEASE</version> </dependency> </dependencies>
3.3 通过spring的IOC来创建对象
<!-- 1. spring会通过反射的方式创建对象,并将该对象以key和value的方式存入到IOC容器中。 2. bean标签的id就是key,value就是类的全路径 3. 通过bean标签将对象创建并存入到IOC容器的方式,我们可以称之为装配bean 4. 只要可以正常new出来的对象,都可以通过这种方式装配到IOC容器中 --> <!--将service实现类装配到IOC容器中--> <bean id="accountService" class="com.yuank.service.impl.AccountServiceImpl"/> <!-- 将dao实现类装配到IOC容器中 --> <bean id="accountDao" class="com.yuank.dao.impl.AccountDaoImpl"/>
3.4 加载applicationContext.xml配置文件&从spring的IOC容器中获取对象
package com.yuank.web; import com.yuank.dao.IAccountDao; import com.yuank.service.IAccountService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author PC-Black * @version v1.0 * @date 2019/7/21 21:39 * @description TODO **/ public class Client { public static void main(String[] args) { // IAccountService accountService = new AccountServiceImpl(); //1. 加载配置文件 ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); //2. 从容器中获取service IAccountService accountService = (IAccountService) ac.getBean("accountService"); //2.1 打印accountService对象 System.out.println(accountService); //3. 从容器中获取dao IAccountDao accountDao = (IAccountDao) ac.getBean("accountDao"); //3.1 打印accountDao对象 System.out.println(accountDao); accountService.saveAccount(); } }
3.5 运行main方法
查看控制台,我们发现对象创建成功
相关文章