1:什么是注解?

(1):注解是代码中特殊标记,格式:@注解名称(属性名称=属性值,属性名称=属性值…..)
(2):使用注解,注解作用在类上面,方法上面,属性上面
(3):使用注解目的:简化xml配置,使代码更加简化优雅

2:spring针对bean管理(创建对象)提供相关注解

spring注解大全 / 注解
声明bean的注解
@Component 组件,没有明确的角色
@Service 在业务逻辑层使用(service层)
@Repository 在数据访问层使用(dao层)
@Controller 在展现层使用,控制器的声明(C,web层)
*上面四个注解功能是一样的,都可以用来创建bean实例

3:基于注解方式实现对象的创建

(1):使用注解,需要引入依赖aop jar包,导入项目中
image.png
(2):开启组件扫描

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xmlns:util="http://www.springframework.org/schema/util"
  7. xmlns:tx="http://www.springframework.org/schema/tx"
  8. 需要加context 命名空间
  9. xmlns:context="http://www.springframework.org/schema/context"
  10. xsi:schemaLocation="
  11. http://www.springframework.org/schema/beans
  12. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  13. http://www.springframework.org/schema/aop
  14. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  15. http://www.springframework.org/schema/tx
  16. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  17. http://www.springframework.org/schema/context
  18. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  19. http://www.springframework.org/schema/util
  20. http://www.springframework.org/schema/util/spring-util.xsd
  21. ">
  22. <!-- 开启组件扫描 -->
  23. <!-- context 上下文 -->
  24. <!-- component-scan 组件扫描 -->
  25. <!-- 基本包 包(扫描哪个包下的内容) -->
  26. <!-- 扫描多包下注解,
  27. 方式一: base-package="com.junjay.spring5.dao,com.junjay.spring5.service
  28. 中间以逗号隔开"
  29. 方式二:扫描包的上层目录com.junjay.spring5
  30. -->
  31. <context:component-scan
  32. base-package="com.junjay.spring5"></context:component-scan>
  33. </beans>

(3):创建类,在类上添加创建对象的注解,获取实例对象

  1. package com.junjay.spring5.service;
  2. import org.springframework.stereotype.Component;
  3. // 等同于在xml配置文件中添加<bean id="userService" class="**.**.UserService"></bean>
  4. // id的值,与@Component中的value的值是一样的
  5. // 在注解里面value属性值,可以不省略写。默认值是类名称,首字母小写;eg:UserService =》userService
  6. @Component(value = "userService")
  7. // @Service 四个方式都可以实现对象创建,且默认值都是类名称,首字母小写;
  8. public class UserService {
  9. public void add() {
  10. System.out.println("add------------");
  11. }
  12. }

测试
image.png

4:开启组件扫描中细节

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xmlns:util="http://www.springframework.org/schema/util"
  7. xmlns:tx="http://www.springframework.org/schema/tx"
  8. xmlns:context="http://www.springframework.org/schema/context"
  9. xsi:schemaLocation="
  10. http://www.springframework.org/schema/beans
  11. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  14. http://www.springframework.org/schema/tx
  15. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  16. http://www.springframework.org/schema/context
  17. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  18. http://www.springframework.org/schema/util
  19. http://www.springframework.org/schema/util/spring-util.xsd
  20. ">
  21. <!-- 开启组件扫描 -->
  22. <!-- context 上下文 -->
  23. <!-- component-scan 组件扫描 -->
  24. <!-- 基本包 包(扫描哪个包下的内容) -->
  25. <!-- 扫描多包下注解, 方式一: base-package="com.junjay.spring5.dao,com.junjay.spring5.service
  26. 中间以逗号隔开" 方式二:扫描包的上层目录com.junjay.spring5 -->
  27. <context:component-scan
  28. base-package="com.junjay.spring5"></context:component-scan>
  29. <!-- 实例1 use-default-filters="false" 默认为 true 作用是否扫描com.junjay.spring5下的所有注解,
  30. 改为false之后,可以自定义扫描那些注解 include-filter 扫描那些注解 通俗语言:不在扫描com.junjay.spring5的所有注解,
  31. 而只扫描@Service(org.springframework.stereotype.Service)注解 -->
  32. <context:component-scan
  33. base-package="com.junjay.spring5" use-default-filters="false">
  34. <context:include-filter type="annotation"
  35. expression="org.springframework.stereotype.Service" />
  36. </context:component-scan>
  37. <!-- 实例2 下面配置扫描包所有注解 exclude-filter 不扫描那些注解 白话:扫描com.junjay.spring5下的所有注解,
  38. 但不扫描@Service(org.springframework.stereotype.Service)注解 -->
  39. <context:component-scan
  40. base-package="com.junjay.spring5">
  41. <context:exclude-filter type="annotation"
  42. expression="org.springframework.stereotype.Service" />
  43. </context:component-scan>
  44. </beans>

5:基于注解方式实现属性注入

@AutoWired 根据属性类型自动装配
@Qualifier 根据属性的名称注入要和@AutoWired一起使用
@Resource 可以根据类型输入也可以根据名称注入
@Value 注入普通类型属性

@Autowired 注解
(1):把service和dao创建对象,在service和dao类上加上bean注解

  1. package com.junjay.spring5.dao;
  2. /**
  3. * @author My
  4. * interface 接口关键字
  5. */
  6. public interface UserDao{
  7. public void add();
  8. }
  1. package com.junjay.spring5.dao;
  2. import javax.annotation.Resource;
  3. // <context:exclude-filter type="annotation">
  4. /**
  5. * @author My
  6. * implements 实现 UserDao 接口
  7. */
  8. @Repository
  9. public class UserDaoImpl implements UserDao{
  10. @Override
  11. public void add() {
  12. System.err.println("add.......");
  13. }
  14. }
  1. package com.junjay.spring5.service;
  2. import org.springframework.stereotype.Service;
  3. // 等同于在xml配置文件中添加<bean id="userService" class="**.**.UserService"></bean>
  4. // id的值,与@Component中的value的值是一样的
  5. // 在注解里面value属性值,可以不省略写。默认值是类名称,首字母小写;eg:UserService =》userService
  6. //@Component(value = "userService")
  7. @Service
  8. public class UserService {
  9. public void add() {
  10. System.out.println("add------------");
  11. }
  12. }
  1. (2):在service 注入userdao接口属性,通过@Autowired 注解,不在通过set注入属性
  1. // 定义dao类型属性
  2. // 自动注入属性,不需要在加set方法
  3. @Autowired
  4. private UserDao userDao;
  5. public void add() {
  6. System.out.println("add------------");
  7. userDao.add();
  8. }

测试:
image.png
@Autowired和@Qualifier搭配使用(spring提供的属性注入注解)

  1. // import org.springframework.beans.factory.annotation.Autowired;
  2. // import org.springframework.beans.factory.annotation.Qualifier;
  3. // 定义dao类型属性
  4. // 自动注入属性,不需要在加set方法
  5. @Autowired // 根据类型注入,但一个接口可以有多个实现类,如果有多个类型实现类,就找不到要注入哪一个
  6. @Qualifier(value = "userDaoImpl") // 这个时候就要用@Qualifier通过名称注入属性,默认为首字母小写userDaoImpl
  7. private UserDao userDao;
  8. public void add() {
  9. //System.out.println("add------------");
  10. userDao.add();
  11. }

@Resource 使用(@Resource注解是javax提供的包,spring建议使用Autowired和Qualifier

  1. // import javax.annotation.Resource;
  2. // @Resource // 根据类型注入
  3. @Resource(name = "userDaoImpl") // 根据名称注入
  4. private UserDao userDao;
  5. public void add() {
  6. //System.out.println("add------------");
  7. userDao.add();
  8. }
  1. @value:注入普通类型属性
  1. /**
  2. * 普通类型属性,注入值
  3. */
  4. @Value(value = "adc")
  5. private String name;
  6. // 定义dao类型属性
  7. // 自动注入属性,不需要在加set方法
  8. // @Autowired // 根据类型注入,但一个接口可以有多个实现类,如果有多个类型实现类,就找不到要注入哪一个
  9. // @Qualifier(value = "userDaoImpl") // 这个时候就要用@Qualifier通过名称注入属性,默认为首字母小写userDaoImpl
  10. // private UserDao userDao;
  11. // @Resource // 根据类型注入
  12. @Resource(name = "userDaoImpl") // 根据名称注入
  13. private UserDao userDao;
  14. public void add() {
  15. System.out.println("@Value普通类型注入------------"+name);
  16. userDao.add();
  17. }

测试:image.png

6:完全注解开发

(1):创建配置类,替代xml配置文件,将配置文件中内容放入配置类中
@Configuration的使用AnnotationConfigApplicationContext容器创建过程

  1. package com.junjay.spring5.config;
  2. import org.springframework.context.annotation.ComponentScan;
  3. import org.springframework.context.annotation.Configuration;
  4. // 作为配置类,替代xml配置文件。
  5. @Configuration
  6. // 扫描那些包下的注解
  7. // 等同于xml配置文件中:<context:component-scan base-package="com.junjay.spring5"></context:component-scan>
  8. // 配置文件中名称为 context:上下文,component-scan:扫描包,base-package:扫描包路径
  9. // 对应关系:context——@Configuration component-scan——@ComponentScan;base-package——basePackages =‘数组’
  10. // @Configuration表明了该类是配置类,@ComponentScan 扫描包 basePackages={"com.junjay.spring5"} 扫描那些路径下注解
  11. // 大白话:被@Configuration修饰的类是配置类,开启扫描包,扫描com.junjay.spring5下的注解
  12. @ComponentScan(basePackages = { "com.junjay.spring5", "" })
  13. public class SpringConfig {
  14. }

测试:
image.png
github