ioc—01

目录

1.Spring中ioc的特点

2.依赖注入的3种方式

3.Spring与web容器的整合


1.Spring中ioc的特点

package com.zking.biz.impl;

import com.zking.biz.UserBiz;

public class UserBizImpl1 implements UserBiz{

	@Override
	public void list() {
		System.out.println("查询用户数据,按照年龄排序。。。");
		
	}

}


package com.zking.biz.impl;

import com.zking.biz.UserBiz;

public class UserBizImpl2 implements UserBiz{

	@Override
	public void list() {
		System.out.println("查询用户数据,按照入职时间排序。。。");
	}

}

2.依赖注入的3种方式

<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="byName" xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

	<!-- IOC的主要作用管理整个项目的javabean,依靠依赖注入、控制反转的特点进行管理 -->
	<bean class="com.zking.biz.impl.UserBizImpl1" id="userBiz"></bean>
	<!-- <bean class="com.zking.biz.impl.UserBizImpl2" id="userBiz"></bean> -->
	<!-- set注入 -->
	<bean class="com.zking.web.UserAction" id="userAction">
		<property name="userBiz" ref="userBiz"></property>
		<property name="age" value="22"></property>
		<property name="name" value="zhangsan"></property>
		<property name="hobby">
			<list>
				<value>篮球</value>
				<value>boy</value>
				<value>篮球</value>
			</list>
		</property>
	</bean>
	<!-- 构造注入 -->
	<bean class="com.zking.web.OrderAction" id="orderAction">
		<property name="userBiz" ref="userBiz"></property>
		<constructor-arg name="name" value="zhangsan"></constructor-arg>
		<constructor-arg name="age" value="22"></constructor-arg>
		<constructor-arg name="hobby">
			<list>
				<value>篮球</value>
				<value>boy</value>
				<value>篮球</value>
			</list>
		</constructor-arg>
	</bean>
</beans>

3.Spring与web容器的整合

package com.zking.ioc.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zking.web.UserAction;

public class SpringLoadListener implements ServletContextListener{

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		System.out.println("初始化。。。。");
		ServletContext servletContext = sce.getServletContext();
		String springConfigLocation = servletContext.getInitParameter("springConfigLocation");
		System.out.println(springConfigLocation+"....");
		//		拿到spring上下文
		ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("/spring-context.xml");
//		将spring上下文保存到tomcat上下文中
		servletContext.setAttribute("springContext", context);
	}
	
}


package com.zking.ioc.demo;

import java.io.IOException;

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 org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zking.web.UserAction;

/**
 * spring与web容器的整合原理
 * why:建模的过程是十分耗时的
 * 	解决问题:
 * 	1.建模必不可少
 * 	2.建模要保障只执行一次
 * 	3.建模后期望在每一个servlet都能够拿到spring的上下文对象ClassPathXmlApplicationContext
 * how:
 * 	1.监听器的初始化方法
 * 	2.spring的上下 要 存放 在tomcat上下文中
 * 
 * @author Administrator
 *
 */
@WebServlet("/springDemo")
public class DemoServlet extends HttpServlet{
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		Thread.sleep(1000);
//		ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("/spring-context.xml");
		ClassPathXmlApplicationContext context=(ClassPathXmlApplicationContext) req.getServletContext().getAttribute("springContext");
		UserAction userAction = (UserAction)context.getBean("userAction");
		userAction.list();
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值