当前位置: 首页 > news >正文

Spring-1-注解管理依赖

可以将 Spring  其当做一个 容器 ,可以随意的存储对象。

@Component
@Controller
@Service
@Repository

这四个是常用的注解,用于声明 Bean 的注解,可以在后面跟括号为起 value 属性传值,代表依赖注入的 id,若不设置,则默认为 类名首字母小写
这四个注解本质都是同一个注解 @Component,只是名称不同,可以用于三层架构中不同的层
写在需要注入依赖的类上

上面是写在类上的注解

接下来是写在属性或者方法上的注解

@Value ()

给简单属性直接赋括号里的值

@AutoWired  

非简单类型注入,默认根据类型装配,byType
@Autowired+@Qualifier 连用可以根据名字装配,byName
使用方法,在被注入的类上要加 @Compoment 注解
在注入类的非简单属性类型上直接写上 @Autowired 即可自动工具类型装配
当被注入的对象有多个,就无法通过类型直接注入,需要使用名字:
@Autowired
@Qualifier("被注入依赖的id")
两个注解联合使用
Autowired 注解可以使用在,属性,构造方法,构造方法的传参等需要注入依赖处

@Resource

未指定 name 属性则直接根据当前属性名作为名字
该方法与 @Autowired+@Qualifier 连用 的效果一致
区别:不能出现在构造方法上

使用前,需要注入依赖,配置 sping.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>Compomen</artifactId><version>1.0-SNAPSHOT</version><!--仓库--><repositories><!--    spring里程碑版本的仓库--><repository><id>repository.spring.milestone</id><name>Spring Milestone Repository</name><url>https://repo.spring.io/milestone</url></repository></repositories><dependencies>
<!--        AutoWired 需要的依赖--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>6.0.0-M2</version></dependency>
<!--        单元测试--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency>
<!--        Resource 需要的依赖--><dependency><groupId>jakarta.annotation</groupId><artifactId>jakarta.annotation-api</artifactId><version>2.1.1</version></dependency></dependencies><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target></properties></project>

需要对要用到的包进行扫描,在 spring.xml 中声明 (在 resource 目录下创建)

<?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.xsdhttp://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="breeze"/></beans>

需要注入对象的类:

@Component
public class Compomen {@Value("18")int age;@AutowiredAuto auto;@Resource(name="reso")Reso reso;public void printValue(){System.out.println(age);}public void autoIsNullOrNot(){System.out.println(auto == null ? "fail":"succeed");}public void printReso(){reso.ResoText();}}

被注入的两个类:

@Component("reso")
public class Reso {public void ResoText(){System.out.println("Reso这个类中的方法被调用");}
}
@Component
public class Auto {Auto(){System.out.println("Auto类中的构造方法被调用...");}
}

测试程序

    @Testpublic void text(){//拿到配置文件中的容器ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");//拿到容器中的类Compomen compomen = context.getBean("compomen", Compomen.class);compomen.printValue();compomen.autoIsNullOrNot();compomen.printReso();}

运行结果:

Auto类中的构造方法被调用...
18
succeed
Reso这个类中的方法被调用

Process finished with exit code 0


http://www.mrgr.cn/news/7159.html

相关文章:

  • 中间件安全
  • Docker的私有仓库部署——Harbor
  • 如何轻松有效地将 Windows 10 电脑迁移到 SSD
  • 【Electron】桌面应用开发electron-builder打包报错问题处理
  • 在 FPGA 上实现以太网的“低级”指南
  • WRF-LES与PALM微尺度气象大涡模拟实践技术应用
  • 设计模式六大原则之一的接口隔离原则
  • 前端面试——什么是原型和原型链
  • EasyExcel 导入计算公式导出展示字符串问题(SUM)
  • vue3 多文件下载zip压缩包
  • 基于AT32F421的开源电调项目测试
  • 获取当前时间戳
  • Nginx配置负载均衡
  • 如何使用 Java 中的 API 更改 PDF 纸张大小
  • 『基础』线性代数-1行列式
  • 【吊打面试官系列-Memcached面试题】memcached 能接受的 key 的最大长度是多少?
  • 亚信科技转型持久战:扎根行业大模型,深耕行业数字化
  • 户用光伏项目如何操作?
  • 鸿蒙 点击获取电话号拨打电话 @ohos.telephony.call (拨打电话)
  • 【机器翻译方向】数据集合集!