• Spring01


    这里写目录标题

    1.什么是Spring

      	 Spring是一个开源框架,它由Rod Johnson创建。它是为了解决企业应用开发的复杂性而创建的。
    
    • 1

    Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。
    然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。
    目的:解决企业应用开发的复杂性
    功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
    范围:任何Java应用
    简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。

    2.代码

    userbiz:

    package com.ruojuan.biz;
    
    /**
     * 
     * @author ruojuan
     * 
     * 名称:用户业务类
     * 需求:
     * 		同时在用户模块,订单模块所拿到所有的用户数据
     * 
     * 需求变更1:
     * 	同时在用户模块,订单模块拿到所有的用户数据,并且要求用户数据是已经通过年龄排序的
     * 	对应策略:修改userBiz中list方法,添加排序功能
     * 
     * 需求变更2:
     * 	同时在用户模块,订单模块拿到所有的用户数据,并且要求用户数据是已经通过时间点排序的
     * 	对应策略:修改userBiz中list方法,添加排序功能,按照时间点排序
     * 
     * 
     * 总结:
     * 	最原始:频繁修改业务层biz层代码
     * 	多实现:凡是涉及到用户业务层调用的地方,都需要修改代码
     * 。。。
     * 时间:2022年8月4日 下午7:00:25
     */
    public interface UserBiz {
    	void list();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    userBizimpl1:

    package com.ruojuan.impl;
    
    import com.ruojuan.biz.UserBiz;
    
    public class UserBizImpl1 implements UserBiz{
    
    	@Override
    	public void list() {
    		System.out.println("查询用户数据,按照入职时间排序。。。。。");
    		
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    UserBizImpl2:

    package com.ruojuan.impl;
    
    import com.ruojuan.biz.UserBiz;
    
    public class UserBizImpl2 implements UserBiz{
    
    	@Override
    	public void list() {
    		System.out.println("查询用户数据,按照入职年龄排序。。。。。");
    		
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    Demo1:

    package com.ruojuan.ioc.demo;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.ruojuan.web.OrderAction;
    import com.ruojuan.web.UserAction;
    
    /**
     * 1.对sqpring框架的配置文件进行建模,建模之后 spring-context.xml中所有的javabean信息都
     * 都会加载进Spring  容器的上下文
     * 2.上下文中就包含了spring-context.xml 所有对象
     * 
     * IOC的特点-什么叫控制反转
     * 指的是将创建对象的权利反转个给spring容器来完成
     * 
     * @author 若娟
     * 名称:
     * 时间:2022年8月5日 下午2:10:57
     */
    public class Demo1 {
    	@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();
    		
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    DemoServlet:

    package com.ruojuan.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.ruojuan.web.UserAction;
    
    /**
     * spring与web容器的整合原理
     * why:建模的过程是十分耗时的
     * 解决问题:
     * 		1.建模必不可少
     * 		2.建模要保障只执行一次
     * 		3.建模后期望在每一次servlet都能够拿到Spring的上下文对象ClassPathXmlApplicationContext
     * 
     * how:
     * 	1.监听器的初始化方法
     * 	2.Spring的上下要存放在tomcat上下文中
     * 
     * @author 若娟
     * 名称:
     * 时间:2022年8月5日 下午4:15:12
     */
    
    
    @WebServlet("/springDemo")
    public class DemoServlet extends HttpServlet{
    	
    	@Override
    	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    		//Thread.sleep(10000);
    		//ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
    		ClassPathXmlApplicationContext context = (ClassPathXmlApplicationContext) req.getServletContext().getAttribute("servletContext");
    		UserAction useraction =(UserAction) context.getBean("userAction");
    		useraction.list();
    	}
    	
    	
    	
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    SpringLoadListener:

    package com.ruojuan.ioc.listener;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.ruojuan.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("servletContext", servletContext);
    	
    	}
    	
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    OrderAction:

    package com.ruojuan.web;
    
    import java.util.List;
    
    import com.ruojuan.biz.UserBiz;
    import com.ruojuan.impl.UserBizImpl1;
    
    public class OrderAction {
    
    //	private UserBiz userbiz = new UserBizImpl1();
    	
    	
    	private UserBiz userbiz;
    	
    	public UserBiz getUserbiz() {
    		return userbiz;
    	}
    
    	public void setUserbiz(UserBiz userbiz) {
    		this.userbiz = userbiz;
    	}
    	
    	
    	private String name;
    	private int age;
    	private List<String> hobby;
    	
    	
    	
    	public OrderAction() {
    
    	}
    	public OrderAction(String name, int age, List<String> hobby) {
    		this.name = name;
    		this.age = age;
    		this.hobby = hobby;
    	}
    
    	public void list() {
    		System.out.println(name);
    		System.out.println(age);
    		System.out.println(hobby);
    		userbiz.list();
    	}
    	
    	
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    UserAction:

    package com.ruojuan.web;
    
    import java.util.List;
    
    import com.ruojuan.biz.UserBiz;
    
    
    /**
     * 依赖注入的方式:
     * 1.set注入
     * 2.构造注入
     * 3.自动装配
     * 	default-autowire="byType"
     * 		byName:是spring管理的bean对象的id进行查找,如果找不到则注入失败;反之成功
     * 		byType:是Spring管理bean对象接口实现类进行查找;如果没有或2个以上,则注入失败,反之成功
     * @author ruojuan
     * 名称:
     * 时间:2022年8月5日 下午2:39:26
     */
    
    public class UserAction {
    //	private UserBiz userbiz = new UserBizImpl1();
    	
    	private UserBiz userbiz;
    	
    	
    	
    	public UserBiz getUserbiz() {
    		return userbiz;
    	}
    
    	public void setUserbiz(UserBiz userbiz) {
    		this.userbiz = userbiz;
    	}
    
    
    	
    	private String name;
    	private int age;
    	private List<String> hobby;
    	
    	
    
    	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 List<String> getHobby() {
    		return hobby;
    	}
    
    	public void setHobby(List<String> hobby) {
    		this.hobby = hobby;
    	}
    	
    	
    	
    
    	public void list() {
    		System.out.println(name);
    		System.out.println(age);
    		System.out.println(hobby);
    		userbiz.list();
    	}
    	
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79

    spring-context.xml:

    
    <beans  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">
    
    
    	
     	<bean class="com.ruojuan.impl.UserBizImpl1" id="userBiz">bean>
     	
    
    	<bean class="com.ruojuan.web.UserAction" id="userAction">
    		<property name="userbiz" ref="userBiz">property>
    		<property name="name" value="luoluo">property>
    		<property name="age" value="22">property>
    		<property name="hobby">
    			<list>
    				<value>玩鬼屋value>
    				<value>吓别人value>
    				<value>好开心value>
    			list>
    		property>
    	bean>
    	
    	<bean class="com.ruojuan.web.OrderAction" id="orderAction">
    		<property name="userbiz" ref="userBiz">property>
    		<constructor-arg name="name" value="lele">constructor-arg>
    		<constructor-arg name="age" value="112">constructor-arg>
    		<constructor-arg name="hobby">
    			<list>
    				<value>玩密室value>
    				<value>很烧脑value>
    				<value>超喜欢value>
    			list>
    		constructor-arg>
    	bean>
    
    beans>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    web.xml:

    
    ```java
    <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">
      <display-name>Archetype Created Web Applicationdisplay-name>
    	
      <context-param>
      	<param-name>springconfiglocationparam-name>
     	<param-value>/applicationContext.xmlparam-value>
      context-param>
      <listener>
      	<listener-class>com.ruojuan.ioc.listener.SpringLoadListenerlistener-class>
      listener>
    
    web-app>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    pom.xml
    
    ```java
    
    ```xml
    
    	4.0.0
    	com.javaxl
    	spring
    	war
    	0.0.1-SNAPSHOT
    	spring Maven Webapp
    	http://maven.apache.org
    	
    	
    	
    		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
    		
    	
    	
    		spring
    		
    			
    				org.apache.maven.plugins
    				maven-compiler-plugin
    				3.7.0
    				
    					1.8
    					1.8
    					UTF-8
    				
    			
    		
    	
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    
    
    • 1
  • 相关阅读:
    按照 STAR 法则介绍自己做过的项目
    分析师:百度到2030年可能成为中国市值最高的公司
    java计算机毕业设计医院出入院管理系统源程序+mysql+系统+lw文档+远程调试
    leetcode 64. 最小路径和
    Java多线程——Callable和future
    3d打印出现stl文件物体不是流形,意味着不是水密体...解决办法
    六种常用事务解决方案,你方唱罢,我登场(没有最好只有更好)
    R语言lavaan结构方程模型在复杂网络分析中的科研技术新趋势
    【C指针终极奥义】回调函数思想——函数指针做函数参数
    LVGL中LV_SCROLL_SNAP_CENTER宏的作用
  • 原文地址:https://blog.csdn.net/weixin_67338832/article/details/126181234