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容器的更多相关文章

  1. Spring源码阅读-IoC容器解析

    目录 Spring IoC容器 ApplicationContext设计解析 BeanFactory ListableBeanFactory HierarchicalBeanFactory Messa ...

  2. 【sping揭秘】5、IOC容器(一)

    OC容器实现过程 1. 容器启动阶段,读取配置文件,解析文件BeanDefinitionReader 2. Bean 实例化阶段 关于BeanFactoryPostProcessor 插手spring ...

  3. 【sping揭秘】4、某些无法注册到IOC容器的对象如何交给spring托管

    可以实现spring的factoryBean 接口,这样可以加入spring的IOC容器 比如现在有一个类叫MyObject,我们没有这个对象的源码,无法对这个对象进行操作,那么我们如何加入sprin ...

  4. Spring IoC容器初始化过程学习

    IoC容器是什么?IoC文英全称Inversion of Control,即控制反转,我么可以这么理解IoC容器: 把某些业务对象的的控制权交给一个平台或者框架来同一管理,这个同一管理的平台可以称为I ...

  5. MyEclipse Spring 学习总结一 Spring IOC容器

    一.Spring IOC容器---- Spring AllicationContext容器 程序的结构如下: 1.首先在MyEclipse 创建创建Java Project 2.创建好后,添加spin ...

  6. [IoC容器Unity]第四回:使用范例

    1.引言 前面几个章节介绍了Unity的基本使用,主要分为程序和配置文件两种方法的使用,可以参考一下链接, [IoC容器Unity]第一回:Unity预览 [IoC容器Unity]第二回:Lifeti ...

  7. 反射 + 配置文件 实现IOC容器

    IOC实现: IOC容器我们只停留在知道上是不行的,我们要动手做印象对更深刻,那么我给大家看一个代码.看看代码中IOC容器的实现. 代码实现: 创建一个类库: 解决方式的类库建立: 创建一个实体类:U ...

  8. 自己动手实现一个简单的 IOC容器

    控制反转,即Inversion of Control(IoC),是面向对象中的一种设计原则,可以用有效降低架构代码的耦合度,从对象调用者角度又叫做依赖注入,即Dependency Injection( ...

  9. 深入理解DIP、IoC、DI以及IoC容器

    摘要 面向对象设计(OOD)有助于我们开发出高性能.易扩展以及易复用的程序.其中,OOD有一个重要的思想那就是依赖倒置原则(DIP),并由此引申出IoC.DI以及Ioc容器等概念.通过本文我们将一起学 ...

随机推荐

  1. deepin-wine-qq无法加载图片解决方案

    最近在qq水群讨论学术的时候发现了一个奇怪的问题:无法加载图片. 具体点是,如果图片没有被其他设备接收,并且在缓存中,图片是可以加载的,反之不可. 这东西很烦人啊,于是我就去查项目issue:http ...

  2. JS高级---利用原型共享数据

    什么样子的数据是需要写在原型中? 需要共享的数据就可以写原型中 原型的作用之一: 数据共享 //属性需要共享, 方法也需要共享 //不需要共享的数据写在构造函数中,需要共享的数据写在原型中 //构造函 ...

  3. Tarjan-有向图

    (我到底是咕了多少知识点啊) 在有向图中tarjan主要用来求强连通分量并缩点 一.定义 强连通:如果两个顶点可以相互通达,则称两个顶点 强连通 强连通分量:如果有向图G的每两个顶点都 强连通,称G是 ...

  4. vue 中监听页面滚动

    监听页面滚动 在methods中定义一个方法 handleScroll() { //获取滚动时的高度 let scrollTop = window.pageYOffset || document.do ...

  5. element 确认框 confirm 的写法

    this.confirm('内容', '标题', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'success', callbac ...

  6. 【MySQL】库的操作

    "SQL语言主要用于存取数据.查询数据.更新数据和管理关系数据库系统,SQL语言由IBM开发. SQL语言分为3种类型: DDL语句 数据库定义语言:数据库.表.视图.索引.存储过程,例如C ...

  7. DBC里首饰不同代码的含义

    DuraMax"这个就是我们要找的物件持久的属性了,一开始怎么也找不到,原来这里的变量中 "1000"表示1个持久度,那么"40000"就表示40个持 ...

  8. echart 重新加载

    myechart2.clear(); myechart2.setOption(options[1]);

  9. javac 编译引用外部指定jar包进行编译和执行编译后的class文件

    1.libs新建文件夹存放依赖所有jar包 2.cmd 执行: 2.1 javac -encoding UTF-8 -classpath .;C:\Users\chenquan\IdeaProject ...

  10. 「JSOI2013」侦探jyy

    「JSOI2013」侦探jyy 传送门 个人感觉我写的复杂度不够优秀啊,但是好像没有别的办法了... 我们枚举每个点,考虑这个点能不能不发生. 首先我们从这个点开始,在反图上面 \(\text{BFS ...