Spring基本介绍和使用示例
2020-01-15 11:53
441 查看
Spring 是分层的 Java SE/EE 应用 full-stack (一站式)轻量级开源框架,以 IoC(Inverse Of Control:反转控制) 和 AOP(Aspect Oriented Programming: 面向切面编程) 为内核, 提供了展现层 SpringMVC 和持久层 Spring JDBC 以及业务层事务管理等众多的企业级应用技术, 还能整合开源世界众多著名的第三方框架和类库, 逐渐成为使用最多的 Java EE 企业应用开源框架。
1、什么是spring?
spring是一个javaEE的全栈式框架,现在是一个生态体系。
2、spring主要有那几个功能?
1)IoC,也叫控制反转,主要的作用是帮我们创建对象
2)AOP,面向切面,它是面向对象的一种补充
3)声明式事务管理,用于简化对事务的操作
3、如何使用spring?
步骤如下:
1)下载spring相应的jar文件
2)创建一个java工程,并把相应的jar加入到工程中
3)编写spring的配置文件,并在配置文件中定义一个bean对象
4)编写测试程序来测试
spring的配置文件默认名称叫applicationContxt.xml,可以更改
使用示例如下所示:
基于maven项目中pom.xml的配置,需加如下两个依赖文件:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.2.1.RELEASE</version> </dependency>
首先spring的配置文件
spring-config.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的完整路径 (包名加类名) lazy-init:指定加载策略,有以下几个值: 1)default:默认使用的是false 2)true:表示延迟加载 3)false:表示立即加载 此属性只对当前bean有效,要想所有的bean都有效,可以在beans标签中添加 default-lazy-init属性来批量设置。但不推荐使用。 --> <bean id="person" class="com.bean.Person"/> </beans>
再写一个bean对象,此处我写的是Person
package com.bean; public class Person { public Person() { System.out.println("Person-------"); } }
然后在写测试类
package com.test; import org.junit.Test; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import com.bean.Person; @SuppressWarnings("deprecation") public class SpringTest { @Test public void testUseSpring() { //获取spring的配置文件 ClassPathResource resource = new ClassPathResource("spring-config.xml");//配置文件名称 XmlBeanFactory factory = new XmlBeanFactory(resource); //从配置文件中获取指定bean Object bean = factory.getBean("person"); Person person=(Person) bean; } }
最后能够看见person里的构造函数里的输出,就表示成功了。
此外,还可以多种测试方式,如下所示:不过还需在pom.xml文件里再加两个依赖包
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.1.RELEASE</version> </dependency>
测试方法如下:
package com.test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; import com.bean.Person; //@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringRunner.class) @ContextConfiguration("classpath:spring-config.xml") public class SpringTest2 { @Autowired//需加这些注解 private BeanFactory factory; @Test public void test() { // 从配置文件中获取定义bean Object bean = factory.getBean("person"); Person person = (Person) bean; } }
或者如下所示:
package com.test; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bean.Person; /** * 获取Bean的几种方式: * 1、getBean(String name):根据bean的名称来获取。 * 注意:如果是通过BeanFactory来获取资源并创建对象,它是延迟加载, * 而通过ClassPathXmlApplicationContext来获取资源并创建对象是立即加载。 */ public class SpringTest3 { @Test public void test() { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring-config.xml"); //Object obj = ctx.getBean("person1"); //Person person = (Person)obj; //System.out.println(person); } }
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- Spring Aop配置使用示例及实现过程和原理介绍
- shiro大致介绍,shiro-Spring整合细节,shiro基本使用,自定义方法使用shiro
- 使用myeclipse集成struts,hibernate,spring的一个示例程序(转)
- 使用myeclipse集成struts,hibernate,spring的一个示例程序(转)
- 【Android-Emulator】Android模拟器介绍和基本使用
- 使用myeclipse集成struts,hibernate,spring的一个示例程序
- Spring入门示例及相关概念介绍
- 【Android-Emulator】Android模拟器介绍和基本使用
- MongoDB java api for 插入和单collection基本查询使用示例
- Cygwin 介绍 安装 基本使用 交叉编译vivi kernel
- Ant介绍以及基本使用指南
- 传智播客 Spring基本概念介绍与环境搭建
- 通用usb驱动libusb介绍和使用示例
- 【Java.Spring.MVC】Spring使用示例参考手册;MVC注解参考
- Spring系列之三——Spring体验中IOC使用的介绍
- 使用myeclipse集成struts,hibernate,spring的一个示例程序
- ubunut11.0安装nginx基本目录结构介绍和配置使用方法
- Ant介绍以及基本使用指南
- lighttpd的安装和基本模块的使用介绍
- 使用myeclipse集成struts,hibernate,spring的一个示例程序