Spring入门
1. Spring简介
1.1 Spring是什么?
Spring是一个开源框架,它由Rod Johnson创建。它是为了解决企业应用开发的复杂性而创建的。
1.2 它能够做什么?
Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。
Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。
1.3 它的功能与目的 范围
目的:解决企业应用开发的复杂性
功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
两大重要功能 IOC:控制反转 AOP:切面编程
范围:任何Java应用
重点:Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
1.4 什么是控制反转 IOC?
这里解释上面的功能,控制反转(将以前由程序员实例化对象/赋值的工作交给了spring处理)
控制反转(IoC=Inversion of Control)IoC,用白话来讲,就是由容器控制程序之间的(依赖)关系,而非传统实现中,
由程序代码直接操控。这也就是所谓“控制反转”的概念所在:(依赖)控制权由应用代码中转到了外部容器,控制权的转移,是所谓反转。
IoC还有一个另外的名字:“依赖注入 (DI=Dependency Injection)” ,即由容器动态的将某种依赖关系注入到组件之中
2. Spring中包括的模块图
基于Spring Core(Spring是一个容器框架)
3.Spring初步使用
3.1 项目创建(Maven)
项目创建与配置详情请看Maven配置与环境搭建
3.2 添加项目依赖(pom.xml)
<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>com.tanle</groupId>
<artifactId>Spring</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Spring Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<hibernate.version>5.2.12.Final</hibernate.version>
<mysql.driver.version>5.1.44</mysql.driver.version>
<spring.version>5.0.1.RELEASE</spring.version>
<struts2.version>2.5.13</struts2.version>
<slf4j.version>1.7.7</slf4j.version>
<log4j.version>2.9.1</log4j.version>
<disruptor.version>3.2.0</disruptor.version>
<junit.version>4.12</junit.version>
<servlet.version>4.0.1</servlet.version>
<jstl.version>1.2</jstl.version>
<standard.version>1.1.2</standard.version>
<tomcat-jsp-api.version>8.5.20</tomcat-jsp-api.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!--1. hibernate相关依赖 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.driver.version}</version>
</dependency>
<!--2. spring相关(5.0.1.RELEASE) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.1_3</version>
</dependency>
<!-- end -->
<!--3. struts2相关(2.5.13) -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts2.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-io</artifactId>
<groupId>commons-io</groupId>
</exclusion>
<exclusion>
<artifactId>log4j-api</artifactId>
<groupId>org.apache.logging.log4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>${struts2.version}</version>
<exclusions>
<exclusion>
<artifactId>spring-web</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>spring-beans</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>spring-context</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>spring-core</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<!--4. log配置:Log4j2 + Slf4j -->
<!-- slf4j核心包 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
<scope>runtime</scope>
</dependency>
<!--用于与slf4j保持桥接 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j.version}</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!--核心log4j2jar包 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
<!--web工程需要包含log4j-web,非web工程不需要 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>${log4j.version}</version>
<scope>runtime</scope>
</dependency>
<!--需要使用log4j2的AsyncLogger需要包含disruptor -->
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>${disruptor.version}</version>
</dependency>
<!--5. other -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
<!--6. jstl -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>${standard.version}</version>
</dependency>
<!-- 7. jsp自定义标签依赖 (必须与tomcat的版本一致) -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
<version>${tomcat-jsp-api.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>Spring</finalName>
<plugins>
<!--第一步就是配置maven-compiler-plugin插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
3.3 简单测试(Spring.xml配置解释)
Spring.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">
<import resource="/springa.xml"></import>
<import resource="/springb.xml"></import>
</beans>
分包分模块
Springa.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">
<!--
id:在容器中查找Bean的id(唯一、且不能以/开头)
name:在容器中查找Bean的名字(唯一、允许以/开头、允许多个值,多个值之间用逗号或空格隔开)
class:bean的完整类名
scope:(singleton|prototype)默认是singleton
1.singleton(单例模式):在每个Spring IoC容器中一个bean定义对应一个对象实例
2.prototype(原型模式/多例模式):一个bean定义对应多个对象实例
parent:指定一个父bean(必须要有继承关系才行)
abstract:将一个bean定义成抽象bean(抽象bean是不能实例化的),抽象类一定要定义成抽象bean,非抽象类也可以定义成抽象bean
constructor-arg:使用有参数构造方法创建javaBean
注1:必须提供有参的构造方法
注2:必须提供无参的构造方法
init-method:指定bean的初始化方法
-->
<bean id="demo1" name="d1" class="com.tanle.test.Demo1" scope="singleton">
<property name="name">
<value>oop</value>
</property>
</bean>
<!-- 人类 -->
<bean id="p1" class="com.tanle.entity.Person" scope="prototype" abstract="true">
<property name="name">
<value>p1</value>
</property>
</bean>
<!-- 学生类 -->
<bean id="s1" class="com.tanle.entity.Student" scope="singleton" parent="p1">
<property name="sid">
<value>s001</value>
</property>
<property name="addr">
<!-- 引用一个bean -->
<ref bean="address" />
</property>
<property name="list">
<list>
<value>1</value>
<value>2</value>
<value>3</value>
</list>
</property>
<property name="map">
<map>
<entry>
<key>
<value>zs</value>
</key>
<value>张三</value>
</entry>
</map>
</property>
</bean>
<!-- 工人类 -->
<bean id="w1" class="com.tanle.entity.Work" scope="singleton" parent="p1">
<property name="wid">
<value>w001</value>
</property>
</bean>
<bean id="w2" class="com.tanle.entity.Work" scope="singleton" parent="p1" init-method="init">
<constructor-arg index="0">
<value>w0088</value>
</constructor-arg>
</bean>
</beans>
概解
Springb.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="address" class="com.tanle.entity.Address" scope="singleton">
<property name="city">
<value>长沙</value>
</property>
</bean>
</beans>
Demo测试类
package com.tanle.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tanle.entity.Person;
import com.tanle.entity.Student;
import com.tanle.entity.Work;
public class Demo1 {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Demo1() {
super();
System.out.println("单例模式对象实例化");
}
public static void main(String[] args) {
//传统new对象
/*Demo1 demo1=new Demo1();
demo1.setName("zs");
System.out.println(demo1.getName());*/
//读取spring配置文件
//得到应用上下文
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("/spring.xml");
//根据name拿的值
//Demo1 d1 = (Demo1) applicationContext.getBean("d1");
//System.out.println(d1.getName());
//根据id
//Demo1 d2 = (Demo1) applicationContext.getBean("demo1");
//System.out.println(d2.getName());
//人类
//Person p1=(Person) applicationContext.getBean("p1");
//System.out.println(p1);
//学生
Student s1 = (Student) applicationContext.getBean("s1");
System.out.println(s1);
//工人
/*Work w1 = (Work) applicationContext.getBean("w1");
System.out.println(w1);*/
Work w2 = (Work) applicationContext.getBean("w2");
System.out.println(w2);
}
}
测试结果
4. 总结
1.Spring是一个开源框架
2.Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
3.实例化对象与反射赋值都可以直接交给Spring(配置好xml文件)
4.最后谢谢观看!有不足欢迎指出!