spring开源框架基础案例入门
发布日期:2021-05-04 01:05:58 浏览次数:28 分类:技术文章

本文共 11392 字,大约阅读时间需要 37 分钟。

Spring初级入门

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

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

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

SHH2/SSM

1.1 中间层框架、万能胶
struts2   spring   hibernate
1.2 容器框架
JavaBean        bean(通常翻译):小豆子,(java里比如)Java对象     IOC(控制反转或依赖注入)和AOP(面向切面)

先修改pom.xml配置文件及源码:

4.0.0
com.zking
test1
war
0.0.1-SNAPSHOT
test1 Maven Webapp
http://maven.apache.org
5.2.12.Final
5.1.44
5.0.1.RELEASE
2.5.13
1.7.7
2.9.1
3.2.0
4.12
4.0.1
1.2
1.1.2
8.5.20
org.hibernate
hibernate-core
${hibernate.version}
org.hibernate
hibernate-c3p0
${hibernate.version}
org.hibernate
hibernate-ehcache
${hibernate.version}
mysql
mysql-connector-java
${mysql.driver.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-orm
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-aspects
${spring.version}
org.aspectj
aspectjrt
1.6.11
org.aspectj
aspectjweaver
1.6.11
cglib
cglib
2.1_3
org.apache.struts
struts2-core
${struts2.version}
commons-io
commons-io
log4j-api
org.apache.logging.log4j
org.apache.struts
struts2-spring-plugin
${struts2.version}
spring-web
org.springframework
spring-beans
org.springframework
spring-context
org.springframework
spring-core
org.springframework
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
jcl-over-slf4j
${slf4j.version}
runtime
org.apache.logging.log4j
log4j-slf4j-impl
${log4j.version}
slf4j-api
org.slf4j
org.apache.logging.log4j
log4j-api
${log4j.version}
org.apache.logging.log4j
log4j-core
${log4j.version}
org.apache.logging.log4j
log4j-web
${log4j.version}
runtime
com.lmax
disruptor
${disruptor.version}
junit
junit
${junit.version}
test
javax.servlet
javax.servlet-api
${servlet.version}
provided
javax.servlet.jsp.jstl
jstl
${jstl.version}
taglibs
standard
${standard.version}
org.apache.tomcat
tomcat-jsp-api
${tomcat-jsp-api.version}
provided
sp01
org.apache.maven.plugins
maven-compiler-plugin
3.7.0
1.7
1.7
UTF-8

2. 什么是控制反转(或依赖注入)

控制反转(IoC=Inversion of Control)IoC,用白话来讲,就是由容器控制程序之间的(依赖)关系,而非传统实现中,由程序代码直接操控。这也就是所谓“控制反转”的概念所在:(依赖)控制权由应用代码中转到了外部容器,控制权的转移,是所谓反转。

IoC还有一个另外的名字:“依赖注入 (DI=Dependency Injection)” ,即由容器动态的将某种依赖关系注入到组件之中

案例:

实现Spring的IoC

在这里插入图片描述

IOC/DI:(IOC控制反转或DI依赖注入)
将以前由程序员实例化对象/赋值的工作交给了spring处理
在这里插入图片描述
在这里插入图片描述

##必须要记的

3. 如何在spring当中定义和配置一个JavaBean(使用无参构造方法+set方法创建一个JavaBean)

3.1 id:在容器中查找Bean的id(唯一、且不能以/开头)
3.2 class:bean的完整类名(反射机制实例化对象)
3.3 name:在容器中查找Bean的名字(唯一、允许以/开头、允许多个值,多个值之间用逗号或空格隔开)

在这里插入图片描述

3.4 scope:(singleton | prototype)默认是singleton
3.4.1 singleton(单例模式):在每个Spring IoC容器中一个bean定义对应一个对象实例
3.4.2 prototype(多例模式):一个bean定义对应多个对象实例

注1: struts2的Action请使用多例模式,因为Action要用来接收参数

在这里插入图片描述

3.4 abstract:将一个bean定义成抽象bean(抽象bean是不能实例化的),抽象类一定要定义成抽象bean,非抽象类也可以定义成抽象bean
3.5 parent:指定一个父bean(必须要有继承关系才行)

在这里插入图片描述

3.6 init-method:指定bean的初始化方法

在这里插入图片描述

3.7 constructor-arg:使用有参数构造方法创建javaBean

在这里插入图片描述

4. 简单属性的配置:

8+1+3

8基础数据+String+3个sql
java.util.Date
java.sql.Date
java.sql.Time
java.sql.Timestamp
通过标签赋值即可

5. 复杂属性的配置

5.1 JavaBean

ref bean=""
在这里插入图片描述
5.2 List或数组
在这里插入图片描述
5.3 Map
在这里插入图片描述
5.4 Properties
在这里插入图片描述

前面案例Student继承了Person源码

package com.zking.test.p1;import java.util.Arrays;import java.util.List;import java.util.Map;import java.util.Properties;public class Student extends Person {	private String sid;	private Address address;private Integer[] arr;private List  list;private Map  map;private Properties properties;public Student() {}public String getSid() {	return sid;}public void setSid(String sid) {	this.sid = sid;}public Address getAddress() {	return address;}public void setAddress(Address address) {	this.address = address;}public Integer[] getArr() {	return arr;}public void setArr(Integer[] arr) {	this.arr = arr;}public List getList() {	return list;}public void setList(List list) {	this.list = list;}public Map getMap() {	return map;}public void setMap(Map map) {	this.map = map;}public Properties getProperties() {	return properties;}public void setProperties(Properties properties) {	this.properties = properties;}@Overridepublic String toString() {	return "Student [sid=" + sid + ", address=" + address + ", arr=" + Arrays.toString(arr) + ", list=" + list			+ ", map=" + map + ", properties=" + properties + ", idcard=" + getIdcard() + "]";}}

Sprin.xml源码:

王五
大明
4325XXXXXXXXXXXXXXXX
001
1
111
222
333
aaa
bbb
ccc
张三
123
王五
456
李四
789
数值1
数值2
娄底

Demol测试类源码:

package com.zking.test.p1;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.zking.test.Model.HelloBean;public class Demo1 {		public static void main(String[] args) {		//Application应用		//Context容器/环境		//不合法的状态,beanFactory没有初始化或者关闭,在上下文中刷新会导致报错。	ApplicationContext  applicationContext = new ClassPathXmlApplicationContext("classpath:spring.xml");	//实例化对象HelloBean指向spring.xml里id名//	HelloBean bean = (HelloBean) applicationContext.getBean("/c");//	//spring控制反转//	bean.hello();//	//实例化对象HelloBean指向spring.xml里id名//	HelloBean zdm = (HelloBean) applicationContext.getBean("zdm");//	//spring控制反转//	zdm.hello();	//	Student s = (Student) applicationContext.getBean("s");//	Employee e = (Employee) applicationContext.getBean("e");//	System.out.println("student:"+s.getSid()+"  employee:"+e.getEid()+"  Person:"+s.getIdcard());Student s = (Student) applicationContext.getBean("s");System.out.println(s);}}

6. 多配置文件(项目中用得到)

//把spring.xml比作为组长

spring.xml
//组员写的xml文件

//组内成员

spring-a.xml/spring-b.xml/spring-c.xml
在这里插入图片描述

通过此案例,我们可以搞清楚spring与web项目的一个集成原理

7. spring与web项目的集成

WEB项目如何读取spring上下文

通过监听器实现SpringtListener源码

package com.zking.test.Util;import javax.servlet.ServletContext;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * 监听器 * 读取Spring上下文的 * @author zjjt * */public class SpringListener implements ServletContextListener {		private String contextConfigLocation="classpath:spring.xml";	@Override	public void contextDestroyed(ServletContextEvent sce) {	}	@Override	public void contextInitialized(ServletContextEvent sce) {	System.out.println("读取Spring上下文,并保存到application作用域里面");	ServletContext application = sce.getServletContext();	// 指定配置文件的路径	String contextConfigLocation = application.getInitParameter("contextConfigLocation");	if (null != contextConfigLocation && !"".equals(contextConfigLocation)) {		this.contextConfigLocation = contextConfigLocation;	}	ApplicationContext applicationContext = new ClassPathXmlApplicationContext(contextConfigLocation);	SpringUtil.setContext(application, applicationContext);}}

再写一个SpringUtil作用域源码:

package com.zking.test.Util;import javax.servlet.ServletContext;import org.springframework.context.ApplicationContext;/** * 作用域 * @author zjjt * */public class SpringUtil {public static final String CONTEGT_KEY = "com.zking.test.Util.SpringUtil";public SpringUtil() {}public static void setContext(ServletContext application,ApplicationContext applicationContext) {	application.setAttribute(CONTEGT_KEY, applicationContext);}public static ApplicationContext getContext(ServletContext application) {	return (ApplicationContext) application.getAttribute(CONTEGT_KEY);}}

contextConfigLocation:classpath:applicationContext-*.xml

在这里插入图片描述
在这里插入图片描述
启动Tomcat/发布web项目
在这里插入图片描述

转载地址:https://blog.csdn.net/weixin_45067120/article/details/105452962 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:实体类返回值出现[属性名为:com.zking.test.p1.Address@7193666c]
下一篇:hibernate-没用的ehcache(二级缓存)

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年03月31日 15时12分29秒