Spring已更新至第五版,需Java第八版支持。
Spring框架分为多个模块,各模块具有不同功能。核心容器为其主要模块,主要做配置实例与注入机制。不同组件还完成如消息传递,事务数据及持久层控制,与web相关技术支持。故此框架可应用所有层次与流程逻辑,并可融合配置其他各种框架。
Spring中已内嵌基于Servlet的Spring MVC Web框架,可作为前后端交互可用。
Spring自五版本后,Spring MVC框架需JavaEE七版本支持,即Servlet 3.1、JSP 2.1版本以上,并可与Tomcat 8/9与JBoss 7完全兼容。
Spring设计之初,遵循以下设计理念:
为各种操作提供选择,使其尽可能晚地作出设计决策。如在切换持久层时,无需更换代码即可。此仅为持久层,在其他问题中,亦可通过第三方库完成以上操作,使代码耦合度降低。
灵活的应用于各个场景,可作为各种项目及场景所用。
向后兼容,尽量使版本简无重大变革导致兼容问题,精心选择Java版本以维护其库。
悉心设计文档,API与官方文档始终是最好的教材。
包之间没有循环依赖关系,且代码结构整洁明了。
IOC为控制反转,亦称为依赖注入即DI。对象可由Spring通过构造函数,工厂等方式创建对象,并向其中注入依赖项(对象所使用到的对象,例如父类、引用等),后将其放置入程序引用中。
BeanFactory接口能够管理任何类型对象,其子ApplicationContext为其添加以下功能:
更轻松的AOP功能集成。
用于国际化的消息资源处理。
事件发布。
特定层次中的管理,如web应用层所使用的WebApplicationContext。
可通过注解方式或xml配置的方式完成对对象实例的配置与注入操作,以下展示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" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> ? <bean id="..." class="..."> <!-- 对bean/实例的配置书写到此处 --> </bean> <bean id="..." class="..."> <!-- 对bean/实例的配置书写到此处 --> </bean> <!-- 此处可书写更多bean配置 --> ? </beans>
通过id定为一个IOC映射的标识,此标识在一个xml文件中只可存在一个,class用于标识创建的类的全限定名。
创建普通Java项目或Maven项目,其中导入Spring相关依赖(beans+core+context):
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.0.8.RELEASE</version> </dependency> ? <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.0.8.RELEASE</version> </dependency> ? <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.0.8.RELEASE</version> </dependency>
Maven项目下,于resources中下创建application.xml文件,此文件可名称自拟,并可存放于任意路径中,但官方推荐如上文描述。xml文件根元素为beans,其中可添加多个xmlns验证文件网址。在IDEA中,可能出现文件报错情况,可在File-Settinges-Languages&Frameworks-Schemas and DTDs中点击加号添加出现问题的文件网址,点击应用即可消除问题:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> </beans>
创建随意一实例,位于任意包中。在beans中书写bean标签,并制定id与class值,id为此实例唯一标识,class为此类全限定名:
<bean id="TestBean" class="bean.Hello"></bean>
书写测试类,其中获取ApplicationContext接口实现类ClassPathXmlApplicationContext实例并实现多态,使用其getBean方法传入ID名即可获得实例:
ApplicationContext app = new ClassPathXmlApplicationContext("application.xml"); Object o = app.getBean("TestHello"); System.out.println(o);
在bean中,可使用property对单个属性进行配置:
<bean id="TestHello" class="bean.Hello"> <property name="name" value="hello"></property> </bean>
但在进行配置时,务必要求类中存在set方法:
public class Hello { String name; ? public void setName(String name) { this.name = name; } ? @Override public String toString() { return "Hello{name=" + name + "}"; } }
上文显示,可通过toString方法输出实体类中对象信息:
Hello{name=hello}
不仅可以注入基本类型及String类型数据,方可注入对象类型,但属性要给为ref,指定到另一bean标签:
<bean id="TestHello" class="bean.Hello"> <property name="name" value="Father"></property> <property name="hello" ref="HelloSon"></property> </bean> ? <bean id="HelloSon" class="bean.Hello"> <property name="name" value="helloSon"></property> </bean>
可将配置文件分为多份,单元性配置不同内容,使用import 标签与beans中导入使用:
<!-- applicationContext.xml --> <import resource="applicationSonContext.xml"></import> ? <bean id="TestHello" class="bean.Hello"> <property name="name" value="Father"></property> <property name="hello" ref="HelloSon"></property> </bean>
<!-- applicationSonContext.xml --> <bean id="HelloSon" class="bean.Hello"> <property name="name" value="helloSon"></property> </bean>
可使用相对路径与绝对路径(即/开头路径),不建议使用URL路径。推荐使用绝对文件路径,或前缀为class:的路径。
文件路径:file:C:/config/services.xml
类路径:classpath:/config/services.xml
在使用ClassPathXMLApplicationContext对象时,可在参数中多次传入参数,用于读取多个xml文件,并且使用getBean方法时,可在第二个参数初传入某实例class类型,以泛型的形式约束此方法返回的值得类型:
ApplicationContext app = new ClassPathXmlApplicationContext("application.xml","applicationSonContext.xml"); Hello o = app.getBean("TestHello", Hello.class); System.out.println(o);
韶光淑气_Spring_1.0 概述+构建_2020年4月7日17:29:51
原文:https://www.cnblogs.com/agoodjavaboy/p/12656428.html