1、创建Maven工程,导入Spring坐标
<properties>
<spring.version>5.2.10.RELEASEspring.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
dependencies>
2、定义Spring管理的类(接口)
package com.liming.service;
public interface UserService {
public void addUser();
}
package com.liming.service.impl;
import com.liming.service.UserService;
public class UserServiceImpl implements UserService {
public void addUser() {
System.out.println("这是在新增用户的service哦");
}
}
3、创建Spring配置文件,配置对应类作为Spring管理的bean
<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 id="ud" class="com.etime.dao.impl.UserDaoImpl">bean>
<bean id="us" class="com.etime.service.impl.UserServiceImpl">bean>
beans>
注意:bean定义时id属性在同一个上下文中不能重复
4、初始化 Ioc容器(Spring核心容器),通过容器获取bean
@Test
public void test1(){
//new的方式创建对象调用方法
/*
UserService userService = new UserServiceImpl();
userService.addUser();
*/
//Ioc方式创建对象调用方法
//1、加载并获取SpringIOC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("application.xml");
//2、根据id获取对象
UserService userservice = ctx.getBean("userservice", UserService.class);
userservice.addUser();
}
1、IOC配置文件详解
默认情况下他会调用类中无参数的构造器,如果没有无参数构造器则不能成功创建
基本属性:
id : Bean实例对象在Spring容器当中的唯一标识
class: Bean 的全限定类名(全类名)
2、创建容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("application.xml");
ApplicationContext ctx = new FileSystemXmlApplicationContext("D:\\JavaSSM305\\day01_Spring\\src\\main\\resources\\application.xml");
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
ApplicationContext ctx = new ClassPathXmlApplicationContext("application1.xml","application2.xml");
3、获取bean
UserDao userdao = (UserDao) ctx.getBean("userdao");
UserDao userdao = ctx.getBean("userdao", UserDao.class);
UserDao userdao = ctx.getBean(UserDao.class);
4、容器类层次结构图
- BeanFactory :是Ioc容器的顶层接口,不提供开发人员使用,加载配置文件初始化BeanFactory对象时,不会创建对象,在获得(使用)对象时才采取创建对象。
(延迟加载)
- ApplicationContext: BeanFactory接口的子接口(Spring容器的核心接口),提供更多强大的功能,一般由开发人员使用。
初始化时bean立即加载
- ApplicationContext接口常用初始化类:
-ClassPathXmlApplicationContext
-AnnotationConfigApplicationContext
Ⅰ、分析IOC 实现思路
Ⅱ、构建maven工程,引入依赖
<properties>
<spring.version>5.2.10.RELEASEspring.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>dom4jgroupId>
<artifactId>dom4jartifactId>
<version>1.6.1version>
dependency>
dependencies>
Ⅲ、设计接口
//dao层接口
public interface UserDao {
public abstract void addUser();
}
//dao层实现类
public class UserDaoImpl implements UserDao {
@Override
public void addUser() {
System.out.println("这是在新增用户的dao哦");
}
}
//service层接口
public interface UserService {
public abstract void addUser();
}
//service层接口实现类
public class UserServiceImpl implements UserService {
@Override
public void addUser() {
System.out.println("这是在新增用户的service哦");
}
}
Ⅳ、在resources中创建myapplication.xml核心配置文件
<objects>
<object id="userDao" name="com.etime.dao.impl.UserDaoImpl">object>
<object id="userService" name="com.etime.service.impl.UserServiceImpl">object>
objects>
Ⅴ、使用dom4j技术解析xml配置文件
package com.liming.util;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MyFactory {
private static Map<String,Object> map = new HashMap<>();
static {
try {
//1、创建一个Dom4j的解析器对象,代表了整个dom4j框架
SAXReader reader = new SAXReader();
//2、把XML文件加载到内存中成为一个Document文档对象
Document document = reader.read("src/main/resources/myapplication.xml");
//3、获取根元素对象
Element root = document.getRootElement();
//4、获取根元素的所有子元素
List<Element> elements = root.elements();
//5、获取每一个元素的指定属性值
for (Element element : elements) {
String name = element.attributeValue("name");
String id = element.attributeValue("id");
//根据获取的name属性的值,创建对象
Class clazz = Class.forName(name);
//newInstance()调用无参构造创建类对象
Object obj = clazz.newInstance();
//根据id添加对象到集合
map.put(id,obj);
}
} catch (DocumentException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
//对外提供获取对象方法
public static Object getBean(String id){
return map.get(id);
}
}
Ⅵ、编写测试文件,展示测试结果
@Test
public void test2(){
UserDao userdao = (UserDao) MyFactory.getBean("userdao");
userdao.addUser();
UserService userservice = (UserService) MyFactory.getBean("userservice");
userservice.addUser();
}