Spring-----注解开发和Spring测试单元
一、注解开发
- 导入jar包;spring-aop-xxx.jar
- 导入约束:(在官方文档xsd-configuration.html可找)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> - xml中配置扫描开关:
<context:component-scan base-package="com.itheima"/>
- 1. ioc注解
/*
1. @Component组件,表示要托管这个类,让spring创建这个类的实例。 括号里面的us 其实就是id标识符 2. @Component 是通用的注解, 对任何托管的类都可以使用,但是spring为了迎合三层架构,所以对每一层
也给出了具体的注解。 Action --- @Controller
Service --- @Service
Dao --- @Repository :仓库, 建议: 如果以后托管三层中的类,请使用具体对应的注解,如果托管的是普通的其他类。@Component 3. 默认生成的实例还是单例,如果想做成多例,那么还得添加一个注解
@Scope("prototype") 4. @PostConstruct //初始化实例的时候调用
@PreDestroy //销毁实例之前,调用 5. 如果使用注解托管某一个类,不写属性值,那么默认的id标识符就是类的名字(首字母是小写) userServiceImpl 6.<!-- 如果想要扫描多个包,就写一个通用的前缀即可 -->
<context:component-scan base-package="com.itheima"/> */
DI注解
使用注解来完成依赖注入。 一般注解注入,它针对的点是对象的注入。 spring针对对象的注入,提供了两个注解 @Resource 和 @Autowired - 常用的注解就两个 @Resource & @Autowired @Resource(name="ud") 根据给定的标记找到对应的类,创建对象,注入进来。 @Autowired 自动装配,会找到对应的实现类创建对象,注入进来。但是如果存在多个实现,那么会抛出异常
二、Spring测试单元
- 步骤:导入jar包-->托管业务逻辑类-->在测试类上打上注解 ,给测试类的成员变量注入值
//spring扩展了junit的运行环境,除了有测试功能之外,还在里面定义了创建工厂的代码
@RunWith(SpringJUnit4ClassRunner.class) //告诉spring的测试环境,配置文件在哪里
@ContextConfiguration("classpath:applicationContext.xml")
public class TestUserService { //测试类里面出现的注解,不用打开扫描开关。因为这个测试环境里面,它会解析这个测试类的注解。
@Autowired
private UserService userService;
@Test
public void testSave(){
userService.save();
}
}
Spring-----注解开发和Spring测试单元的更多相关文章
- spring注解开发:容器中注册组件方式
1.包扫描+组件标注注解 使用到的注解如下,主要针对自己写的类 @Controller @Service @Repository @Component @ComponentScan 参考 spring ...
- Spring注解开发系列Ⅵ --- AOP&事务
注解开发 --- AOP AOP称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等待,Struts2的拦截器设计就是基于AOP的思想,横向重复,纵向抽取.详细的AO ...
- 浅尝Spring注解开发_自定义注册组件、属性赋值、自动装配
Spring注解开发 浅尝Spring注解开发,基于Spring 4.3.12 包含自定义扫描组件.自定义导入组件.手动注册组件.自动注入方法和参数.使用Spring容器底层组件等 配置 @Confi ...
- 浅尝Spring注解开发_Bean生命周期及执行过程
Spring注解开发 浅尝Spring注解开发,基于Spring 4.3.12 包含Bean生命周期.自定义初始化方法.Debug BeanPostProcessor执行过程及在Spring底层中的应 ...
- 浅尝Spring注解开发_AOP原理及完整过程分析(源码)
浅尝Spring注解开发_AOP原理及完整过程分析(源码) 浅尝Spring注解开发,基于Spring 4.3.12 分析AOP执行过程及源码,包含AOP注解使用.AOP原理.分析Annotation ...
- Spring注解开发_Spring容器创建概述
浅尝Spring注解开发_Spring容器创建概述 浅尝Spring注解开发,基于Spring 4.3.12 概述Spring容器创建的过程,包括12个方法的执行 浅尝Spring注解开发_自定义注册 ...
- 浅尝Spring注解开发_Servlet3.0与SpringMVC
浅尝Spring注解开发_Servlet 3.0 与 SpringMVC 浅尝Spring注解开发,基于Spring 4.3.12 Servlet3.0新增了注解支持.异步处理,可以省去web.xml ...
- Annotation(三)——Spring注解开发
Spring框架的核心功能IoC(Inversion of Control),也就是通过Spring容器进行对象的管理,以及对象之间组合关系的映射.通常情况下我们会在xml配置文件中进行action, ...
- spring注解开发中常用注解以及简单配置
一.spring注解开发中常用注解以及简单配置 1.为什么要用注解开发:spring的核心是Ioc容器和Aop,对于传统的Ioc编程来说我们需要在spring的配置文件中邪大量的bean来向sprin ...
- JAVAEE——SSH项目实战06:统计信息管理、Spring注解开发和EasyUI
作者: kent鹏 转载请注明出处: http://www.cnblogs.com/xieyupeng/p/7190925.html 一.统计信息管理 二.Spring注解开发 1.service ...
随机推荐
- AWK教程
1. IBM:GAWK入门:AWK语言基础 2. Unix AWK使用手册 3. 台湾中研院计算中心ASPAC计划之AWK程序介绍 4. Study-area之AWK 5. AWK学习笔记——酷勤 持 ...
- 暴力破解unix/linux平台上采用crypt加密的口令
# coding=utf-8 ''' 暴力破解crypt模块加密的密码 ''' import crypt import optparse usage = 'Usage: %prog [optinos] ...
- Bash Shell 解析路径获取文件名称和文件夹名
前言 还是今天再写一个自己主动化打包脚本.用到了从路径名中获取最后的文件名称.这里记录一下实现过程. 当然,最后我也会给出官方的做法.(ps:非常囧,实现完了才发现原来Bash Shell有现成的函数 ...
- Nyquist–Shannon sampling theorem 采样定理
Nyquist–Shannon sampling theorem - Wikipedia https://en.wikipedia.org/wiki/Nyquist%E2%80%93Shannon_s ...
- JSON: Circular Dependency Errors
If you’re using Object Relational Mapping frameworks like Hibernate, and are using the bi-directiona ...
- Object Query Language (OQL) query 基本使用
OQL是对java堆内存进行查询分析 使用方法: 1.查看java线程id jps -lvm 上图红框中的为pid: 2.运行命令-导出堆内存信息-会生成一份文件在你本地 jmap -dump:liv ...
- YTU 2559: “心脏出血”
2559: "心脏出血" 时间限制: 1 Sec 内存限制: 128 MB 提交: 5 解决: 2 题目描述 2014年4月,一个开源加密库OpenSSL的严重漏洞" ...
- sphinx测试数据生成
import json from random import sample, randint from uuid import uuid4 def gen_random_words(): with o ...
- linux环境下oracle静默安装
一.安装环境 1.linux版本:redhat6.3_x86_64 2.oracle版本:Oracle Database 11g Enterprise Edition Release 11.2.0.3 ...
- 思维导图js
http://baike.baidu.com/view/1469162.htm?fromenter=Mind+Maps