Spring的基础注解
Spring的基础注解
1、注解的概述
注解是为了便于程序的调试而用于代替配置文件的一种程序语法,与配置文件具有互换性。通常基于注解编程的程序更加简洁。
(注:使用Spring注解必须导入aop包)
2、Spring组件注解
Spring组件注解作用在类上,用于声明该类为组件类。在Spring 框架启动时,自动扫描组件类创建实例对象并存放在Spring容器中。Spring的组件注解有
①@Controller:声明表示层的组件类; ②@Service:声明服务层的组件类;
③@Repository:声明持久层的组件类; ④@Component:声明其它的组件类。
(注:①Spring的4个组件注解在功能上是没有差别的,交换使用结果相同;②组件类的对象名默认为类名的首字母小写形式,组件注解的value属性可以设置对象名)
3、<context:component-scan>标签——扫描器的配置
——base-package属性:指定需要扫描的包。
<context:component-scan base-package="cn"/>
4、Spring依赖注入的注解
(1)@Autowired注解——自动注入容器的对象
①在属性上注入:注入该属性; ②在方法上注入:注入该方法的参数; ③在构造方法上注入:注入该构造方法的参数;
(注:①@Autowired 注解默认允许注入的对象为空,可以通过equired属性设置;②添加@Autowired 注解的方法和构造方法在项目启动的时候将自动执行;③无参的方法和构造方法不支持使用@Autowired注解)
(2)@Qualifier注解——设置注入的对象名
——value属性:指定注入 Spring 容器中的对象名;
(注:@Autowired没有指定对象名的属性,只能通过@Qualifier注解指定容器中的对象名)
(3)@Resource注解——注入容器的对象
——name属性:指定注入 Spring 容器中的对象名;
(注:①@Resource注解在功能上是@Autowired和@Qualifier注解的合并注解;②@Resource不能在构造方法上注入)
(4)@Value注解——注入标量数据
——@Value注解用于注入标量数据,并支持注入Properties文件的键值对。
public class Manager {
@Value("zhangsan")
private String name;
@Value("${manager.age}")
private int age;
@Autowired
@Qualifier(value="customer01")
private Customer customer = null;
@Autowired(required=false)
public Manager(Customer customer) {
super();
this.customer = customer;
}
@Resource(name="customer01")
public void saveCustomer(Customer customer) {
this.customer = customer;
}
}
①@Scope注解——指定示例对象的生命周期
——value属性:设置创建的Spring对象的生命周期;
②@PostConstruct注解——指定Spring对象的初始化方法;
③@PreDestroy注解——指定Spring对象的销毁方法。
@Service
@Scope(value="singleton")
public class CustomerService { @PostConstruct
public void init(){
System.out.println("--对象初始化--");
} @PreDestroy
public void destroy(){
System.out.println("--对象销毁--");
}
}
6、Spring纯注解配置
①@Configuration注解
该注解标示当前类是Spring 的配置类,该类用于代替Spring的XML配置文件;
②@ComponentScan注解
该注解用于配置扫描Spring组件类的路径;
——value/basePackages属性:用于指定要扫描的包;
③@PropertySource注解
该注解用于配置需要加载的Properties文件;
——encoding属性:指定编码格式;
——value属性:指定 properties 文件位置;
④@Bean注解
该注解用于将通过方法创建的对象存放在Spring容器中;
——name属性:设置对象名;
⑤@Import注解
该注解用于导入其他配置类;
——value属性:指定配置类。
@Configuration
@ComponentScan(basePackages="cn")
@Import(value=Config.class)
@PropertySource(encoding="UTF-8",value="classpath:sys.properties")
public class Myconfiguration { @Bean
public Date getDate() {
return new Date();
} }
(注:①基于纯注解声明的对象将存放在AnnotationConfigApplicationContext容器中②在需要指定文件的路径时,当文件在类路径下时其路径为“classpath:[文件名]”)
7、获取Spring注解容器的对象
@Test
public void springTest() { //1、创建spring容器
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Myconfiguration.class); //2、获取Customer实例对象
Customer customer= context.getBean("customer", Customer.class); customer.save(); //3、调用对象的方法 context.close(); //4、关闭Spring容器,释放资源
}
Junit是单元测试工具,Spring 框架提供test包对 Junit 单元测试工具进行了整合。
①@RunWith注解
——value属性:指定使用的单元测试执行类;
②@ContextConfiguration注解
——classes属性:指定配置类。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=Myconfiguration.class)
public class SpringTest{ @Autowired
private Customer customer; @Text
public void springTest() {
customer.save();
} }
———————————————————————————————————————————————————————————————————
The end @ 万有引力+
-
-
-
-
-
Spring的基础注解的更多相关文章
- Spring MVC 基础注解之@RequestMapping、@Controller、(二)
我现在学的是spring4.2 今天主要学习了Spring MVC注解 引入注解可以减少我们的代码量,优化我们的代码. @Controller:用于标识是处理器类: @RequestMapping:请 ...
- Spring的基础配置,以及注解
常用依赖 <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webm ...
- Spring MVC常用注解
cp by http://www.cnblogs.com/leskang/p/5445698.html 1.@Controller 在SpringMVC 中,控制器Controller 负责处理由Di ...
- [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.
前言: 在上一篇中: [Spring框架]Spring AOP基础入门总结一. 中 我们已经知道了一个Spring AOP程序是如何开发的, 在这里呢我们将基于AspectJ来进行AOP 的总结和学习 ...
- [Spring框架]Spring AOP基础入门总结一.
前言:前面已经有两篇文章讲了Spring IOC/DI 以及 使用xml和注解两种方法开发的案例, 下面就来梳理一下Spring的另一核心AOP. 一, 什么是AOP 在软件业,AOP为Aspect ...
- spring mvc 基于注解的使用总结
本文转自http://blog.csdn.net/lufeng20/article/details/7598801 概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Sprin ...
- Spring Boot 基础
Spring Boot 基础 Spring Boot 项目(参考1) 提供了一个类似ASP.NET MVC的默认模板一样的标准样板,直接集成了一系列的组件并使用了默认的配置.使用Spring Boot ...
- Spring MVC 基础
Spring MVC 基础 1.Web MVC基础 MVC的本质是表现层模式,我们以视图模型为中心,将视图和控制器分离出来.就如同分层模式一样,我们以业务逻辑为中心,把表现层和数据访问层代码分离出来是 ...
- spring boot基础 入门
spring boot基础 spring boot 的简单搭建 spring boot 的基本用法 spring boot 基本用法 自动配置 技术集成 性能监控 源码解析 工程的构建 创建一个mav ...
随机推荐
- spring-boot mybatis配置
接着我们的spring boot项目,spring boot如何使用mybatis访问数据库呢? 个人习惯使用mapper接口和xml配置sql,从pom.xml入手 1.1 添加依赖 <dep ...
- Python3学习之路~9.1 paramiko模块:实现ssh执行命令以及传输文件
我们一般使用linux的时候,都是在Windows上安装一个ssh客户端连接上去.那么从一台linux如何连接到另一条linux呢?使用ssh命令即可,因为每台linux机器自己都有一个ssh客户端. ...
- 使用springmvc进行文件的上传和下载
文件的上传 SpringMVC支持文件上传组件,commons-fileupload,commons-fileupload依赖commons-io组件 配置步骤说明 第一步:导入包 commons-f ...
- Python结合SAP GUI Script操作sap的简易教程
众所周知,如果要用Python做一些桌面WIN32应用的自动化工作,就需要用到著名的pywin32尤其是其中的win32com.client模块,pywin32的安装不能直接通过pip install ...
- 70个Python练手项目列表(都有完整教程)
前言: 不管学习那门语言都希望能做出实际的东西来,这个实际的东西当然就是项目啦,不用多说大家都知道学编程语言一定要做项目才行. 这里整理了70个Python实战项目列表,都有完整且详细的教程,你可以从 ...
- jenkins centos slave起不来报错The SSH key presented by the remote host does not match the key saved in the Known Hosts file against this host. Connections to this host will be denied until the two keys mat
场景:我的centos-204是一台centos的机器,本来用https://www.cnblogs.com/zndxall/p/8297356.html 的centos slave方式搭建ok的,一 ...
- js超链接锚点定位
<html> <head> <meta charset="UTF-8"> </head> <body> <a on ...
- Idea导包与打包
今天做了一个javavuser协议的性能测试,需要导入jar包,将jar包粘贴到lib下面后不知道怎么加到工程当中, 1,下面分享一下有关导包的流程: 先是在jar 右键,如图 : 选择项目结构,选择 ...
- Git 教程(一):简介和安装
为什么要编写这个教程?因为我在学习Git的过程中,买过书,也在网上Google了一堆Git相关的文章和教程,但令人失望的是,这些教程不是难得令人发指,就是简单得一笔带过,或者,只支离破碎地介绍Git的 ...
- 2018.2.22 学习笔记 random模块及每日一练
给你一个字符数,把它的每个字符之间加上一个空格,形成一个新字符串. 例如:I love FishC. 变成I l o v e F i s h C . 跟小甲鱼学编程 变成 跟 小 甲 鱼 学 ...