详解spring如何使用注解开发

Spring4之后,要使用注解开发,必须要保证aop的包导入了。

使用注解需要导入context约束,增加注解的支持。

<?xml version="1.0" encoding="utf-8"?>

<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
       https://www.springframework.org/schema/context/spring-context.xsd">

    <!--指定要扫描的包,这个包下的注解会生效-->
    <context:component-scan base-package="com.chen.project"/>
    <context:annotation-config/>
</beans>

1.bean

@Component:组件,放 在类上,说明这个类被Spring管理了,就是bean

2.属性如何注入

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

//等价于<bean id="user" class="com.chen.dao.User"></bean>
@Component
public class User {

   public String name;

   //等价于<property name="name" value="lan"></property>
   @Value("LAN")
   public void setName(String name) {
       this.name = name;
   }
}

3.衍生的注解

@Component有几个衍生注解,我们在web开发中,会按照mvc三层架构分层!

  • dao【@Repository
  • service 【@Service
  • controller【@Controller

这四个注解功能都是一样的,都代表将某个类注册到Spring中,装配Bean

4.自动装配置

-@Autowired通过byType的方式实现。

@Resource默认通过byName的方式实现。

5.作用域

@Scope("singleton")
public class User {
}

6.小结

  • xml更加万能,适用于任何场合,维护简单方便
  • 注解不是自己的类使用不了,维护相对复杂!
  • xml于注解最佳实践
  • xml用来管理bean
  • 注解只负责完成属性的注入
  • 我们在使用的过程中,只需要注意一个问题,必须让注解生效,就需要开启注解的支持
<!--指定要扫描的包,这个包下的注解会生效-->
   <context:component-scan base-package="com.chen.project"/>
   <context:annotation-config/>

到此这篇关于详解spring如何使用注解开发的文章就介绍到这了,更多相关spring使用注解开发内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Spring中的注解之@Override和@Autowired

    一.Override 首先,@Override 注解是伪代码,表示子类重写父类的方法.这个注解不写也是可以的,但是写了有如下好处: 1. 可以当注释用,方便阅读(注解很重要的一个作用就是注释): 2. 编译器和 IDE 可以验证 @Override 下面的方法名是否是父类中所有的,如果没有的话就会报错.如果没有加 @Override ,而子类中的方法名又写错了,这个时候编译器是可以编译通过的,因为编译器以为这个方法是你的子类中自己增加的方法. 下面来验证一下,首先有一个 IPay 的父类,包含一

  • spring中AOP 注解开发示例详解

    一.简介 AOP主要包含了通知.切点和连接点等术语,介绍如下: 通知(advice) 通知定义了切面是什么以及何时调用,何时调用包含以下几种 Before 在方法被调用之前调用通知 After 在方法完成之后调用通知,无论方法执行是否成功 After-returning 在方法成功执行之后调用通知 After-throwing 在方法抛出异常后调用通知 Around 通知包裹了被通知的方法,在被通知的方法调用之前和调用之后执行自定义的行为 切点(PointCut) 通知定义了切面的什么和何时,切

  • 使用注解开发SpringMVC详细配置教程

    1.使用注解开发SpringMVC 1.新建一个普通的maven项目,添加web支持 2.在pom.xml中导入相关依赖 SpringMVC相关 <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.8.RELEASE</version> </dependency&

  • SpringMVC注解之@ResponseBody注解原理

    一.介绍 @ResponseBody 注解的作用是将方法的返回值通过适当的转换器转换为指定的格式之后,写入到 response 对象的 body 区,通常用来返回 JSON.XML 数据. 使用了 @ResponseBody 注解标记的方法不再做视图解析 二.作用范围 标记在方法上 标记在类上 通过 @RestController 注解实现,此时所有的方法都将会被添加 @ResponseBody 注解 三.源码分析 具体为何调用了以下方法可以看我的另一篇文章.SpringMVC 执行流程解析 S

  • 详解Spring注解驱动开发之属性赋值

    一.@Value注解 在Person的属性上使用@Value注解指定注入值 public class Person { @Value("#{20-2}") //SpEL表达式 #{} private Integer id; @Value("张三") //基本数据类型 private String name; } 配置类 @Configuration public class MainConfigOfPropertyValues { @Bean public Pers

  • Spring注解开发生命周期原理解析

    生命周期 initMethod和destroyMethod Bean定义 public class Car { public Car() { System.out.println("car constructor"); } public void init(){ System.out.println("car init"); } public void destroy(){ System.out.println("car destroy"); }

  • SpringBoot整合Mybatis注解开发的实现代码

    官方文档: https://mybatis.org/mybatis-3/zh/getting-started.html SpringBoot整合Mybatis 引入maven依赖 (IDEA建项目的时候直接选就可以了) <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <ve

  • spring @Validated 注解开发中使用group分组校验的实现

    之前知道spring支持JSR校验,在自己定义的bean中加入@NotNull,@NotBlank,@Length等之类的校验用于处理前台传递过来的request请求,避免在写多余的代码去处理. 但是随着业务的复杂度增加,对于校验的制定也越来越有要求,这个时候就需要引入分组group的概念,在自定义注解@Validated中 定义了一个Class[]数组用来分组.这样我们就可以引入分组校验的概念,首先根据需要的分组新建自己的接口. 然后在需要校验的bean上加入分组: 最后根据需要,在Contr

  • Spring注解开发@Bean和@ComponentScan使用案例

    组件注册 用@Bean来注册 搭建好maven web工程 pom加入spring-context,spring-core等核心依赖 创建实例类com.hjj.bean.Person, 生成getter,setter方法 public class Person { private String name; private int age; } 创建com.hjj.config.MainConfig @Configuration //告诉spring是一个配置类 public class Main

  • 解决springboot利用ConfigurationProperties注解配置数据源无法读取配置信息问题

    @ConfigurationProperties是springboot新加入的注解,主要用于配置文件中的指定键值对映射到一个java实体类上.那么它是怎么发挥作用的呢?下面我们将揭开@ConfigurationProperties的魔法. ConfigurationPropertiesBindingPostProcessor这个bean后置处理器,就是来处理bean属性的绑定的,这个bean后置处理器后文将称之为properties后置处理器.你需要知道以下几件事: ioc容器context的e

随机推荐