Spring基于xml之IOC的环境搭建和 入门
创建Maven工程(Create New Project)
pom.xml导入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itheima</groupId>
<artifactId>spring_eesy_FirstIOC</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.17.RELEASE</version>
</dependency>
</dependencies>
</project>
Maven之Spring工程的依赖和Spring体系结构
在类路径下创建xml文件,导入约束
百度 Spring Framework Documentation 点击Core
将Configuration Metadata右边的红色框的内容 复制,这就是xml文件的约束
bean.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><!--导入约束-->
到这里Spring基于xml的IOC环境已经搭建好了
入门案例
创建三个类和两个接口,因为创建的是Maven工程而不是web工程因此Client模拟表现层,Service模拟业务层,Account模拟持久层(表现层调用业务层,业务层再调用持久层)
我的Maven工程的目录结构
1.创建Client.java
public class Client {
/*
* 获取Spring的IOC核心容器,并根据id来获取对象*/
public static void main(String[] args){
//1.获取核心容器对象
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
//2.根据id获取bean对象
IAccountService as = (IAccountService) applicationContext.getBean("accountService");
IAccountDao idao = applicationContext.getBean("accountDao",IAccountDao.class);
System.out.println(as);
System.out.println(idao);
as.saveAccount();
}
}
2.创建IAccountService 接口和实现类AccountServiceImpl
IAccountService.java
/*
* 业务层接口*/
public interface IAccountService {
//模拟保存账户
void saveAccount();
}
实现类AccountServiceImpl.java
public class AccountServiceImpl implements IAccountService {
private IAccountDao accountDao = new AccountDaoImpl();
public void saveAccount(){
accountDao.saveAccount();
}
}
3.创建IAccountDao接口和实现类AccountDaoImpl
IAccountDao.java
package com.itheima.dao;
//账户持久层接口
public interface IAccountDao {
//模拟保存账户
void saveAccount();
}
AccountDaoImpl.java
package com.itheima.dao.Impl;
import com.itheima.dao.IAccountDao;
public class AccountDaoImpl implements IAccountDao {
public void saveAccount(){
System.out.println("保存成功");
}
}
4.在bean.xml配置bean(目的:把对象的创建交给Spring达到程序解耦的目的)
id:唯一标识
class:获取对象的全限定类名
<?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">
<!--把对象的创建交给Spring--><!---->
<bean id="accountService" class="com.itheima.service.Impl.AccountServiceImpl"></bean>
<bean id="accountDao" class="com.itheima.dao.Impl.AccountDaoImpl"></bean>
</beans>
5.如何从配置文件bean.xml获取对象或者说如何做使Spring基于xml的IOC达到程序的解耦
第一步:获取核心容器对象(整个Spring体系都离不开核心容器的支持)
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(“配置文件路径.xml”);
第二步:根据id获取bean对象
IAccountService as = (IAccountService) applicationContext.getBean(“accountService”);//这种需要强制转换
IAccountDao idao = applicationContext.getBean(“accountDao”,IAccountDao.class);//这种需要.class
下图是ApplicationContext继承的接口,其中就有BeanFactory 接口,此接口有getBean()方法,该方法可以获取bean对象