Sping IOC容器
Sping IOC容器
package servlet; import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import service.IStudentService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import java.io.IOException; @WebServlet("/QueryStudentByIdServlet")
public class QueryStudentByIdServlet extends javax.servlet.http.HttpServlet {
private IStudentService studentService; public void setStudentService(IStudentService studentService) {
this.studentService = studentService;
} @Override
public void init() throws ServletException {
super.init();
ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
studentService = (IStudentService) applicationContext.getBean("studentServiceId");
} protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
throws javax.servlet.ServletException, IOException {
String name = studentService.queryStudentById();
request.setAttribute("name", name);
request.getRequestDispatcher("result.jsp").forward(request, response);
} protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
throws javax.servlet.ServletException, IOException {
doGet(request, response);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml, classpath:applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> </beans>
applicationContext-Controller.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">
<bean id="studentServlet" class="servlet.QueryStudentByIdServlet">
<property name="studentService" ref="studentServiceId"> </property>
</bean>
</beans>
applicationContext-Service.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">
<bean id="studentServiceId" class="service.impl.StudentServiceImpl">
<property name="studentDao" ref="studentDaoId"></property>
</bean>
</beans>
applicationContext-Dao.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">
<bean id="studentDaoId" class="dao.impl.StudentDaoImpl"></bean>
</beans>
Sping IOC容器的更多相关文章
- Spring源码阅读-IoC容器解析
目录 Spring IoC容器 ApplicationContext设计解析 BeanFactory ListableBeanFactory HierarchicalBeanFactory Messa ...
- 【sping揭秘】5、IOC容器(一)
OC容器实现过程 1. 容器启动阶段,读取配置文件,解析文件BeanDefinitionReader 2. Bean 实例化阶段 关于BeanFactoryPostProcessor 插手spring ...
- 【sping揭秘】4、某些无法注册到IOC容器的对象如何交给spring托管
可以实现spring的factoryBean 接口,这样可以加入spring的IOC容器 比如现在有一个类叫MyObject,我们没有这个对象的源码,无法对这个对象进行操作,那么我们如何加入sprin ...
- Spring IoC容器初始化过程学习
IoC容器是什么?IoC文英全称Inversion of Control,即控制反转,我么可以这么理解IoC容器: 把某些业务对象的的控制权交给一个平台或者框架来同一管理,这个同一管理的平台可以称为I ...
- MyEclipse Spring 学习总结一 Spring IOC容器
一.Spring IOC容器---- Spring AllicationContext容器 程序的结构如下: 1.首先在MyEclipse 创建创建Java Project 2.创建好后,添加spin ...
- [IoC容器Unity]第四回:使用范例
1.引言 前面几个章节介绍了Unity的基本使用,主要分为程序和配置文件两种方法的使用,可以参考一下链接, [IoC容器Unity]第一回:Unity预览 [IoC容器Unity]第二回:Lifeti ...
- 反射 + 配置文件 实现IOC容器
IOC实现: IOC容器我们只停留在知道上是不行的,我们要动手做印象对更深刻,那么我给大家看一个代码.看看代码中IOC容器的实现. 代码实现: 创建一个类库: 解决方式的类库建立: 创建一个实体类:U ...
- 自己动手实现一个简单的 IOC容器
控制反转,即Inversion of Control(IoC),是面向对象中的一种设计原则,可以用有效降低架构代码的耦合度,从对象调用者角度又叫做依赖注入,即Dependency Injection( ...
- 深入理解DIP、IoC、DI以及IoC容器
摘要 面向对象设计(OOD)有助于我们开发出高性能.易扩展以及易复用的程序.其中,OOD有一个重要的思想那就是依赖倒置原则(DIP),并由此引申出IoC.DI以及Ioc容器等概念.通过本文我们将一起学 ...
随机推荐
- Python 高级特性介绍 - 迭代的99种姿势 与协程
Python 高级特性介绍 - 迭代的99种姿势 与协程 引言 写这个笔记记录一下一点点收获 测试环境版本: Python 3.7.4 (default, Sep 28 2019, 16:39:19) ...
- CentOS7重启和关机
重启命令: 1.reboot 2.shutdown -r now 立刻重启(root用户使用) 3.shutdown -r 10 过10分钟自动重启(root用户使用) 4.shutdown -r 2 ...
- 华硕笔记本(i76700hq+nvidia goforce940mx)安装ubuntu18.04
Ubuntu的安装 今天终于下定决心要把笔记本安装成Ubuntu,但是网上的教材不够全面,我就想整合以下教程. 接下来详细讲解安装过程 1. Ubuntu iso镜像下载 下载地址:https://w ...
- JSP技术(一)
Servlet的两个缺点: 1.首先,写在Servlet中所有的HTML标签必须包含JAVA字符串,使得处理HTTP响应报文工作十分繁琐. 2.所有的文件和HTML标记是硬代码,导致即使是微小变化,也 ...
- Flutter Android 真机器调试 、模拟器调试、Vscode 中开发 Flutter 应用
必备条件: 1.准备一台 Android 手机 2.手机需要开启调试模式 3.用数据线把手机连上电脑 4.手机要允许电脑进行 Usb 调试 5.手机对应的 sdk 版本必须安装 注意: 1.关闭电脑上 ...
- 洛谷P1131 时态同步
题意: 给一个n点的树,每条边都有边权,问从根出发需要增加多少长度,使得最终的儿子到根的距离是一样的 思路: 上来一个思路wa了3次,看完题解之后,又一次豁然开朗…… orz #include< ...
- android studio中project structure配置
android studio project structure 1.project中填jdk路径 2.module中添androidsdk路径 3.sdks中填jdk路径
- 3 爬虫cookie的处理办法
cookie的应用和处理 - cookie:服务器端记录客户端的相关状态 - 处理cookie的方式: - 手动处理:不建议 页面找隐藏的标签,获取value - 自动处理:会话对象Session,该 ...
- TakeColor下载及调节鼠标指针不一致的问题
取色器下载链接: 链接:https://pan.baidu.com/s/19TBWZA2ltaLQjzskTipnmg 提取码:2uz3 若出现指针与鼠标不一致: TakeColor v8.0 > ...
- sqlite3 install 和使用
windows: 在 Windows 上安装 SQLite 请访问 http://www.sqlite.org/download.html,从 Windows 区下载预编译的二进制文件. 您需要下载 ...