统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop、事物,这么做有两个缺点:
1、如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大;如果按需求分开.xml文件,那么.xml文件又会非常多。总之这将导致配置文件的可读性与可维护性变得很低。
2、在开发中在.java文件和.xml文件之间不断切换,是一件麻烦的事,同时这种思维上的不连贯也会降低开发的效率。
为了解决这两个问题,Spring引入了注解,通过"@XXX"的方式,让注解与Java Bean紧密结合,既大大减少了配置文件的体积,又增加了Java Bean的可读性与内聚性。

1.@Configuration&@Bean给容器中注册组件

@Configuration可理解为用spring的时候xml里面的<beans>标签

@Bean可理解为用spring的时候xml里面的<bean>标签

xml版:

<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 http://www.springframework.org/schema/context/spring-context.xsd"><!--原始方式-->
<bean id="person" class="com.wang.bean.Person">    <property name="name" value="张三"></property>    <property name="age" value="18"></property></bean>
</beans>

注解版:

//在配置类里配置
@Configuration//告诉spring这是一个配置类
public class PersonConfig {@Bean(value = "person") //给spring容器注册一个bean,类型为返回值类型,id是默认是方法名为id,也可以使用value指定public Person person(){return new Person("lisi",20);}
}

2.@ComponentScan-自动扫描组件&指定扫描规则

该注解会自动扫描包路径下面的所有@Controller、@Service、@Repository、@Component 的类

xml版:

 <!--包扫描,只要注解了@Component,@Controller等会被扫描--><context:component-scan base-package="com.wang" use-default-filters="false" ><!--排除某个注解,除了Controller其他类都会被扫描--><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/><!--只包含某个注解--><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan>

注解版:

@ComponentScan(value = "com.wang"/*excludeFilters = {  //排除@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class,Service.class}) //排除Controller和Service
},*/,includeFilters = { //只包含@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Repository.class})       /*,@ComponentScan.Filter(type = FilterType.CUSTOM,classes = {MyTypeFilter.class})*/ //自定义注解 
},useDefaultFilters = false) //这里要加useDefaultFilters=false让默认的过滤器失效

其中Filter的type的类型有:

1. FilterType.ANNOTATION 按照注解

2. FilterType.ASSIGNABLE_TYPE 按照类型  FilterType.REGEX 按照正则

3.  FilterType.ASPECTJ 按照ASPECJ表达式规则

4. FilterType.CUSTOM 使用自定义规则

其中自定义规则类型过滤器需要实现TypeFilter接口:

package com.wang.config;import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;import java.io.IOException;
//自定义类型过滤器
public class MyTypeFilter implements TypeFilter {//metadataReader读取到当前扫描到的类的信息//metadataReaderFactory可以获取其他任何类的信息
    @Overridepublic boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata(); //获取当前扫描类的信息ClassMetadata classMetadata = metadataReader.getClassMetadata();//获取当前扫描类的类信息Resource resource = metadataReader.getResource();//获取当前扫描类的资源信息
String className = classMetadata.getClassName();System.out.println("className--->"+className);if (className.contains("Dao")){return true;}else {return false;}}
}

3.@Scop 设置组件作用域

xml版:

 <bean id="person" class="com.wang.bean.Person" scope="prototype"><property name="name" value="张三"></property><property name="age" value="18"></property></bean>

注解版:

@Bean/** * prototype 多例,ioc容器创建完成后,要获取的时候才会被调用,多次获取会多次调用 * singleton 单例(默认),ioc容器启动会调用方法创建对象放到ioc中,以后直接从容器中获取 * request 同一次请求创建一个实例 * session 同一个session创建一个实例 * */
@Scope("prototype")public Person person(){    System.out.println("添加Person....");    return new Person("张三",23);}

 

4.@Lazy 懒加载bean

xml版本:

  <bean id="person" class="com.wang.bean.Person" scope="prototype" lazy-init="true"><property name="name" value="张三"></property><property name="age" value="18"></property></bean>

注解版:

 /**** 单实例bean,默认在容器启动的时候创建对象* 懒加载:容器启动不创建对象,第一次使用(获取)创建Bean,并初始化,第二次使用就使用第一次创建的对象**/@Lazy

5.@Conditional按照条件注册bean

 @Conditional是Spring4新提供的注解,它的作用是按照一定的条件进行判断,满足条件给容器注册bean。下面给个例子,判断当前项目的运行环境,如果是Windows系统,注册id为win的bean,若是Linux系统,注册id为lin的bean

1).实现Condition接口
public class WindowsCondition implements Condition {/**** @param conditionContext 判断条件能使用的上下文环境* @param annotatedTypeMetadata 当前标注了condition的注释信息* @return*/@Overridepublic boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {//1.获取ioc的BeanFactoryConfigurableListableBeanFactory beanFactory = conditionContext.getBeanFactory();//2.获取类加载器ClassLoader classLoader = conditionContext.getClassLoader();//3.获取当前环境信息Environment environment = conditionContext.getEnvironment();//4.获取所有bean定义的注册类BeanDefinitionRegistry registry = conditionContext.getRegistry();String property = environment.getProperty("os.name");if (property.contains("Windows")){return true;}else {return false;}}
}

package com.wang.condition;import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;public class LinuxCondition implements Condition {@Overridepublic boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {//3.获取当前环境信息Environment environment = conditionContext.getEnvironment();String property = environment.getProperty("os.name");if (property.contains("Linux")){return true;}else {return false;}}
}

2).在Bean上配置注解
@Configuration
public class ConditionConfig {/*** @Conditional({})按照一定条件进行判断,满足条件容器中注册bean,* 若放在类中,整个配置类中的bean满足条件才会被加载到容器中** 若是windows系统,注册win,若是linux注册lin*/@Bean("win")@Conditional(WindowsCondition.class)public Person person(){return new Person("win",22);}@Bean("lin")@Conditional(LinuxCondition.class)public Person person2(){return new Person("lin",11);}@Bean("person")public Person person3(){return new Person("person",25);}
}

转载于:https://www.cnblogs.com/wangxiayun/p/10083217.html

Spring注解开发系列Ⅰ--- 组件注册(上)相关推荐

  1. Spring注解驱动:组件注册(一)

    文章目录 1.Spring简单环境 2.Spring注解容器 2.1 代码示例 2.2 给容器注册组件4种方式 2.2.1 工厂创建对象 2.3 声明周期 2.3.1 Bean指定初始化和销毁方法 2 ...

  2. spring注解开发:容器中注册组件方式

    1.包扫描+组件标注注解 使用到的注解如下,主要针对自己写的类 @Controller @Service @Repository @Component @ComponentScan 参考 spring ...

  3. Spring之AOP系列--将方法上的注解做为切点(用@Around)

    原文网址:Spring之AOP系列--将方法上的注解做为切点(用@Around)_IT利刃出鞘的博客-CSDN博客 简介 说明         本文介绍Spring(SpringBoot)的AOP的用 ...

  4. 关于Spring注解开发教程,打包全送你

    摘要:spring是我们web开发中必不可少的一个框架,基于传统的xml方式配置bean总觉得太过繁琐,从spring2.5之后注解的出现可以大大简化我们的配置. 本文分享自华为云社区<如何高效 ...

  5. spring注解开发配置spring父子容器

    spring注解开发配置spring父子容器 官网 https://docs.spring.io/spring-framework/docs/current/spring-framework-refe ...

  6. Spring注解开发-Bean注册

    1.使用注解开发我们需要在applicationContext.xml文件中添加context标签. 在配置文件中开启注解扫描. <?xml version="1.0" en ...

  7. 第二章 ---- spring注解开发

    文章目录 参考视频 注解驱动的意义 常用注解(重点) 启动注解驱动 IoC bean定义(@Component .@Controller.@Service. @Repository) @Scope b ...

  8. ❤️使用Spring注解开发(建议收藏)

    ❤️使用注解开发 在Spring4之后,要使用注解开发,必须保证aop的包导入了 使用注解开发需要导入context约束,增加注解的支持! <?xml version="1.0&quo ...

  9. Spring注解开发学习笔记

    1 IOC 1.1 工厂模式 使用工厂中方法代替new形式创建对象的一种设计模式 1.2 Inversion of Control控制翻转 一种思想,用于消减代码间的耦合. 实现思想:利用工厂设计模式 ...

最新文章

  1. 身限辞退风波,Google AI 掌门人 Jeff Dean 不误折桂 IEEE 冯诺依曼奖
  2. Spring Cloud Gateway中异常处理
  3. Leaflet中通过setZIndex实现图层层级控制
  4. 无符号定点数加法运算的VHDL描述
  5. 用quot;hosting.jsonquot;配置ASP.NET Core站点的Hosting环境
  6. 倍增:喷泉 深度解析(洛谷P7167)
  7. Windows Server 2008 R2 域控DOS命令
  8. (20)System Verilog利用clocking块产生输出信号延迟激励
  9. QQ消息支持HTML吗,WebQQ全面升级支持IE9 HTML5无处不在
  10. linux 进程间广播,Linux系统编程之进程间通信之浅谈信号
  11. php环境搭建(php5.5.8+apache2.4)
  12. 百科不全书之Python进阶
  13. 华三路由器虚拟服务器设置,H3C vLNS系列虚拟L2TP网络服务器 配置指导-E0324-5W100...
  14. 新事业,新征程:换屏哥,您身边的手机维修专家
  15. 基于SpringBoot的外卖点餐管理系统
  16. Lua程序设计读书 随笔
  17. 系统时间不够精确?试试RTC(实时时钟)
  18. Centos7配置阿里云DNS
  19. 周记九--不忘记本心是黑暗中不会褪色的路引
  20. 【JS】820- JS 常见报错及异常捕获

热门文章

  1. Video Of You! 勒索诈骗邮件
  2. 6、七段数码管显示译码器设计与应用
  3. (八):docker swarm简单使用
  4. 问题 Z: Corral the Cows(二分,分治)
  5. UltraEdit-32 v13.10+4 官方中文版
  6. PAT甲组1151 LCA in a Binary Tree思路解析和代码
  7. 如何解决“access violation at address”错误
  8. YOLOv5官方教程
  9. 最新修复版“WBO中庚杯拳王争霸赛搞笑报名表源码”
  10. 【专家访谈】测试专家 陈林钧 访谈问题收集中