spring的开发
spring与web的整合
1. 整合的原理:
Spring容器随着tomcat容器ServletContext的启动而启动,并且在初始化完成后放到整个应用都可以访问的范围。
ApplicationContext随着服务器的启动而启动,可以借助与Servlet/Filter/Listener任何一个;
把创建好的ApplicationContext放到ServletContext中,整个应用范围,想怎访问就怎么访问;
spring-web-4.2.4.RELEASE.jar包中提供了Listener的支持(spring建议的方式)
WebApplicationContextUtils是Spring官方提供的一个web环境下便捷从ServletContext中获取ApplicationContext的工具类。
2. 整合的步骤
● 添加依赖
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
● 配置监听器,监听web项目的启动
<!-- needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> <!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
● 创建servlet,在servlet中获取spring容器
public class ContextServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1. 获取spring容器
// 获取servletContext域对象
ServletContext servletContext = getServletContext();
// web环境的spring容器
WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
// 2. 从容器中获取javabean对象
Person person = (Person) applicationContext.getBean("person4");
System.out.println(person.getUsername());
response.getWriter().append("Served at: ").append(request.getContextPath());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
● 访问servlet进行测试
浏览器访问:http://localhost:8080/ContextServlet
spring容器基于注解方式的开发
● 优缺点:
缺点:
1、如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大;如果按需求分开.xml文件,那么.xml文件又会非常多。总之这将导致配置文件的可读性与可维护性变得很低。
2、在开发中在.java文件和.xml文件之间不断切换,是一件麻烦的事,同时这种思维上的不连贯也会降低开发的效率。
优点:
Spring基于注解方式的开发,通过"@XXX"的方式,让注解与Java Bean紧密结合,既大大减少了配置文件的体积,又增加了Java Bean的可读性与内聚性。
● 开发步骤:
导入依赖
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
添加context命名空间的schema

开启注解扫描context:component-scan
<!-- 开启spring的注解扫描 -->
<!-- 扫描包下带注释的类及类中带注释的属性 -->
<context:component-scan base-package="spring.annotation.demo"></context:component-scan>
添加注解
Spring中和注入相关的常见注解有Autowired、Resource、Qualifier、Service、Controller、Repository、Component。
- Autowired是自动按照类型注入,自动从spring的上下文找到合适的bean来注入
- Resource用来指定名称注入
- Qualifier和Autowired配合使用,指定bean的名称
- Service,Controller,Repository分别标记类是Service层类,Controller层类,数据存储层的类,spring扫描注解配置时,会标记这些类要生成bean。
- Component是一种泛指,标记类是组件,spring扫描注解配置时,会标记这些类要生成bean。
上面的Autowired和Resource是用来修饰字段,并做属性注入的。而Service,Controller,Repository,Component则是用来修饰类,标记这些类要生成bean。
@Repository
public class CarDao {
public void insertCar(){
System.out.println("CarDao中的insertCar的方法被调用... ...");
}
}
@Service
//@Component
@Scope(value="singleton")
public class CarService {
// 自动按照类型注入,自动从spring上下文中找到合适的bean来注入
@Autowired
private CarDao carDao; public void insertCar(){
carDao.insertCar();
System.out.println("CarService中的insertCar方法");
} @PostConstruct
public void init(){
System.out.println("init方法被调用... ...");
} @PreDestroy
public void destory(){
System.out.println("destory方法被调用... ...");
}
}
编写测试代码
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-annotation.xml");
CarService bean = (CarService) applicationContext.getBean("carService");
bean.insertCar();
}
@Autowired + @Qualifier 结合使用:指定注入bean的名称
@Resouce注解:@Resource注解实现的效果和@Autowired+@Qualifier的效果是一样的
@Scope用于指定scope作用域的(用在类上)
@PostConstruct用于指定初始化方法(用在方法上)
@PreDestory用于指定销毁方法(用在方法上)
@Value 可以为我们的基本数据类型以及string进行属性赋值
xml配置方式与注解方式的对比

spring与Junit整合测试
整合后测试的优点
整合后测试变得非常简单
避免频繁、重复的容器加载
整合Junit的步骤
导入spring-test的jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
编写Junit整合测试类
@RunWith(SpringJUnit4ClassRunner.class)//使用Junit4进行测试
@ContextConfiguration("classpath:applicationContext-annotation.xml")//加载配置文件
public class JunitSpringTest {
@Autowired
private CarService carService; @Autowired
private CarDao carDao; @Test
public void test01(){
carDao.insertCar();
}
}
spring的开发的更多相关文章
- Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->使用Spring Framework开发自己的应用程序
1.直接基于spring framework开发自己的应用程序: 1.1参考资料: Spring官网spring-framework.4.3.5.RELAESE的Reference Documenta ...
- 用Spring MVC开发简单的Web应用
这个例子是来自于Gary Mak等人写的Spring攻略(第二版)第八章Spring @MVC中的一个例子,在此以学习为目的进行记录. 问题:想用Spring MVC开发一个简单的Web应用, 学习这 ...
- Annotation(三)——Spring注解开发
Spring框架的核心功能IoC(Inversion of Control),也就是通过Spring容器进行对象的管理,以及对象之间组合关系的映射.通常情况下我们会在xml配置文件中进行action, ...
- spring注解开发中常用注解以及简单配置
一.spring注解开发中常用注解以及简单配置 1.为什么要用注解开发:spring的核心是Ioc容器和Aop,对于传统的Ioc编程来说我们需要在spring的配置文件中邪大量的bean来向sprin ...
- Spring Boot——开发新一代Spring应用
Spring官方网站本身使用Spring框架开发,随着功能以及业务逻辑的日益复杂,应用伴随着大量的XML配置文件以及复杂的Bean依赖关系.随着Spring 3.0的发布,Spring IO团队逐渐开 ...
- Spring MVC + Spring + Mybitis开发Java Web程序基础
Spring MVC + Spring + Mybitis是除了SSH外的另外一种常见的web框架组合. Java web开发和普通的Java应用程序开发是不太一样的,下面是一个Java web开发在 ...
- 使用Spring Boot开发Web项目(二)之添加HTTPS支持
上篇博客使用Spring Boot开发Web项目我们简单介绍了使用如何使用Spring Boot创建一个使用了Thymeleaf模板引擎的Web项目,当然这还远远不够.今天我们再来看看如何给我们的We ...
- JAX-RS和 Spring 整合开发
JAX-RS 和 和 Spring 整合开发 1.建立maven项目 2.导入maven坐标 <dependencies> <!-- cxf 进行rs开发 必须导入 --> & ...
- Spring Cloud开发实践 - 01 - 简介和根模块
简介 使用Spring Boot的提升主要在于jar的打包形式给运维带来了很大的便利, 而Spring Cloud本身的优点不是那么明显, 相对于Dubbo而言, 可能体现在跨语言的交互性上(例如可以 ...
- Spring 集成开发工具(STS)安装及配置
安装 spring 集成开发工具,下载地址:https://spring.io/tools 下载后,解压,双击 STS ,运行. 如果提示: 去oracle的网站上下载了1.8版本的jdk,下载地址如 ...
随机推荐
- ubuntu系统下如何切换输入法
如何切换输入法:ctrl+空格键 输入中文时如何翻页:键盘上的 - +两个键 super表示:美式键盘中的Win键
- 如何写javascript代码隐藏和显示这个div
如何写javascript代码隐藏和显示这个div 浏览次数:82次悬赏分:10 | 解决时间:2011-4-21 14:41 | 提问者:hade_girl <div id="div ...
- bzoj3224 普通平衡树 splay模板
题目传送门 题目大意:完成一颗splay树. 思路:模板题,学着还是很有意思的. 学习splay树:蒟蒻yyb 该题模板:汪立超 #include<bits/stdc++.h> #defi ...
- C# 关于时区的操作
有关时区自动更新的 在注册表以下路径,start键值3,4表示自动/不自动更新 计算机\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\tza ...
- UVALive - 3942 左儿子trie DP
题意:白书P209 本题用普通字典树会更快,为了练习还是尝试再敲一遍左儿子-右兄弟字典树(其实就是字典树上开前向星) dp[i]为满足[i...len)的分配方案数,转移方程为dp[i]=sum{dp ...
- web app与app的区别,即html5与app的区别
公司准备要做一个项目,是p2p配资的app.在网上问了一些人后,发现有的是直接有html5做好后,用软件封装的.之前我学过app的开发,当时Android版本的,知道开发Android app时写的代 ...
- java se系列(三) 顺序语句、if...else、switch、While、do-while、for、break、continue
1 顺序语句 语句:使用分号分隔的代码称作为一个语句. 注意:没有写任何代码只是一个分号的时候,也是一条语句,称作空语句. 顺序语句就是按照从上往下的顺序执行的语句. 2 判断(if…else) 什么 ...
- C++ 调用Python3
作为一种胶水语言,Python 能够很容易地调用 C . C++ 等语言,也能够通过其他语言调用 Python 的模块. Python 提供了 C++ 库,使得开发者能很方便地从 C++ 程序中调用 ...
- Maven---pom.xml 详解(转)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- Html练习 | 小影志首页练习
<!DOCTYPE html> <head> <title>小影志首页练习</title> <style> /*应用全页字体*/ .pg-f ...