【spring6】Spring IoC注解式开发

1.声明Bean的注解

负责声明Bean的注解,常见的包括四个:

1)@Component;2)@Controller;3)@Service;4)Repository

源码分析:可以看出@Controller、@Service、@Repository这三个注解都是@Component注解的别名。这四个注解的功能相同,只为了增强程序的可读性,建议:控制器类上使用Controller,service类上使用Service,dao类上使用Repository。

java 复制代码
//@Component
@Target(value = {ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface Component {
    String value();
}

//@Controller
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
    @AliasFor(
        annotation = Component.class
    )
    String value() default "";
}

//@Service
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {
    @AliasFor(
        annotation = Component.class
    )
    String value() default "";
}

//@Repository
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {
    @AliasFor(
        annotation = Component.class
    )
    String value() default "";
}

对@Component的使用:

java 复制代码
//只有value时可以省略
@Component(value = "Goods")
public class Goods {
}

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

@Test
    public void testGoodsSpringCode(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        //若只有@Component,不设置value时,则使用类名,首字母小写
        //Object goodsBean = applicationContext.getBean("goods");
        Object goodsBean = applicationContext.getBean("Goods");
        System.out.println(goodsBean);
    }

2.负责注入的注解

给Bean属性赋值需要用到这些注解:

1)@Value;2)@Autowired;3)@Qualifier;4)Resource

使用@Value注入:

java 复制代码
//setter方法上
 @Value("李四")
    public void setName(String name) {
        this.name = name;
    }
//构造方法
 public User(@Value("张三") String name, @Value("33") int age) {
        this.name = name;
        this.age = age;
    }

使用@Autowired注入:注入非简单型

java 复制代码
//该注解有一个required属性,默认值是true,表示在注入的时候要求被注入的Bean必须是存在的,如果不存在则报错。如果required属性设置为false,表示注入的Bean存在或者不存在都没关系
//构造方法上,方法上,形参上,属性上,注解上
@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {
	boolean required() default true;
}


 @Autowired // 在属性上根据类型自动注入,userDao = new UserDao()
 private UserDao userDao;

使用@Qualifier注入:

java 复制代码
//@Autowired注解和@Qualifier注解联合起来才可以根据名称进行装配,在@Qualifier注解中指定Bean名称
    @Autowired
    @Qualifier("userDaoForOracle") // 这个是bean的名字。
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

使用@Resource注入:也可以完成非简单类型注入

与@Autowired区别:

1)Resource注解是JDK扩展包中的,所以该注解是标准注解,更加具有通用性。Autowired注解是Spring框架自己的。

2)Resource注解默认根据名称装配byName,未指定name时,使用属性名作为name。通过name找不到的话会自动启动通过类型byType装配。Autowired注解默认根据类型装配byType,如果想根据名称装配,需要配合@Qualifier注解一起用。

3)Resource注解用在属性上、setter方法上。Autowired注解用在属性上、setter方法上、构造方法上、构造方法参数上。

@使用Resource(高于JDK11或低于JDK8需要引入以下依赖

XML 复制代码
//spring6的依赖
<dependency>
  <groupId>jakarta.annotation</groupId>
  <artifactId>jakarta.annotation-api</artifactId>
  <version>2.1.1</version>
</dependency>

//spring5的依赖
<dependency>
  <groupId>javax.annotation</groupId>
  <artifactId>javax.annotation-api</artifactId>
  <version>1.3.2</version>
</dependency>

3.全注解式开发

全注解开发就是不再使用spring配置文件了,写一个配置类来代替配置文件。

java 复制代码
//配置类
@Configuration
@ComponentScan({"spring6.dao", "spring6.service"})
public class Spring6Configuration {
}

//测试类
@Test
public void testNoXml(){
    ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Spring6Configuration.class);
    UserService userService = applicationContext.getBean("userService", UserService.class);
    userService.save();
}
相关推荐
冒泡的肥皂10 小时前
AI小应用分享
人工智能·后端
quikai198110 小时前
python练习第六组
java·前端·python
阿虎儿10 小时前
本地部署docker完整版minIO镜像
后端
222you10 小时前
线程的常用方法
java·开发语言
亚当10 小时前
SpringBoot中使用MyBatis入门笔记
后端
诺斯贝克10 小时前
Unable to create converter for xxx.NetworkResponse<Auth> for method AuthService
前端·后端
用户693717500138410 小时前
29.Kotlin 类型系统:智能转换:类型检查 (is) 与类型转换 (as)
android·后端·kotlin
用户693717500138410 小时前
30. Kotlin 扩展:为“老类”添“新衣”:扩展函数与扩展属性
android·后端·kotlin
是梦终空10 小时前
JAVA毕业设计259—基于Java+Springboot+vue3工单管理系统的设计与实现(源代码+数据库+开题报告)
java·spring boot·vue·毕业设计·课程设计·工单管理系统·源代码
用户21903265273510 小时前
Spring Boot 集成 Redis 实现看门狗 Lua 脚本分布式锁
java·后端