Spring入门(一)

一、学前基础

  • 前端框架:

JQ、BS、Easyui、AJAX

  • 后端框架:

Spring、SpringMVC、MyBatis>>>>>SSM框架

Spring+Struts2+Hibernate>>>>>>>SSH框架

SpringBoot:对SSM的再一次封装

SpringCloud:微服务

  • 什么是框架:

框架其实就是软件的半成品,框架是将复杂的基础业务进行封装,提供更简洁的API给程序使用。使用框架的目的是提高开发效率以及组件之间的松耦合(耦合度越低越好)

  • Spring框架作用:

  1. 简化开发:之前连接数据库的时候,使用jdbc,需要打开连接,关闭连接等,Spring中有一个Springjdbc,就是简化jdbc的开发。使用Springjdbc就不需要关注数据库连接的过程和关闭连接等。

  2. IOC(控制反转):将创建对象管理对象交给Spring容器实现。

       IOC底层原理使用技术:

          ①xml配置文件

          ②dom4j解决xml

     ③工厂设计模式

     ④反射

       3.集成其他框架(如集成SpringMVC,mybatis,Struts2等),他可以自己单独使用,也可以配合其他框架使用

注:spring是一个一站式框架,spring在javaee三层结构中,每一层都提供不同的解决技术

-----web层:SpringMVC

-----service:Spring的ioc

-----dao层:Spring的jdbcTemplate

使用的版本:Spring4.x

     4.AOP(面向切面编程):扩展功能不是修改源代码实现

     5.解耦

二、如何使用Spring框架进行开发

1. 导入spring框架包 :spring-webmvc

<!--spring框架包,其中这个jar包含springmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>

2.导入junit测试包

      <!-- 单元测试包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

3.创建类,在类里边创建方法

  • 在src/main/java中创建pojo包,在包中创建类写入想要实现的方法(不用创建对象的那种)

例如在此创建了一个User类:

public class User {

public User() {

System.out.println("执行User类的无参构造发方法");

   }

public String getName() {

return "tomcat";

   }

}

4.创建spring配置文件,创建配置类

  • 在项目中将配置文件ApplicationContext.xml放入src/main/resources中并编写配置文件(通过这个配置文件来创建对象)

其实spring核心配置文件名称和位置不是固定的,建议放在src/main/resources中,建议名字为applicationContext.xml

在这个之前得先了解bean标签的常用属性:

  • id属性:起名称,随便写,不能包含特殊符号

  • class属性:创建对象所在类的全路径(通常都是包名.类名

  • name属性:功能和id属性一样,但是name属性可以包含特殊符号

  • scope属性:决定spring管理的对象的作用域,默认为singleton单例模式

例如:在配置文件ApplicationContext.xml中

<bean id="user" class="pojo.User">

在spring中通常有三种创建对象的方法(bean的实例化)

  • 通过无参的构造函数创建对象

 

  • 通过静态工厂模式创建对象

  • 通过非静态的工厂模式创建对象

5.测试对象是否创建

在src/test/java中建立测试包,并在该测试包中建立测试类,在测试类中进行以下步骤:

①第一步:加载配置文件,启动Spring容器

ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");

注意:ApplicationContext是一个接口,接口中定义了spring的API ClassPathXmlApplicationContext是接口的实现类

后边括号里的是配置文件

②从spring容器中取出某个对象

User user = ac.getBean("user",User.class);

System.out.println(user);

String name=user.getName();

System.out.println(name);

如果测试的时候能够正确输出,则说明spring容器创建对象成功

三、Spring管理的对象的作用域

由spring创建的对象默认是单例的(singleton)。但是,在实际的应用当中,我们可以通过scope属性来改变单例模式

Scope=“prototype”时,多例

例如:<!-- springg管理对象的作用域 -->

<bean id="p" class="pojo.Person" scope="prototype"></bean>

scope配置项有5个属性,用于描述不同的作用域。

① singleton【默认】

使用该属性定义Bean时,IOC容器仅创建一个Bean实例,IOC容器每次返回的是同一个Bean实例。

② prototype

使用该属性定义Bean时,IOC容器可以创建多个Bean实例,每次返回的都是一个新的实例。

③ request

该属性仅对HTTP请求产生作用,使用该属性定义Bean时,每次HTTP请求都会创建一个新的Bean,适用于WebApplicationContext环境。

④ session

该属性仅用于HTTP Session,同一个Session共享一个Bean实例。不同Session使用不同的实例。

⑤ global-session

该属性仅用于HTTP Session,同session作用域不同的是,所有的Session共享一个Bean实例。

四、spring的依赖注入(名词DI:依赖注入)

1、什么是依赖注入

通过spring容器,使用一定的方式给对象的属性进行赋值

2、依赖注入的两种方式

①set方法注入(前提是基类中要有set方法)

  • 通过Spring容器给属性赋值

Person类中:

public class Person {
    
    private Integer id;
    private String name;
    private String address;

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
@Override
    public String toString() {
        return "Person [id=" + id + ", name=" + name + ", address=" + address + ”]";
    }
}

applicationContext.xml配置文件中:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<bean id="p" class="pojo.Person">
    <property name="id" value="10"></property>
    <property name="name" value="张山峰"></property>
    <property name="address" value="江苏如皋"></property>
  </bean>
</beans>

测试类中:

public class SpringTest {
    
    @Test
    public void test1() {
        
        ApplicationContext ac=new
                ClassPathXmlApplicationContext("applicationContext.xml");
        
        Person p = ac.getBean("p",Person.class);
        
        System.out.println(p.toString());    
    }
  • 通过Spring容器给list集合赋值

Person类中:

public class User {
    private List list;
    
    @Override
    public String toString() {
        return "User [list=" + list + "]";
    }
    public List getList() {
        return list;
    }
    public void setList(List list) {
        this.list = list;
    }    
}

applicationContext.xml配置文件中;

     <bean id="p" class="pojo.Person">
        <!-- 这个给list集合注入值 -->
    <property name="list">
    <list>
    <value>看电影</value> 
    <value>游泳</value>
    <value>打游戏</value>
    </list>
    </property>
      </bean>    

测试类中:

@Test
	public void test2() {
		
		ApplicationContext ac=new
				ClassPathXmlApplicationContext("applicationContext.xml");
		
		Teacher t = ac.getBean("t",Teacher.class);
		
		System.out.println(t.toString());	
	}

  

  • 通过Spring容器给map集合赋值

<bean id="p" class="pojo.Person">
<!-- 这个是给map集合注入值 -->
    <property name="map">
    <map>
    <entry key="数学" value="90"></entry>
    <entry key="语文" value="80"></entry>
    <entry key="英语" value="70"></entry>
    
    </map>
    </property>
</bean>
  • 通过Spring容器给set集合赋值

<bean id="p" class="pojo.Person">
     <!-- 这个是给set集合注入值 -->
    <property name="set">
    <set>
    <value>国庆节</value>
    <value>中秋节</value>
    <value>端午节</value>
    </set>
    
    </property>
    
    </bean>    
  • 将一个类的对象注入到另一个类中

 例如将Student的值注入到Person类中:

首先要在Person类里边创建一个返回值为Student的属性,并创建get和set方法:

public class Person {
   private Student ss;
   public Student getSs() {
        return ss;
    }
    public void setSs(Student ss) {
        this.ss = ss;
    }
  } 

在applicationContext.xml中:

<!-- 通过set方法将值注入到属性中 -->
    <bean id="p" class="pojo.Person">
    
    <!-- 将Student注入到Person类中         使用ref引用Student的id值 -->
    <property name="ss" ref="stu"></property>


           <!-- 这时student类 -->
    <bean id="stu" class="pojo.Student">
    <property name="id" value="20"></property>
    <property name="name" value="张无忌"></property>
    <property name="score" value="100"></property>
    
    </bean>

最后在测试类里测试:

②有参数的构造器注入(要先在基类中生成构造器)

在基类中:

package pojo;

public class Teacher {
    
    private Integer id;
    private String name;
    private String address;
    @Override
    public String toString() {
        return "Teacher [id=" + id + ", name=" + name + ", address=" + address + "]";
    }
    
    public Teacher(Integer id, String name, String address) {
        super();
        this.id = id;
        this.name = name;
        this.address = address;
    }
}

在applicationContext.xml测试类中:

<!-- 第二种注入方式:构造器注入
    index 表示构造器中参数列表下表位置,从0开始
     -->
    <bean id="t" class="pojo.Teacher">
    <constructor-arg index="0" value="01"></constructor-arg>
    <constructor-arg index="1" value="智障"></constructor-arg>
    <constructor-arg index="2" value="四川"></constructor-arg>
    </bean>

注意:index 表示构造器中参数列表下表位置,从0开始

原文地址:https://www.cnblogs.com/bzbz/p/11580587.html