spring的注解开发

启动注解功能

启动注解功能
⚫ 启动注解扫描,加载类中配置的注解项

⚫ 说明:
◆ 在进行包所扫描时,会对配置的包及其子包中所有文件进行扫描
◆ 扫描过程是以文件夹递归迭代的形式进行的
◆ 扫描过程仅读取合法的java文件
◆ 扫描时仅读取spring可识别的注解
◆ 扫描结束后会将可识别的有效注解转化为spring对应的资源加入IoC容器
⚫ 注意:
◆ 无论是注解格式还是XML配置格式,最终都是将资源加载到IoC容器中,差别仅仅是数据读取方式不同
◆ 从加载效率上来说注解优于XML配置文件

applicationContext.xml

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd">
<!--注解只是一个标记,如果spring不去扫描解析的话,这个标记就不会生效-->
<!--    启动spring扫描注解的包,只扫描spring的注解--><context:component-scan base-package="com.fs"/></beans>

将类交给springIOC管理,bean的定义@Component @Controller @Service @Repository

代码解释

package com.fs.demo;
/*
spring注解将类交给spring的ioc管理*/import org.springframework.stereotype.Component;/*
@Component这个注解就相当于核心配置文件中的<bean id="类名首字母小写" class="权限定类名"/>@Component的衍生注解@Controller @Service @Repository,功能和 @Component一样只是spring对应三层结构来定的注解,功能一样,只是含义不同而已,所以使用其中那一个都可以将类交给ioc管理*/
@Component
public class AnnotationDemo {public AnnotationDemo() {System.out.println("AnnotationDemo被spring的IOC管理了~~~");}public void testMethod(){System.out.println("测试方法执行~~~");}
}

测试代码

    @Testpublic void testOne(){//创建ioc容器ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");//通过类名首字母小写从ioc中获取对象AnnotationDemo annotationDemo = (AnnotationDemo) applicationContext.getBean("annotationDemo");//使用类.class从ioc中获取对象AnnotationDemo bean = applicationContext.getBean(AnnotationDemo.class);//调用方法annotationDemo.testMethod();bean.testMethod();}

bean的作用域(@Scope(“单例或者双列”))(了解)

bean的生命周期

@Bean注解的使用

代码演示

package com.fs.demo;import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*
@Configuration放在类上,说明这个类是一个spring的配置类首先这个类要标志是被spring扫描的,才会去扫描这个类下的其他注解@Bean*/
@Configuration
public class DataSourceDemo {/*在方法上@Bean将方法的返回值交给ioc管理配置DruidDataSource链接池对象交给ioc管理*/@Bean("druidDataSource")//给一个idpublic DruidDataSource getDruidDataSource(){DruidDataSource druidDataSource = new DruidDataSource();druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");druidDataSource.setUrl("jdbc:mysql://192.168.93.132:3306/test");druidDataSource.setUsername("root");druidDataSource.setPassword("root");return druidDataSource;}
}

测试代码

    /*测试@Bean注入的druid连接池*/@Testpublic void testDataSource(){//创建ioc容器ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");//通过类名首字母小写从ioc中获取对象DruidDataSource druidDataSource = (DruidDataSource) applicationContext.getBean("druidDataSource");
//        DataSource dataSource = (DataSource) applicationContext.getBean(DataSource.class);DruidDataSource dataSource = applicationContext.getBean(DruidDataSource.class);
//        System.out.println(dataSource);System.out.println(druidDataSource);}

bean的属性依耐注入DI

代码演示@Value

package com.fs.pojo;import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
/*
@Value非引用类型的属性注入*/
@Component("student")//注入的时候起了个id名
//@Primary//没有起id的时候,这个注解是让这个bean优先加载
public class Student {/*@ValueDI依耐注入,给基本类型属性注入值*/@Value("18")private int age;@Value("小付")private String name;@Overridepublic String toString() {return "Student{" +"age=" + age +", name='" + name + '\'' +'}';}
}

代码演示@Autowired、@Qualifier

package com.fs.pojo;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;/*
依耐注入之引用类型的注入@Autowired注意:注入的bean一定要在ioc容器中,才能注入因为这个注解是在ioc容器中去找对应的bean注入*/
@Component
public class Person{@Value("男")private String gender;/*@Autowired  DI依耐注入按照类型自动注入(id类名首字母小写)*/
//    @Autowired
//    private Student student;/*@Autowired@Qualifier("student")//若有相同类型的,按照id注入*/@Autowired@Qualifier("student")private Student student;@Overridepublic String toString() {return "Person{" +"gender='" + gender + '\'' +", student=" + student +'}';}
}

测试代码

    /*测试依耐注入属性@Autowired@Qualifier("id")*/@Testpublic void testDI(){//创建ioc容器ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");//通过类名首字母小写从ioc中获取对象Person person = (Person) applicationContext.getBean("person");System.out.println(person);/*输出结果:Person{gender='男', student=Student{age=18, name='小付'}}*/}

了解注解


加载类路径下的properties文件@PropertySource

准备properties文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.93.132:3306/test
jdbc.username=root
jdbc.password=root

代码演示

package com.fs.properties;import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;/*
@PropertySource
引入类路径下的的properties文件属性:value = "classpath:resource中的properties文件"*/
@Component//将这个类交给ioc管理
@PropertySource(value = "classpath:jdbc.properties")
public class PropertiesDemo {//@Value("${Key}")@Value("${jdbc.driver}")private String driver;@Value("${jdbc.url}")private String url;@Value("${jdbc.username}")private String username;@Value("${jdbc.password}")private String password;@Overridepublic String toString() {return "PropertiesDemo{" +"driver='" + driver + '\'' +", url='" + url + '\'' +", username='" + username + '\'' +", password='" + password + '\'' +'}';}
}

测试代码

    /*测试@PropertySource(value = "classpath:jdbc.properties")测试引入类路径下的properties文件*/@Testpublic void testProperties(){//创建ioc容器ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");//通过类名首字母小写从ioc中获取对象//PropertiesDemo propertiesDemo = (PropertiesDemo) applicationContext.getBean("propertiesDemo");//通过类名.classPropertiesDemo propertiesDemo = applicationContext.getBean(PropertiesDemo.class);System.out.println(propertiesDemo);//控制台输出结果:
//PropertiesDemo{driver='com.mysql.jdbc.Driver', url='jdbc:mysql://192.168.93.132:3306/test', username='root', password='root'}}

测底去除配置文件@Configuration、@ComponentScan @Import

我们现在还有applicationContext.xml这个文件,我们可以通过注解来指定一个配置类,从而让配置文件不见

@Import(类.class)配置类上写入后,就会把你给的类存放在IOC容器中,存放的id为类的全限定类名(包名加加类名)

代码演示

package com.fs.config;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;/*
@Configuration这个注解就表示这是一个配置类
@ComponentScan("com.fs")这个注解表示spring扫描那些包下的注解
@Import({DataSourceConfig.class})写在配置类,导入其他的配置类类.class就可以将这个类注入到spring的ioc容器中什么类都可以通过@Import({类.class,类2.class})来注入到spring的ioc容器中*/
@Configuration
@ComponentScan("com.fs")
@Import({DataSourceConfig.class})
public class SpringConfig {}

测试代码

    //使用spring配置类来创建ioc@Testpublic void testSpringConfig(){//创建ioc容器 AnnotationConfigApplicationContext(配置类.class)ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);//通过类名首字母小写从ioc中获取对象//PropertiesDemo propertiesDemo = (PropertiesDemo) applicationContext.getBean("propertiesDemo");//通过类名.classPropertiesDemo propertiesDemo = applicationContext.getBean(PropertiesDemo.class);System.out.println(propertiesDemo);}/*测试@Import 导入的druid连接池*/@Testpublic void testImport(){//创建ioc容器ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");//通过类名首字母小写从ioc中获取对象DruidDataSource druidDataSource = (DruidDataSource) applicationContext.getBean("druidDataSource");
//        DataSource dataSource = (DataSource) applicationContext.getBean(DataSource.class);DruidDataSource dataSource = applicationContext.getBean(DruidDataSource.class);
//        System.out.println(dataSource);System.out.println(druidDataSource);}

依耐加载




依赖加载应用场景
⚫ @DependsOn
◆微信订阅号,发布消息和订阅消息的bean的加载顺序控制
◆双11活动期间,零点前是结算策略A,零点后是结算策略B,策略B操作的数据为促销数据。策略B
加载顺序与促销数据的加载顺序
⚫ @Lazy
◆程序灾难出现后对应的应急预案处理是启动容器时加载时机
⚫ @Order
◆多个种类的配置出现后,优先加载系统级的,然后加载业务级的,避免细粒度的加载控制

spring的注解开发@Component @Bean @Value @Autowired、@Qualifier @PropertySource @Configuration相关推荐

  1. spring原始注解开发-01

    我们使用xml-Bean标签的配置方式和注解做对比理解 1.创建UserDao接口以及UserDao的实现类UserDaoImpl(接口代码省略) public class UserDaoImpl i ...

  2. Spring IOC注解开发

    Spring IOC注解开发 @(Spring)[Spring, ioc, 注解] Spring IOC注解开发 Spring的IOC的注解使用步骤 创建项目引入jar包 引入配置文件 创建相关包和类 ...

  3. 【Java从0到架构师】Spring - 纯注解开发

    纯注解开发 纯注解开发 - AOP 纯注解开发 - 整合 MyBatis 纯注解开发 - 事务管理 JSR 注解 JSR 注解 - @Resource.@Inject JSR 注解 - @PostCo ...

  4. Spring之注解开发

    8.使用注解开发 8.1.什么是注解? 注解是代码特殊标记,格式:@注解名称(属性名称=属性值,属性名称=属性值) 使用注解,注解作用在类上面,方法上面,属性上面 使用注解目的:简化xml文件的配置. ...

  5. Spring原始注解开发-02

    使用@Repository.@Service.@Controller注解配置,使其更加清晰属于哪一层,因为我是模拟的web层,所有没有使用@Controller注解,后面结合web开发会使用到 1.创 ...

  6. Spring 使用注解@DependsOn控制Bean加载顺序

    文章目录 1. 前言 2. 代码实现 1. 前言 默认情况下,Spring加载Bean的顺序是不确定的(或者可以理解为,按编译后的class文件顺序加载).当我们需要控制Bean加载顺序以满足特定的需 ...

  7. Spring 基于注解(annotation)的配置之@Autowired注解

    Setter 方法中的 @Autowired 当 Spring遇到一个在 setter 方法中使用的 @Autowired 注解,它会试图执行 byType 自动连接.换言之,加了@Autowired ...

  8. 五篇教你掌握spring之三:详解Spring的bean以及注解开发

    详解Spring的bean以及注解开发 各种复杂类型的依赖注入 我们采用一个类的大杂烩的形式,新建一个Student package com.lwh.pojo;import java.util.*;p ...

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

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

最新文章

  1. 李洪强iOS开发之RunLoop的原理和核心机制
  2. php怎么输出3个函数和,PHP利用var_dump,var_export,print_r三个函数的区别示例
  3. 概率论-4.2中心极限定理(待补充)
  4. Spring-Cloud中的统一配置中心
  5. WCF Basic(1)-操作重载
  6. alsa的动态库安装在哪里_Linux链接库一(动态库,静态库,库放在什么路径下)...
  7. python读取文件路径乱码 linux_Python之pandas读写文件乱码的解决方法
  8. AdminLTE框架的基本使用
  9. 一博商业进销存管理系统 v2008 怎么用
  10. 计算机会计处理流程,会计电算化账务系统期末处理流程
  11. 【微信小程序】入门第一篇 注册账号
  12. 即时通讯,2022即时通讯IM厂商介绍
  13. windows 7 iso镜像刻录到U盘后选择安装的版本
  14. 珍惜现在才能活的快乐
  15. 项目、习惯以及Todolist的区别
  16. Python+Vue计算机毕业设计餐饮管理系统qpa33(源码+程序+LW+部署)
  17. 数据库迁移 软件Spoon Kettle环境搭配(本人踩过的坑)
  18. 如何在 LaTeX 中画一个树状图(使用tikz和tikz-qtree包中的宏绘制树、森林、二叉树)
  19. 微信小程序checkbox的全选以及所有checkbox选中之后的全选
  20. 爱心捐赠爱传情 暖心行动暖寒冬

热门文章

  1. 分离数据库(Detach database).
  2. SQL语句修改主键列
  3. Spring学习(三)Spring AOP 简介
  4. 找出1-100中缺失的两个数
  5. .NET开发者必备的工具箱
  6. mac 环境变量设置
  7. hdu3870——平面图最小割
  8. Windows via C/C++ 学习(15)线程调度、线程优先级和亲缘性
  9. (第五篇)Linux操作系统基本结构介绍
  10. MySQL完全备份与恢复