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 ...
- [LeetCode] 97. Interleaving String_ Hard tag: Dynamic Programming
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = ...
- cumsum累计函数系列:pd.cumsum()、pd.cumprod()、pd.cummax()、pd.cummin()
cum系列函数是作为DataFrame或Series对象的方法出现的,因此命令格式为D.cumsum() 举例: D=pd.Series(range(0,5)) 1. cumsum 2. cumpro ...
- eclipse中tomcat的add and Remove找不到项目
在我们运行项目前,都需要将项目部署到tomcat上,但是有时我们会遇到这种情况:项目明明存在,但是eclipse中tomcat的add and remove找不到项目,无法部署,那么这个问题该如何解决 ...
- CDI services--Scope(生命周期)&&EL.(Sp El)
一.EL/SpEL 1.EL语言(CDI与表达式语言(EL)集成,允许在JavaServer Faces页面或JavaServer Pages页面中直接使用任何组件) 1)概述:EL是JSP内置的表达 ...
- Vue相关开源项目库汇总
https://github.com/opendigg/awesome-github-vue http://www.opendigg.com/tags/front-vue README.md 内容 U ...
- Python练习:爬取图片
贴吧地址 https://tieba.baidu.com/p/5272413637?red_tag=0606091703 程序如下import urllib.requestimport re def ...
- Redis哨兵模式(sentinel)部署记录(主从复制、读写分离、主从切换)
部署环境: CentOS7.5 192.168.94.11 (master) 192.168.94.22 (slave0) 192.168.94.33 (slave1) 192.168.94.44 ...
- 7个优秀的国内外移动端web框架(转)
淘宝SUI Mobile框架 (light7框架 官网:http://www.light7.cn/)官网地址:http://m.sui.taobao.org/ SUI Mobile 是一套基于 F ...
- MIUI系统如何获取ROOT权限
MIUI系统有么好方法启用了Root超级权限?各位都清楚,Android手机有Root超级权限,一旦手机启用了root相关权限,就能够实现更多的功能,举例子,各位公司的营销部门的同事,使用大多数营销工 ...