什么是spring,它能够做什么?

273 阅读5分钟

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

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

2. 什么是控制反转(或依赖注入)
控制反转(IoC=Inversion of Control)IoC,用白话来讲,就是由容器控制程序之间的(依赖)关系,而非传统实现中,由程序代码直接操控。这也就是所谓“控制反转”的概念所在:(依赖)控制权由应用代码中转到了外部容器,控制权的转移,是所谓反转。
IoC还有一个另外的名字:“依赖注入 (DI=Dependency Injection)” ,即由容器动态的将某种依赖关系注入到组件之中 ,案例:实现Spring的IoC

第一步:需要添加springIDE插件,配置相关依赖(插件如何安装点击打开链接)
pom.xml (1.spring-context 2.spring-orm 3.spring-web 4.spring-aspects)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>zking</groupId>
  <artifactId>s1</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>s1 Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
        <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>3.8.1</version>
              <scope>test</scope>
        </dependency>
   
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.1.RELEASE</version>
    </dependency>
   
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.0.1.RELEASE</version>
    </dependency>
    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>5.0.1.RELEASE</version>
    </dependency>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.0.1.RELEASE</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>s1</finalName>
  </build>
</project>

第二步:插件Spring的xml文件(右键-->new-->other-->spring-->Spring Bean Configuration File)

注:创建spring的XML文件时,需要添加beans/aop/tx/context标签支持(勾上即可)
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:aop="http://www.springframework.org/schema/aop"
    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.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
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    
    
</beans>

第三步:创建一个helloworld类

package p1;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class HelloWorld {
    private String name;
 
    public HelloWorld() {
        super();
        System.out.println("new HelloWorld()");
    }
    
    public HelloWorld(String name) {
        super();
        this.name = name;
    }
 
    public void init() {
        System.out.println("init.......");
    }
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }    
}

3. 如何在spring当中定义和配置一个JavaBean
使用无参构造方法+set方法创建一个JavaBean
1 id:在容器中查找Bean(对象)的id(唯一、且不能以/开头)
2 class:bean(对象)的完整类名
3 name:在容器中查找Bean(对象)的名字(唯一、允许以/开头、允许多个值,多个值之间用逗号或空格隔开)
4 scope:(singleton|prototype)默认是singleton
4.1 singleton(单例模式):在每个Spring IoC容器中一个bean定义对应一个对象实例
4.2 prototype(原型模式/多例模式):一个bean(对象)定义对应多个对象实例
4 abstract:将一个bean定义成抽象bean(抽象bean是不能实例化的),抽象类一定要定义成抽象bean,非抽象类也可以定义成抽象bean
5 parent:指定一个父bean(必须要有继承关系才行)
6 init-method:指定bean对象()的初始化方法
7 使用有参数构造方法创建javaBean(java对象):constructor-arg

第四步:在xml中创建bean(看不懂属性的,在第三点中有介绍)

<bean id="helloworld" class="p1.HelloWorld" scope="prototype" name="a b c" init-method="init">
        <property name="name">
            <value>zs</value>
        </property>
</bean>
    
    <bean id="helloworld2" class="p1.HelloWorld">
        <constructor-arg index="0">
            <value>zzz</value>
        </constructor-arg>
    </bean>

第五步:写一个测试的类即可

public static void main(String[] args) {
        //以前的写法
        HelloWorld helloWorld=new HelloWorld();
        helloWorld.setName("张三");
        System.out.println("hello"+helloWorld.getName());
        //-------------------------------------------------------------
        //Spring
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("ApplicationContext.xml");
        HelloWorld a = (HelloWorld)applicationContext.getBean("a");
        System.out.println("你好: "+a.getName());
        
        HelloWorld b = (HelloWorld)applicationContext.getBean("b");
        System.out.println("你好: "+b.getName());
        
        HelloWorld c = (HelloWorld)applicationContext.getBean("c");
        System.out.println("你好: "+c.getName());
        
        HelloWorld d = (HelloWorld)applicationContext.getBean("helloworld2");
        System.out.println("--------------------------------");
        System.out.println("你好: "+d.getName());
    }

4. 简单属性的配置:
8+1+3:8大基本数据类型+String+3个sql
java.util.Date java.sql.Date java.sql.Time java.sql.Timestamp
通过<value>标签赋值即可

5. 复杂属性的配置
5.1 JavaBean ref bean=""
5.2 List或数组
5.3 Map
5.4 Properties
创建一个学生类(Student),定义这几个属性

private HelloWold helloworld;

private String []arr;
private List list;
private Map map;
private Properties properties;
在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:aop="http://www.springframework.org/schema/aop"
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.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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<bean id="helloworlds" class="p1.HelloWold">
<property name="name">
<value>张三</value>
</property>
 
</bean>
<bean id="ss" class="p1.Student">
<property name="helloworld">
<ref bean="helloworlds"><!-- ref引用另一个对象 -->
</property>
 
<property name="arr">
<list>
<value>aa</value>
<value>bb</value>
<value>cc</value>
<value>dd</value>
</list>
</property>
<property name="list">
<list>
<value>11</value>
<value>22</value>
<value>33</value>
</list>
</property>
<property name="map">
<map>
<entry>
<key>
<value>zs</value>
</key>
<value>张三</value>
</entry>
<entry>
<key>
<value>ls</value>
</key>
<value>李四</value>
</entry>
<entry>
<key>
<value>ww</value>
</key>
<value>王五</value>
</entry>
</map>
</property>
<property name="properties">
<props>
<prop key="a2">222</prop>
</props>
</property>
 
</bean>

6. 针对项目,配置文件路径的2种写法
ApplicationContext

String path = "applicationContext.xml";(独自开发)

String path = "classpath:applicationContext-*.xml";//src(分模块开发 多人开发)