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"/>
 (注:使用该标签需要在Spring 配置文件中导入context名称空间下的约束)

  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;
} }
 
 
 
  5、Spring对象的生命周期注解

  ①@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容器,释放资源

}
  8、Spring整合Junit

  

  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的基础注解的更多相关文章

  1. Spring MVC 基础注解之@RequestMapping、@Controller、(二)

    我现在学的是spring4.2 今天主要学习了Spring MVC注解 引入注解可以减少我们的代码量,优化我们的代码. @Controller:用于标识是处理器类: @RequestMapping:请 ...

  2. Spring的基础配置,以及注解

    常用依赖 <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webm ...

  3. Spring MVC常用注解

    cp by http://www.cnblogs.com/leskang/p/5445698.html 1.@Controller 在SpringMVC 中,控制器Controller 负责处理由Di ...

  4. [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.

    前言: 在上一篇中: [Spring框架]Spring AOP基础入门总结一. 中 我们已经知道了一个Spring AOP程序是如何开发的, 在这里呢我们将基于AspectJ来进行AOP 的总结和学习 ...

  5. [Spring框架]Spring AOP基础入门总结一.

    前言:前面已经有两篇文章讲了Spring IOC/DI 以及 使用xml和注解两种方法开发的案例, 下面就来梳理一下Spring的另一核心AOP. 一, 什么是AOP 在软件业,AOP为Aspect ...

  6. spring mvc 基于注解的使用总结

    本文转自http://blog.csdn.net/lufeng20/article/details/7598801 概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Sprin ...

  7. Spring Boot 基础

    Spring Boot 基础 Spring Boot 项目(参考1) 提供了一个类似ASP.NET MVC的默认模板一样的标准样板,直接集成了一系列的组件并使用了默认的配置.使用Spring Boot ...

  8. Spring MVC 基础

    Spring MVC 基础 1.Web MVC基础 MVC的本质是表现层模式,我们以视图模型为中心,将视图和控制器分离出来.就如同分层模式一样,我们以业务逻辑为中心,把表现层和数据访问层代码分离出来是 ...

  9. spring boot基础 入门

    spring boot基础 spring boot 的简单搭建 spring boot 的基本用法 spring boot 基本用法 自动配置 技术集成 性能监控 源码解析 工程的构建 创建一个mav ...

随机推荐

  1. JavaWeb学习之三层架构实例(二)

    引言 这个实例是上一个实例JavaWeb学习 三层架构实例(一)的加强版,实现的是在前端对数据库中student表的 增.删.改.查 操作.关于三层组成云云,这里就不再叙述. 实例 效果图 先来看一下 ...

  2. [LeetCode] 70. Climbing Stairs_ Easy tag: Dynamic Programming

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  3. C++ 自定义订单号

    自定义订单号 #include<iostream> #include<stack> #include <time.h> #include <sys/timeb ...

  4. java学习之成员内部类

    //成员内部类:直接在类中定义 /*成员内部类的通常用法: * 通常是提供给外部类使用不进行内部类的实例化 * 因此一般把他设为私有的类用private限定 * * */ /*Demo*/ class ...

  5. web攻击之xss(一)

    1,xss简介 跨站脚本攻击(Cross Site Scripting),为了不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS.恶意攻击 ...

  6. OpenStack-Neutron-VPNaaS-代码

    目前juno只支持ipsec的vpn  但是其实稍微修改代码pptp/openvpn/gre也都是可以支持的,下面看看vpn服务的代码流程: 默认我们创建好了ide策略.ipsec策略和vpn服务,因 ...

  7. vue知识总结

    vue: 渐进式JavaScript 框架 Vue项目构建 npm install -g vue vue init webpack-simple my-project cd my-project np ...

  8. wm_concat函数的排序问题

    wm_concat在行转列的时候非常有用,但在行转列的过程中的排序问题常常难以控制. 可见下面例子: 准备测试表: drop table t; create table t (n number,m n ...

  9. 【Codeforces Round】 #431 (Div. 2) 题解

    Codeforces Round #431 (Div. 2)  A. Odds and Ends time limit per test 1 second memory limit per test ...

  10. C博客作业03--函数

    1. 本章学习总结 1.1 思维导图 1.2 本章学习体会及代码量学习体会 1.2.1 学习体会 这几周学习了函数,题目还是原样只是多了种做题的方法.一开始看书感觉声明,定义啊,还有全局变量那些,文绉 ...