什么是spring,它能够做什么?
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.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://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());//-------------------------------------------------------------//SpringApplicationContext 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(分模块开发 多人开发)
------------如果大家喜欢我的博客,可以点击左上角的关注哦。
什么是spring,它能够做什么?相关推荐
- 从架构演进的角度聊聊Spring Cloud都做了些什么?
Spring Cloud作为一套微服务治理的框架,几乎考虑到了微服务治理的方方面面,之前也写过一些关于Spring Cloud文章,主要偏重各组件的使用,本次分享主要解答这两个问题:Spring Cl ...
- Spring整合Redis做数据缓存(Windows环境)
当我们一个项目的数据量很大的时候,就需要做一些缓存机制来减轻数据库的压力,提升应用程序的性能,对于java项目来说,最常用的缓存组件有Redis.Ehcache和Memcached. Ehcache是 ...
- Spring Security可以做的十件事
一 您可以在Spring XML配置文件中指定您选择的授权提供者. 您可以通过配置Spring的http://www.springframework.org/schema/security/sprin ...
- 什么是spring框架?spring框架到底有什么用?spring框架到底做了些什么?
什么是spring框架,spring框架究竟有什么用呢?我们可以用spring框架来做些什么呢?这是我今天要说的内容. 当然,百度spring框架会出现一大堆spring框架的介绍,以及IOC和AOP ...
- 中value大小_如何在Spring/SpringBoot 中做参数校验?你需要了解的都在这里!
数据的校验的重要性就不用说了,即使在前端对数据进行校验的情况下,我们还是要对传入后端的数据再进行一遍校验,避免用户绕过浏览器直接通过一些 HTTP 工具直接向后端请求一些违法数据. 本文结合自己在项目 ...
- spring整合aspects做aop演示
第一步:接入spring 和aspects依赖 <dependency> <groupId>org.springframework</groupI ...
- Spring整合MyBatis做 student crud
把mybatis框架和spring集成在一起,向一个框架一样使用. 用的技术是:ioc . 为什么ioc:能把mybatis和spring集成在一起,像一个框架, 是因为ioc能创建对象. 可以把my ...
- Spring Boot 项目如何做性能监控?
来源:zhaoyh's blog,http://zhaoyh.com.cn/ Spring Boot特别适合团队构建各种可快速迭代的微服务,同时为了减少程序本身监控系统的开发量,Spring Boot ...
- spring的事务隔离_再深一点:面试工作两不误,源码级理解Spring事务
原创:小姐姐味道(微信公众号ID:xjjdog),欢迎分享,转载请保留出处. Spring有5种隔离级别,7种传播行为.这是面试常问的内容,也是代码中经常碰到的知识点.这些知识枯燥而且乏味,其中有些非 ...
- spring 单例 获取多例的位_Spring 获取单例流程(一)
读完这篇文章你将会收获到 在 getBean 方法中, Spring 处理别名以及 factoryBean 的 name Spring 如何从多级缓存中根据 beanName 获取 bean Spri ...
最新文章
- js:appendChild、insertBefore和insertAfter
- 上传jar到nexus的thirdparty第三方库
- 第十届四川省大学生程序设计竞赛
- 手机抓包app_Python爬取网站上面的数据很简单,但是如何爬取APP上面的数据呢
- opencv利用矩形框选中某一区域_【从零学习OpenCV】4Ubuntu系统中安装OpenCV 4
- MVC架构设计——EF-Code First
- Codeforces 474 D. Flowers
- 27. Remove Element[E]移除元素
- php和mysql web开发 5_WEB开发圣经:PHP和MySQL Web开发(原书第5版) 原版pdf+完整源码...
- 搜狗浏览器智慧版_4月浏览器份额榜单出炉:Edge登上第二、Chrome第一无人撼动...
- 博客园电子期刊2010年2月刊发布啦
- 基于J2EE的门诊挂号收费系统设计与实现.rar(毕业论文+程序源码+数据库文件)
- Jenkins自动化构建Gitee项目
- 特斯拉-数据分析师-SQL笔试题
- 通过AndroidStudio找到数据库文件
- java ajax传递到action_day8——ajax传参到action(Struts2)
- MySQL查询不同年份母亲节_日期习俗大不同,走近七个国家的母亲节!
- 99%网赚精准引流技巧!
- 微信小程序font-family中提供的十四种字体
- CSS 实现切角效果