Spring框架基础(依赖注入-半注解形式)

上回我们学到了使用配置文件形式实现spring框架的依赖注入,这一次我们来看看使用半注解形式实现依赖注入的相关操作(为什么叫半注解呢?因为介绍的依赖注入方法并不是完全的注解形式,还需要配置文件来配置注解扫描器)。

1.创建注解扫描

在resources文件夹下创建一个新的配置文件(我将它命名为applicationContextScan),并在配置文件中输入如下内容来配置注解扫描:

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/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">

    <!--半注解形式-->
    <context:component-scan base-package="com.newFile"></context:component-scan>

</beans>

如果需要测试的话可以直接将Test.java文件中读取配置文件的内容更改为:

java 复制代码
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContextScan.xml");

2.对象注解

对于对象注解来说,可以使用:@Component 通用对象注解、 @Service 业务层对象注解、 @Repository 数据层对象注解 @Controller 控制层对象注解。

我们点击开User类文件,首先在User类文件上使用@Component(value="user1")标注一个名为user1的对象,其他对象注解的使用方法相同。

3.普通属性值注入

使用@Value标签为普通类型属性注入值:

4.复杂属性值注入

那么当一个类的属性是其他非基本数据类型时,该如何进行依赖注入呢?这时就可以使用@Autowired 注解来实现自动注入非基本数据类型属性了。

@Autowired注解相关属性required :

required 属性默认情况下为true

@Autowired(required=true):必须找到 Bean,否则抛异常

@Autowired(required=false):找不到时不报错,注入 null

我们已经在User类中注入User类的相关基本数据类型属性,所以可以直接使用Autowired注入非基本数据类型属性。如果Autowired标注的包装类属性没有注入基本数据类型属性,那么int类型会显示0,String类型或其他类型会显示null。

有一些特殊情况需要使用 @Qualifier 注解来区分实现的具体类:

首先创建service包,在service包下创建UserService接口和serviceImpl包,并在UserService包下创建UserServiceImpl1类和UserServiceImpl2两个类共同实现UserService接口。

是不是很奇怪?为什么要两个实现类来实现一个接口呢?

就拿实际业务过程来举例子吧,比如一个接口中的查找方法可以由Mysql实现也可以由Redis来实现。再比如说一个交易方法可以使用微信交易API来实现,也可以使用支付宝API来实现

java 复制代码
//创建service接口
package com.newFile.service;

public interface UserService {
    public void test();
}

//实现类1
package com.newFile.service.serviceImpl;

import com.newFile.service.UserService;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl1 implements UserService{
    public void test(){
        System.out.println("这是方法1");
    }
}

//实现类2
package com.newFile.service.serviceImpl;

import com.newFile.service.UserService;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl2 implements UserService{
    public void test(){
        System.out.println("这是方法2");
    }
}

此时如果我们创建一个Controller类并创建一个UserService类型对象并使用Autowired注入:

java 复制代码
package com.newFile.Controller;

import com.newFile.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

@Controller
public class UserController {
    @Autowired
    UserService userService;
}

就会报错:

Could not autowire. There is more than one bean of 'UserService' type.
Beans:
userServiceImpl1 (UserServiceImpl1.java)

userServiceImpl2 (UserServiceImpl2.java)

原因就是UserService接口由两个Service来实现,当创建UserService对象并使用Autowired进行注入时,Spring容器无法识别是是注入Service1类还是注入Service2类。

此时就需要使用@Qualifired标签来标注需要注入的真实类。

java 复制代码
    @Autowired
    @Qualifier(value = "userServiceImpl1")
    UserService userService;
相关推荐
码云社区1 小时前
Java家政服务系统源码:人员技能、服务范围与多商家业务落地
java·开发语言·家政服务·家政小程序·家政系统
Sashtok2 小时前
cpp基础知识(10)
c++·后端
摇滚侠2 小时前
《On Java 中文版 基础卷》阅读笔记 接口 18
java·笔记·python
CoderYanger2 小时前
Java EE 进阶:9.MyBatis 操作数据库(进阶)(第八弹):MyBatis Generator
java·数据库·spring boot·spring·java-ee·tomcat·mybatis
行百里er3 小时前
【Spring Insight 技术分享】runtime-settings.json 的妙用
后端·开源·监控
databook4 小时前
处理一批 PDF,怎么算真的处理完了
后端·python
苏三说技术5 小时前
为什么越来越多人用 Docling ? - 苏三说技术
后端
nice_lcj5205 小时前
【从零理解计算机网络 · 12】登录态的载体:Cookie、Session、Token 在网络层怎么走
后端·计算机网络·面试·jwt·token·cookie·session
jaysee-sjc5 小时前
【黑马点评】Day02:优惠券秒杀与分布式锁
java·分布式·nginx·spring·node.js·tomcat·maven
秋天的怀念_David6 小时前
算法一千零一夜 · 第三十五夜 · 恢复风暴
java·程序人生·算法