Spring4入门之第一章IOC和DI

Spring4入门之第一章

Spring的概述

Spring的概述

  • 什么是Spring

    Spring框架是Java平台的一个开源的全栈(Full-stack)应用程序框架和控制反转(IOC)容器实现,一般被直接称为 Spring。该框架的一些核心功能理论上可用于任何 Java 应用,但 Spring 还为基于java企业版平台构建的 Web 应用提供了大量的拓展支持。虽然 Spring 没有直接实现任何的编程模型,但它已经在 Java 社区中广为流行,基本上完全代替了企业级JavaBeans(EJB)模型。

  • Spring:SE/EE开发的一站式框架。

    • 一站式框架:有EE开发的每一层解决方案。
    • WEB层 :SpringMVC
    • Service层 :Spring的Bean管理,Spring声明式事务
    • DAO层 :Spring的Jdbc模板,Spring的ORM模块
  • 为什么学习Spring

在这里插入图片描述

Spring的入门(IOC XMl方式)

  • IOC: Inversion of Control(控制反转)。将对象的创建权反转给(交给)Spring。

  • 下载Spring的开发包

    https://blog.csdn.net/SYJ_1835_NGE/article/details/89415506

  • Spring的开发包

    在这里插入图片描述

    docs :Spring的开发规范和API

    libs :Spring的开发的jar和源码

    schema :Spring的配置文件的约束

  • 创建web项目,引入jar包(红框内为必须部分)

在这里插入图片描述

  • 传统方式和将控制权交给Spring管理的区别

    假设一个场景:Service层调用Dao层的代码实现某个功能

    • 传统的方式:

      //创建一个接口
      public interface UserDao {
      	public void save();
      }
      //-------------------------------
      //创建一个实现类
      public class UserDaoImpl implements UserDao {
      	@Override
      	public void save() {
      		System.out.println("UserDaoImpl执行了。。。。");
      	}
      }
      

      在进行某功能实现的时候我们可以直接调用,但是,如果要求在Dao的是实现方式上,要求使用Hibernate实现和数据库的交互,我们需要修改源代码,显然是不合理的。

在这里插入图片描述

  • Spring方式实现

    要想使用Spring框架去管理我们的实现类,我们需要去创建一个配置文件一般以applicationContext.xml进行命名(但不是必须的)

    在applicationContext.xml文件中的一些注意的细节:

    引入schema约束:在spring-framework-4.2.4\spring-framework-4.2.4.RELEASE\docs\spring-framework-reference\html\xsd-configuration.html,在最下面,里面有多种schema约束

    the beans schema

    <?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">
    
    
    </beans>
    

    关于在Eclipse中Spring约束提示问题:

    对bean进行配置

    <!-- Spring IOC的入门配置 -->
    <bean id="userDao" class="com.syj.spring.demo1.UserDaoImpl" />
    <bean id="userDaoHibernate" class="com.syj.spring.demo1.UserDaoHibernateImpl" />
    

    测试类:

    @Test
    	/**
    	 * Sring的IOC方式
    	 */
    	public void demo2() {
    		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    		UserDao userDao = (UserDao) applicationContext.getBean("userDao");
    		UserDao userDaoHibernate = (UserDao) applicationContext.getBean("userDaoHibernate");
    		userDaoHibernate.save();
    		userDao.save();
    	}
    

IOC和DI的区别和联系

  • IOC:控制反转,将对象的创建权反转给了Spring

    • DI:依赖注入,前提必须有IOC的环境,Spring管理这个类的时候将类的依赖的属性注入(设置)进来。
  • 面向对象的时候

    • 依赖

      Class A{}
      //B依赖于A
      Class B{
      	public void xxx(A a){}
      }
      
    • 继承:is a

      Class A{}
      Class B extends A{}
      
    • 聚合:has a

      根据紧密程度分:

      一个人必须有一个脑袋

      一个篮球队有没有中锋都可以打比赛

  • 依赖注入的简单演示

    public class UserDaoImpl implements UserDao {
        //为name属性进行赋值
    	private String name;
    	public void setName(String name) {
    		this.name = name;
    	}
    	@Override
    	public void save() {
    		System.out.println("UserDaoImpl执行了。。。。" + name);
    	}
    }
    

    我们只需要在配置文件中添加一段配置即可(不需要修改源码)

    <bean id="userDao" class="com.syj.spring.demo1.UserDaoImpl" >
    		<property name="name" value="孙悟空" />
    </bean>
    

Spring的工厂类

  • 首先记住一个图。Spring工程类的结构图

在这里插入图片描述

  • ApplicationContext继承BeanFactory

    • BeanFactory:老版本的工厂类

      BeanFactory:调用getBean的时候,才会生成类的实例

    • ApplicationContext:新版本的工厂类

      ApplicationContext:加载配置文件的时候,就会将Spring管理的类都实例化

      ApplicationContext有两个实现类

      • ClassPathXmlApplicationContext :加载类路径下的配置文件
      • FileSystemXmlApplicationContext :加载文件系统下的配置文件

Spring中的Bean

Bean的相关配置

  • bean标签的id和name的配置

    id :使用了约束中的唯一约束。里面不能出现特殊字符的。

    name :没有使用约束中的唯一约束(理论上可以出现重复的,但是实际开发不能出现的)。里面可以出现特殊字符。

    <bean name=”/user” class=””/>
    
  • Bean的生命周期的配置

    init-method :Bean被初始化的时候执行的方法

    destroy-method :Bean被销毁的时候执行的方法(Bean是单例创建,工厂关闭)

  • Bean的作用范围的配置(重点)

    scope — > Bean的作用范围

    scope取值描述
    singleton默认的,Spring会采用单例模式创建这个对象
    prototype多例模式。(Struts2和Spring整合一定会用到)
    request应用在web项目中,Spring创建这个类以后,将这个类存入到request范围中。
    session应用在web项目中,Spring创建这个类以后,将这个类存入到session范围中。
    globalsession应用在web项目中,必须在porlet环境下使用。但是如果没有这种环境,相对于session。(登录腾讯之后旗下的子项目qq空间等不需要再进行登录)

Bean的管理(XML方式)

  • Spring的Bean的实例化方式(了解)

    Bean已经都交给Spring管理,Spring创建这些类的时候,有几种方式:

    1. 无参构造方法的方式(默认)
    public class Bean1 {
    	public Bean1() {
    		super();
    		System.out.println("Bean1的无参数的构造方法执行了...");
    	}
    }
    //----------applicationContext.xml配置------------------
    <bean  id="bean1" class="com.syj.spring.demo2.Bean1" ></bean>
    
    1. 静态工厂实例化的方式
     public class Bean2Factory {
     	public static Bean2 createBean2() {
     		System.out.println("Bean2Factory中方法执行了...");
     		return new Bean2();
     	}
     }
     //----------applicationContext.xml配置------------------
      <bean  id="bean2Factory" class="com.syj.spring.demo2.Bean2Factory" factory-method="createBean2" ></bean>
    
    1. 实例工厂实例化的方式
      public class Bean3Factory {
      	public Bean3 createBean3() {
      		System.out.println("Bean3Factory中方法执行了...");
      		return new Bean3();
      	}
      }
      //----------applicationContext.xml配置------------------
      <bean  id="bean3Factory" class="com.syj.spring.demo2.Bean3Factory" ></bean>
      <bean id="bean3"  factory-bean="bean3Factory" factory-method="createBean3" ></bean>
    

Spring的属性注入

在这里插入图片描述

  • 构造方法的方式注入普通属性

    public class Car {
    	/*
    	 * 属性注入(构造方式)
    	 */
    	private String name;
    	private double price;
    
    	public Car(String name, double price) {
    		super();
    		this.name = name;
    		this.price = price;
    	}
    }
    //----------applicationContext.xml配置------------------
    <bean   id="car" class="com.syj.spring.demo3.Car" >
    			<constructor-arg name="name"  value="宝马" />
    			<constructor-arg name="price"  value="800000" />
    </bean>
    
  • 构造方法的方式注入对象属性

    public class Employee {
    	private String name;
    	private Car car;
    
    	public Employee(String name, Car car) {
    		super();
    		this.name = name;
    		this.car = car;
    	}
    }	
    //----------applicationContext.xml配置------------------
    <bean   id="employee" class="com.syj.spring.demo3.Employee" >
    			<constructor-arg name="name"  value="张三" />
    			<constructor-arg name="car"  ref="car" />
    </bean>
    
  • set方法的普通属性注入

    public class Car2 {
    	/*
    	 * 属性注入(构造方式)
    	 */
    	private String name;
    	private double price;
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public void setPrice(double price) {
    		this.price = price;
    	}
    //----------applicationContext.xml配置------------------
    <bean  id="car2" class="com.syj.spring.demo3.Car2" >
    			<property name="name" value="奔驰" />
    			<property name="price" value="1000000" />
    </bean>
    
  • set方法的对象引用的属性注入

    public class Employee2 {
    	private String name;
    	private Car2 car2;
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public void setCar2(Car2 car2) {
    		this.car2 = car2;
    	}
    }
    //----------applicationContext.xml配置------------------
    <bean  id="employee2" class="com.syj.spring.demo3.Employee2" >
    			<property name="name" value="李四" />
    			<property name="car2" ref="car2" />
    </bean>
    
  • p命名空间的属性注入

xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p"<!-- 增加p命名空间-->
	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">

使用p名称空间

<!-- p命名空间的属性注入======================= -->
		<bean id="car2"  class="com.syj.spring.demo3.Car2" p:name="大众" p:price="300000"  ></bean>
  • SpEL的属性注入(Spring3.0以后)

    Spring Expression Language,Spring的表达式语言。

    语法: #{SpEL}

    public class CarInfo {
    	private String name;
    	public String getName() {
    		return "电车";
    	}
    	public double calculatorPrice() {
    		return Math.random() * 3000;
    	}
    }
    //----------applicationContext.xml配置------------------
    <!--  SpEL的属性注入(Spring3.0以后)可以调用方法-->
    <bean  id="carinfo" class="com.syj.spring.demo3.CarInfo" ></bean>
    
    <bean  id="car2" class="com.syj.spring.demo3.Car2" >
        <property name="name" value="#{carinfo.name}" />
        <property name="price" value="#{carinfo.calculatorPrice()}" />
    </bean>
    
    <bean  id="employee2" class="com.syj.spring.demo3.Employee2" >
        <property name="name" value="#{'李四'}" />
        <property name="car2" value="#{car2}" />
    </bean>
    

集合类型的属性注入

配置:

public class CollectionBean {
	private String[] strs;
	private List<String> list;
	private Set<String> set;
	private Map<String, String> map;
	public void setMap(Map<String, String> map) {
		this.map = map;
	}
	public void setSet(Set<String> set) {
		this.set = set;
	}
	public void setList(List<String> list) {
		this.list = list;
	}
	public void setStrs(String[] strs) {
		this.strs = strs;
	}
	@Override
	public String toString() {
		return "CollectionBean [strs=" + Arrays.toString(strs) + ", list=" + list + ", set=" + set + ", map=" + map
				+ "]";
	}
}
//----------applicationContext.xml配置------------------
<!-- 复杂类型的属性注入=============== -->
		<bean  id="collectionBean" class="com.syj.spring.demo4.CollectionBean" >
			<property name="strs">
				<list>
					<value>张三</value>
					<value>李四</value>
					<value>王五</value>
					
				</list>
			</property>
			<property name="list">
				<list>
					<value>111</value>
					<value>222</value>
					<value>333</value>
				</list>
			</property>
			<property name="set">
				<set>
					<value>aaa</value>
					<value>bbb</value>
					<value>ccc</value>
				</set>
			</property>
			<property name="map">
				<map>
					<entry key="张三" value="奥迪" ></entry>
					<entry key="李四" value="奔驰" ></entry>
					<entry key="王五" value="宝马" ></entry>
				</map>
			</property>
		</bean>
		

分模块开发

  • 在加载配置文件的时候,加载多个
    @Test
    	// 分模块开发(多个参数)
    	public void demo1() {
    		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml",
    				"applicationContext2.xml");
    		CollectionBean collectionBean = (CollectionBean) applicationContext.getBean("collectionBean");
    		System.out.println(collectionBean);
    	}
    
  • 在一个配置文件中引入多个配置文件
    <!-- 在其中一个引入另外一个-->
    <import resource="applicationContext2.xml"/>
    

Spring小练习(模拟保存用户关系)

  • 传统的保存客户关系,是在web层new出CustomerServiceImpl对象,然后Service层的。最后Service层调用Dao层。每次在new对象的时候,当需求发生改变的时候我们就需要修改源代码。极其的不合适,于是我们可以通过Spring为我们管理Service层和Dao层的创建,

  • Action实现调用由Spring管理的Service层和在Service注入的Dao层

  • 此项目前台已经基本完成,只需要简单引入即可进行后台的测试

    链接:https://pan.baidu.com/s/1ph1NQ2HDbl3PPqpnJGDTkQ 密码:7oia

    主要引入红色部位

在这里插入图片描述

步骤:

  • 创建web项目,引入相应的jar包

  • 包结构

在这里插入图片描述

  • 创建配置文件Struts2的(web.xml,struts.xml)Spring的(applicationContext.xml)和日志的(log4j.properties)

  • 对页面进行修改menu.jsp的【新增客户】按钮

在这里插入图片描述

  • 创建action

    com.syj.web.action.CustomerAction

    public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
    	private Customer customer = new Customer();
    
    	public String saveUI() {
    		System.out.println("CustomerAction的saveUI方法执行了。。。");
    		return "saveUI";
    	}
    
    
    	@Override
    	public Customer getModel() {
    		return customer;
    	}
    
    }
    
    

    struts.xml配置

    <package name="crm" extends="struts-default" namespace="/" >
    		<action name="customer_*" class="com.syj.web.action.CustomerAction" method="{1}"  >
    			<result  name="saveUI" >/jsp/customer/add.jsp</result>
    		</action>
    </package>
    
  • 创建Service层和Dao层,全都交给Spring管理

    CustomerServiceImpl.java

    public class CustomerServiceImpl implements CustomerService {
    	private CustomerDao customerDao;
    
    	public void setCustomerDao(CustomerDao customerDao) {
    		this.customerDao = customerDao;
    	}
    
    	@Override
    	public void save(Customer customer) {
    		System.out.println("CustomerServiceImpl的save执行了。。。");
    		customerDao.save(customer);
    	}
    }
    

    CustomerDaoImpl.java

    public class CustomerDaoImpl implements CustomerDao {
    
    	@Override
    	public void save(Customer customer) {
    		System.out.println("CustomerDaoImpl的save方法执行了。。。");
    		System.out.println(customer);
    	}
    
    }
    

    对Spring的applicationContext.xml文件进行配置

    <!-- 将CustomerService交给Spring进行管理 -->
    	<bean id="customerService" class="com.syj.service.CustomerServiceImpl" >
    			<!-- 在Service层注入CustomerDaoImpl属性 -->
    		<property name="customerDao"  ref="customerDao" />
    	</bean>
    	<!-- 将CustomerDao交给Spring进行管理 -->
    	<bean id="customerDao" class="com.syj.dao.CustomerDaoImpl" >
    	</bean>
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目标检测(Object Detection)是计算机视觉领域的一个核心问题,其主要任务是找出图像中所有感兴趣的目标(物体),并确定它们的类别和位置。以下是对目标检测的详细阐述: 一、基本概念 目标检测的任务是解决“在哪里?是什么?”的问题,即定位出图像中目标的位置并识别出目标的类别。由于各类物体具有不同的外观、形状和姿态,加上成像时光照、遮挡等因素的干扰,目标检测一直是计算机视觉领域最具挑战性的任务之一。 二、核心问题 目标检测涉及以下几个核心问题: 分类问题:判断图像中的目标属于哪个类别。 定位问题:确定目标在图像中的具体位置。 大小问题:目标可能具有不同的大小。 形状问题:目标可能具有不同的形状。 三、算法分类 基于深度学习的目标检测算法主要分为两大类: Two-stage算法:先进行区域生成(Region Proposal),生成有可能包含待检物体的预选框(Region Proposal),再通过卷积神经网络进行样本分类。常见的Two-stage算法包括R-CNN、Fast R-CNN、Faster R-CNN等。 One-stage算法:不用生成区域提议,直接在网络中提取特征来预测物体分类和位置。常见的One-stage算法包括YOLO系列(YOLOv1、YOLOv2、YOLOv3、YOLOv4、YOLOv5等)、SSD和RetinaNet等。 四、算法原理 以YOLO系列为例,YOLO将目标检测视为回归问题,将输入图像一次性划分为多个区域,直接在输出层预测边界框和类别概率。YOLO采用卷积网络来提取特征,使用全连接层来得到预测值。其网络结构通常包含多个卷积层和全连接层,通过卷积层提取图像特征,通过全连接层输出预测结果。 五、应用领域 目标检测技术已经广泛应用于各个领域,为人们的生活带来了极大的便利。以下是一些主要的应用领域: 安全监控:在商场、银行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值