目录
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();
}
}