目录
什么是spring,它能够做什么?
Spring是一个开源框架,它由Rod Johnson创建。它是为了解决企业应用开发的复杂性而创建的。
Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。
然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。
目的:解决企业应用开发的复杂性
功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
范围:任何Java应用
简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。:
容器框架
JavaBean 项目中的一个个类
IOC和AOP
项目报错解决:根据图片进行操作
新建Maven项目后--->添加以下代码
5.0.1.RELEASE
4.0.0
4.12
如图

junit
junit
3.8.1
test
org.springframework
spring-context
${spring.version}
org.springframework
spring-aspects
${spring.version}
junit
junit
${junit.version}
test
javax.servlet
javax.servlet-api
${javax.servlet.version}
provided
如图

org.apache.maven.plugins
maven-compiler-plugin
3.7.0
1.8
1.8
UTF-8
如图

继续操作取消勾选,最后确定
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">

找到Project Facets ---->java 1.3修改为1.8

如图操作修改后点击apply应用

这里要注意:java修改版本后 ,想要修改Dynamic Web Module 要先取消勾选再修改版本为3.1,然后应用 ,最后勾选
仔细观察下面蓝色字体点击 进行修改

修改路径src/main/webapp(项目根目录)
最后更新一下update

仔细看,报错解决了

=================================开始写入项目=========================================================================
案例分析:
新建以下包类

package com.zking.biz;
/**
*
* 用户业务类:
* 需求:
* 同时在用户模块,订单模块拿到所有用户数据
*
* 需求变更1:
* 同时在用户模块,订单模块拿到所有用户数据,要求按年龄排序
* 对应方法:修改Userbiz里的list方法 ,添加排序功能
* 需求变更2:
* 同时在用户模块,订单模块拿到所有用户数据,用户的入职时间排序
* 对应方法:修改Userbiz里的list方法 ,添加时间排序功能
* @author LUCY
*
*/
总结:
* 在使用spring之前,此方法(修改实现类)是最原始的,且频繁使用biz层代码,多实现,
* 缺陷是经常修改实现类代码
*
public interface UserBiz {
void list();
}
实现类1
- package impl;
-
- import com.zking.biz.UserBiz;
-
- /**实现类
- * @author lucy
- *
- */
- public class UserBizImpl1 implements UserBiz{
-
- @Override
- public void list() {
- System.out.println("查询用户数据,按照年龄排序~");
-
- }
-
-
-
- }
实现类2
- package impl;
-
- import com.zking.biz.UserBiz;
-
- /**实现类
- * @author lucy
- *
- */
- public class UserBizImpl2 implements UserBiz{
-
- @Override
- public void list() {
- System.out.println("查询用户数据,按照入职时间排序~");
-
- }
-
-
-
-
- }
action1
package com.zking.web;
import com.zking.biz.UserBiz;
import impl.UserBizImpl1;
import impl.UserBizImpl2;public class UserAction {
private UserBiz ub=new UserBizImpl2();
public void list() {
ub.list();
}
}
action2
package com.zking.web;
import com.zking.biz.UserBiz;
import impl.UserBizImpl1;
import impl.UserBizImpl2;public class OrderAction {
private UserBiz ub=new UserBizImpl2();
public void list() {
ub.list();
}
}
把那个复制入项目:使用Spring 里的ioc后
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">
代码进行更新
userAction内容和orderactin一致 添加set、get方法
package com.zking.web;
import com.zking.biz.UserBiz;
import impl.UserBizImpl1;
import impl.UserBizImpl2;public class UserAction {
//private UserBiz ub=new UserBizImpl2();
private UserBiz userbiz;
public UserBiz getUserbiz() {
return userbiz;
}
public void setUserbiz(UserBiz userbiz) {
this.userbiz = userbiz;
}public void list() {
userbiz.list();
}
}
测试
package com.zking.web;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 1.对Spring框架配置文件进行建模,建模后,spring-context.xml里所有的额javabean信息会加载入Spring容器的上下文
* 2.上下文就包容了sprinbg-context.xml所有对象
*
* @author lucy控制反转:指的是创建对象的权利反转给Spring容器来完成
*
*/
public class demo {
@SuppressWarnings("resource")
public static void main(String[] args) {
ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext("/spring-context.xml");
UserAction useraction=(UserAction) context.getBean("userAction");
useraction.list();
OrderAction orderaction=(OrderAction) context.getBean("orderAction");
orderaction.list();
}
}
结果图

依赖注入;
更新xml ----SET注入
注入方式:
1.set注入
基本数据类型注入
集合注入
对象注入
2.构造注入
基本数据类型注入
3 .自动装配
:byname:是Spring管理的对象id进行查找,如找不到则注入失败,反之注入成功
bytype:是Spring 管理的bean 对象接口实现类进行查找,如果有或者2个以上,则注入失败,反之成功
rap
画画
看沉香
userAction代码更新
package com.zking.web;
import java.util.List;import com.zking.biz.UserBiz;
import impl.UserBizImpl1;
import impl.UserBizImpl2;/**
* 依赖注入方式:
*
*
* @author lucy
*
*/
public class UserAction {
//private UserBiz ub=new UserBizImpl2();
private UserBiz userbiz;
public UserBiz getUserbiz() {
return userbiz;
}
public void setUserbiz(UserBiz userbiz) {
this.userbiz = userbiz;
}
private String name;
private int age;
private Listhobby;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public ListgetHobby() {
return hobby;
}
public void setHobby(Listhobby) {
this.hobby = hobby;
}
public void list() {
System.out.println(name);
System.out.println(age);
System.out.println(hobby);
userbiz.list();
}
}
如图
<constructor-arg name="age" value="14">
rap
画画
看沉香
orderaction类代码更新
package com.zking.web;
import java.util.List;
import com.zking.biz.UserBiz;
import impl.UserBizImpl1;
import impl.UserBizImpl2;public class OrderAction {
// private UserBiz ub=new UserBizImpl2();
private UserBiz userbiz;
public UserBiz getUserbiz() {
return userbiz;
}
public void setUserbiz(UserBiz userbiz) {
this.userbiz = userbiz;
}private String name;
private int age;
private Listhobby;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public ListgetHobby() {
return hobby;
}
public void setHobby(Listhobby) {
this.hobby = hobby;
}
public OrderAction(String name, int age, Listhobby) {
super();
this.name = name;
this.age = age;
this.hobby = hobby;
}public OrderAction() {
super();
}
public void list() {
System.out.println(name);
System.out.println(age);
System.out.println(hobby);
userbiz.list();
}
}
效果图

为了减少代码量 改进方法:
测试byName 与bytype区别
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">
注掉该代码
rap
画画
看沉香
--> 注掉该代码
rap
画画
看沉香
有效果

修改byType
package com.zking.web;
import java.util.List;import com.zking.biz.UserBiz;
import impl.UserBizImpl1;
import impl.UserBizImpl2;/**
* 依赖注入方式:
*
*
* @author lucy
*
*/
public class UserAction {
//private UserBiz ub=new UserBizImpl2();
private UserBiz userbiz1;
public UserBiz getUserbiz() {
return userbiz1;
}
public void setUserbiz(UserBiz userbiz) {
this.userbiz1 = userbiz;
}
private String name;
private int age;
private Listhobby;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public ListgetHobby() {
return hobby;
}
public void setHobby(Listhobby) {
this.hobby = hobby;
}
public void list() {
System.out.println(name);
System.out.println(age);
System.out.println(hobby);
userbiz1.list();
}
}
修改
default-autowire="byType"
效果
【可得结论】
1.byType 不报错 :与名字无关
2.byName 报 错 action使用时会报错,根据名字(userbiz1)找不到
如果想要它不报错,首先要在xml里补充代码:
监听器初始化方法快捷键(ALT+shift+s)

勾选

还原代码
package com.zking.web;
import java.util.List;import com.zking.biz.UserBiz;
import impl.UserBizImpl1;
import impl.UserBizImpl2;/**
* 依赖注入方式:
*
*
* @author lucy
*
*/
public class UserAction {
//private UserBiz ub=new UserBizImpl2();
private UserBiz userbiz;
public UserBiz getUserbiz() {
return userbiz;
}
public void setUserbiz(UserBiz userbiz) {
this.userbiz = userbiz;
}
private String name;
private int age;
private Listhobby;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public ListgetHobby() {
return hobby;
}
public void setHobby(Listhobby) {
this.hobby = hobby;
}
public void list() {
System.out.println(name);
System.out.println(age);
System.out.println(hobby);
userbiz.list();
}
}
还原代码
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">
rap
画画
看沉香
rap
画画
看沉香
SpringloadListener
- package Listener;
-
- import javax.servlet.ServletContext;
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
-
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- 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 上下文保存到tomcat上下文
- servletcontext.setAttribute("springContext", context);
-
-
- }
-
-
-
-
- }
测试类demoServlet
- package com.zking.web;
-
- 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;
-
- /**
- * Servlet implementation class demoServlet
- */
- /**
- * Spring 与web容器的整合原理
- * 原因:省时间
- * 解决问题:
- * 1.建模必不可少
- * 2.建模保持执行一次
- * 3.建模后期望在每一个sevlet都可以拿到Spring的上下文对象ClassPathXmlApplicationContext
- * :1.监听器初始化方法
- * 2.上下文放在tomcat里
- *
- * @author lucy
- *
- */
- @WebServlet("/demoServlet")
- public class demoServlet extends HttpServlet {
- @Override
- protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- // ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext("/spring-context.xml");
- // springContext
- ClassPathXmlApplicationContext context =(ClassPathXmlApplicationContext) req.getServletContext().getAttribute("springContext");
- UserAction useraction=(UserAction) context.getBean("userAction");
- useraction.list();
-
- }
-
-
-
- }
配置文件的修改
Archetype Created Web Application
springConfigLocation
/applicationContext.xml
Listener.SpringloadListener
