Spring实现注解


注解的优势与劣势

  • 优势:(Annotation):简化了xml配置操作,不用写过多的配置标签
  • 劣势:过于复杂的配置还是必须要使用配置文件,就算Springboot,一些复杂配置都要写配置文件(yaml)
  • 个人感觉:

注解是真的爽,学过Springboot都知道,Springboot几乎纯注解开发,而且各自框架整合成了启动器,没有ssm各种恶心的环境兼容问题了,不过作为一个懒人脚手架,开发很爽,但是学习还是得看Spring

1.注解使用前提

项目中必须要有aop的依赖包,aop包封装了注解方法,不导入aop包注解功能会报错

2.引入context命名空间

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

3.开启组件扫描

<context:component-scan base-package="com.chime.service"/>

加载配置文件时,扫描指定包内的所有组件(组件注解)
base-package 可以通过逗号隔开扫描多个包

扫描细节


	<context:component-scan base-package="com.chime"/>
    
    <context:component-scan base-package="com.chime" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <context:component-scan base-package="com.chime" >
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
 1. 默认扫描:扫描指定包中所有内容
 2. use-default-filters="false":不开启默认过滤器
	include-filter:包含过滤器
 	type="annotation" :过滤注解类型
 	expression="org.springframework.stereotype.Controller"/>:所有Controller类注解
 	指定扫描的类型,在指定包中,只扫描这个类型(样例是只扫描com.chime包中的Controller注解)
 3. exclude-filter:排斥过滤器
 	指定不扫描的类型,在指定包中,其他都扫描,除了指定类型不扫描

4.声明组件(组件注解)

  • @Component
  • @Repository
  • @Service
  • @Controller
    @Repository、@Service、 @Controller就是一个Component,(IDEA按住CRTL点击注解就能阅读源码),取不同的名字只是为了区分不同层
//@Component(value="userService") //<bean id="userService" class=currentClass></bean>
@Service(value="userService") //<bean id="userService" class=currentClass></bean>
public class UserService {
}
@Service(value="userService") 相当于实现了<bean id="userService" class=currentClass></bean>
currentClass :当前类

@Compone():value可以不写,默认是当前类名的小写,最好是写一下,否则全交给系统小写最后不好区分

5.通过注解注入属性(自动装配)

  1. @Autowired:自动装配,根据byType实现,在属性上写上该注解就可以注入属性(前提是已有该bean)
  2. @Qualifier:需要结合@Autowired一起使用,基于byName实现,在括号中可以写入name,根据name匹配
  3. @Resource:可以单独使用,既可以byType又可以byName
  4. @Value(val):给普通属性注入值

@Resource:自动根据属性类型进行匹配
@Resource(name=“userMapperNew”):根据名称进行匹配
不过@Resource不是Spring的原生扫描,而是javax拓展包里的,所以不建议使用``

  	@Autowired
    @Qualifier(name)
    
    @Resource
    
    @Resource(name="userMapperNew")

6.配置类替代配置文件

@Configuration //声明是一个配置类
@ComponentScan(value = {"com.chime"})
public class WebConfig {
    
}

肯定是不能替代复杂的配置的

7.@Nullable

该注解可以避免某些空值造成的报错

可以声明该方法返回可以为空

@Nullable
public int methodName()

参数可以为空

public void methodName(@Nullable String name)

属性可以为空

class ClassName{
@Nullable
String name;
}

8、注册相关注解

annotation-driven用于注册注解类。

一般根据前缀来注册相关的注解类
<tx:annotation-driven/>:支持事务注解的(@Transactional)
<mvc:annotation-driven/>:支持MVC注解
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值