IOC控制反转
- Spring反向控制应用程序所需要使用的外部资源
- Spring控制的资源全部放置在Spring容器中,该容器称为IoC容器
- 由spring进行对象的创建,而不是主动在代码通过new创建对象
- 主要通过spring的配置文件,交给spring框架进行管理
流程
1.maven引入spring框架
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
2.创建servlet层,建立servlet类
package com.spring.servlet;
import com.spring.service.UserService;
import com.spring.service.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/user")
public class UserServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置字符编码
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
// ApplicationContext ,加载配置文件
ApplicationContext appletContext = new ClassPathXmlApplicationContext("spring.xml");
//获取资源
UserService service = (UserServiceImpl)appletContext.getBean("bean1");
String s = service.find();
resp.getWriter().print(s);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
3.创建service层,在里面编写UserService接口类及其实现类
package com.spring.service;
public interface UserService {
String find();
}
package com.spring.service;
import com.spring.dao.UserDAO;
import com.spring.dao.UserDAOImpl;
import com.spring.dao.UserDAOImpl2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserServiceImpl implements UserService{
@Override
public String find() {
// ApplicationContext ,加载配置文件
ApplicationContext appletContext = new ClassPathXmlApplicationContext("spring.xml");
//获取资源
UserDAO user = (UserDAOImpl2)appletContext.getBean("bean2");
//根据spring代理生成的对象,调用其方法
String one = user.one();
return one;
}
}
4.建立DAO层,编写DAO.class和其实现类
package com.spring.dao;
public interface UserDAO {
String one();
}
package com.spring.dao;
public class UserDAOImpl implements UserDAO{
@Override
public String one() {
return "李白";
}
}
package com.spring.dao;
public class UserDAOImpl2 implements UserDAO{
@Override
public String one() {
return "李清照";
}
}
5.创建spring的配置文件,并通过ApplicationContext加载
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="bean1" class="com.spring.service.UserServiceImpl"></bean>
<bean id="bean2" class="com.spring.dao.UserDAOImpl2"></bean>
</beans>
6.在servlet调用Service,service调用DAO实现类
- 结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6c205575ce4ed6465176876d695877a3.png)
总结
- 要生成不同的对象,只需在spring.xml里面配置不同的<bean>,传入不同的类,根据其bean的id即可加载和生成不同的类对象
- 通过ApplicationContext ,加载配置文件,再通过其对象,获取spring.xml配置文件的bean,获得其对应的类对象,从而可以调用其方法
// ApplicationContext ,加载配置文件
ApplicationContext appletContext = new ClassPathXmlApplicationContext("spring.xml");
//获取资源
UserService service = (UserServiceImpl)appletContext.getBean("bean1");
- 要对properties文件的支取,只需在spring.xml添启context命名空间支持
xmlns:context="http://www.springframework.org/schema/context"
加载指定的properties文件
<context:property-placeholder location="classpath:filename.properties">
4.使用加载的数据
<property name="propertyName" value="${propertiesName}"/>