体验spring(六)---depends-on及parent

depend-on、延迟初始化Bean Bean定义的继承

depend-on

案例:
StudentServiceImpl.java
public class StudentServiceImpl {
	
	/**默认构造器*/
	public StudentServiceImpl() {
		System.out.println("studnetService初始化完成...");
	}
}
public class TeacherServiceImpl {
	/**默认构造器*/
	public TeacherServiceImpl() {
		System.out.println("teacherservice初始化完毕...");
	}
}
dependson.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<!--
		1、depends-on 在初始化teacherServiceImpl之前进行初始化depends-on的属性值匹配的bean
		2、你也可以吧teacherServiceImpl所以依赖的bean都放在teacherService之前并且scope是singleton模式即可
		备注: 只使用第一种方式即可。
	-->
	<bean id="studentServiceImpl" class="cn.csdn.service.StudentServiceImpl" scope=" singleton "/>

	 <bean id="teacherServiceImpl"  class="cn.csdn.service.TeacherServiceImpl" scope="singleton" />
   
</beans>
运行结果:studnetService初始化完成...
teacherservice初始化完毕...
注意的问题:
1.若teacherServiceImpl的bean在前 作用域为scope=’singleton’
     studentServiceImpl的bean在后 作用域也为scope=’singleton’则运行结果为:
teacherservice初始化完毕...
studnetService初始化完成...
2. 若teacherServiceImpl的bean在前 作用域为scope=’singleton’ 并且在其中添加depends-on=’ studentServiceImpl’
     studentServiceImpl的bean在后 作用域也为scope=’singleton’则运行结果为:
studnetService初始化完成...
teacherservice初始化完毕...
3.若studentServiceImpl的bean在前 并且其作用域改为scope=’prototype’  则运行结果为:teacherservice初始化完毕...
4.若studentServiceImpl的bean在前其作用域仍为scope=’prototype’而在teacherServiceImpl的bean中添加depends-on=’ studentServiceImpl’ 其作用域为scope=’singleton’ 则运行结果为:studnetService初始化完成...
teacherservice初始化完毕...

可依据上述结果相互比较来理解depend-on的使用方法

 

Bean定义的继承

案例:
EmpServiceImpl.java
public class EmpServiceImpl {
private String name;

public String getName() {
	return name;
}

public void setName(String name) {
	this.name = name;
}
}
HourEmpServiceImpl.java
public class HourEmpServiceImpl extends EmpServiceImpl{
 private Integer hours;

public void setHours(Integer hours) {
	this.hours = hours;
}
}
RegularEmpServiceImpl.java
public class RegularEmpServiceImpl extends EmpServiceImpl {
private String title;

public void setTitle(String title) {
	this.title = title;
}
}
parent.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
	<bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl"
		scope="singleton" >		
		<property name="name" >
			<value >dxl</value>
		</property>
	</bean>
	
      <!--继承了empServiceImpl中的name属性的setName()方法-->
	<bean id="hourEmpServiceImpl" class="cn.csdn.service.HourEmpServiceImpl"
		scope="singleton"   parent="empServiceImpl">		
		<property name="hours">
			<value>20</value>
		</property>
	</bean>

	<bean id="regularEmpServiceImpl" class="cn.csdn.service.RegularEmpServiceImpl"
		scope="singleton" parent="empServiceImpl">
		<property name="name">
			<value>xiaoli</value>
		</property>
		<property name="title">
			<value>大二学生</value>
		</property>
	</bean>
</beans>
HourEmpServiceImpl.java类与RegularEmpServiceImpl.java类继承了EmpServiceImpl.java类 
运行hourEmpServiceImpl可以或得empServiceImpl类中 的name属性也有自己的hours属性;
运行RegularEmpServiceImpl可以或得自己的属性即name=’xiaoli’ title=’大二学生’ 继承的父类中的name属性将被自己的name属性覆盖。

 

延迟初始化Bean在体验spring(三)中已有补充

 

 

【直流微电网】径向直流微电网的状态空间建模与线性化:一种耦合DC-DC变换器状态空间平均模型的方法 (Matlab代码实现)内容概要:本文介绍了径向直流微电网的状态空间建模与线性化方法,重点提出了一种基于耦合DC-DC变换器状态空间平均模型的建模策略。该方法通过对系统中多个相互耦合的DC-DC变换器进行统一建模,构建出整个微电网的集中状态空间模型,并在此基础上实施线性化处理,便于后续的小信号分析与稳定性研究。文中详细阐述了建模过程中的关键步骤,包括电路拓扑分析、状态变量选取、平均化处理以及雅可比矩阵的推导,最终通过Matlab代码实现模型仿真验证,展示了该方法在动态响应分析和控制器设计中的有效性。; 适合人群:具备电力电子、自动控制理论基础,熟悉Matlab/Simulink仿真工具,从事微电网、新能源系统建模与控制研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①掌握直流微电网中多变换器系统的统一建模方法;②理解状态空间平均法在非线性电力电子系统中的应用;③实现系统线性化并用于稳定性分析与控制器设计;④通过Matlab代码复现和扩展模型,服务于科研仿真与教学实践。; 阅读建议:建议读者结合Matlab代码逐步理解建模流程,重点关注状态变量的选择与平均化处理的数学推导,同时可尝试修改系统参数或拓扑结构以加深对模型通用性和适应性的理解。
<think>我们正在处理一个SBOM(软件物料清单)文件,该文件在上传到Dependency-Track时出现了验证错误。错误信息明确指出: ```json { "status": 400, "title": "The uploaded BOM is invalid", "detail": "Schema validation failed", "errors": [ "$.dependencies: must have only unique items in the array" ] } ``` 这个错误表明,在BOM文件的`dependencies`数组中存在重复的项目,而根据CycloneDX规范,数组中的每个项目必须是唯一的。 ### 问题分析 在CycloneDX规范中,`dependencies`数组用于描述组件之间的依赖关系。每个依赖项应该由以下属性唯一标识(根据规范): - `ref`:引用组件的唯一标识符(通常是组件的`bom-ref`) - 其他属性(如`dependsOn`数组)描述该组件的依赖 但是,注意错误信息中提到的`dependencies`数组,这里指的是顶层`dependencies`数组,它包含多个依赖项对象。每个对象代表一个组件及其依赖的组件。根据错误,这个数组中出现了重复的项。 ### 解决方案 为了确保`dependencies`数组中的每个项目都是唯一的,我们需要检查每个项目是否具有唯一的`ref`值。因为每个组件的`ref`(即`bom-ref`)在BOM中应该是唯一的。因此,修复步骤为: 1. **检查BOM文件中的`dependencies`数组**,确保每个对象的`ref`属性都是唯一的。 2. **如果存在重复的`ref`**,则需要合并这些重复项,或者删除重复项(但注意合并可能需要合并`dependsOn`数组)。 ### 具体操作步骤 假设我们有一个名为`bom.json`的文件,我们可以使用`jq`工具来处理。 #### 步骤1:提取当前`dependencies`数组中的`ref`值,检查重复项 ```bash jq '.dependencies[].ref' bom.json | sort | uniq -d ``` 如果这个命令有输出,说明有重复的`ref`值。 #### 步骤2:去重处理 我们可以使用`jq`对`dependencies`数组进行去重,确保每个`ref`只出现一次。同时,如果重复项中的`dependsOn`数组有不同,我们需要合并它们(取并集)。但是,通常在一个正确的BOM中,同一个组件的依赖关系应该是一致的,所以如果出现重复,可能是生成工具的错误。在这种情况下,我们可以简单地取第一个出现的项(或者合并`dependsOn`数组)。 以下是一个合并的示例(取第一个出现的项,忽略后续重复项): ```bash jq '.dependencies |= group_by(.ref) | map(.[0])' bom.json > bom_fixed.json ``` 但是,如果同一个组件的依赖项出现在多个地方,并且`dependsOn`数组不同,我们可能需要合并这些依赖。下面是一个合并`dependsOn`数组的示例(将同一个`ref`的所有`dependsOn`数组合并,然后去重): ```bash jq '.dependencies |= group_by(.ref) | map({ ref: .[0].ref, dependsOn: (map(.dependsOn?) | flatten | unique) })' bom.json > bom_fixed.json ``` 注意:上面的命令中,`map(.dependsOn?)`会提取每个重复项的`dependsOn`数组(如果不存在则为空),然后合并成一个数组,最后去重(`unique`)。 #### 步骤3:验证修复后的文件 使用`jq`检查修复后的文件是否还有重复的`ref`: ```bash jq '.dependencies[].ref' bom_fixed.json | sort | uniq -d ``` 应该没有输出。 ### 完整示例 假设原始`bom.json`文件如下: ```json { "bomFormat": "CycloneDX", "specVersion": "1.4", "dependencies": [ { "ref": "pkg:maven/org.apache.tomcat/tomcat@9.0.0", "dependsOn": [ "pkg:maven/org.apache.tomcat/tomcat-util@9.0.0" ] }, { "ref": "pkg:maven/org.apache.tomcat/tomcat@9.0.0", "dependsOn": [ "pkg:maven/org.apache.tomcat/tomcat-coyote@9.0.0" ] } ] } ``` 这个文件中,同一个`ref`(`pkg:maven/org.apache.tomcat/tomcat@9.0.0`)出现了两次,且`dependsOn`不同。我们需要合并这两个对象。 使用以下命令合并: ```bash jq '.dependencies |= group_by(.ref) | map({ ref: .[0].ref, dependsOn: (map(.dependsOn) | flatten | unique) })' bom.json > bom_fixed.json ``` 修复后的文件: ```json { "bomFormat": "CycloneDX", "specVersion": "1.4", "dependencies": [ { "ref": "pkg:maven/org.apache.tomcat/tomcat@9.0.0", "dependsOn": [ "pkg:maven/org.apache.tomcat/tomcat-util@9.0.0", "pkg:maven/org.apache.tomcat/tomcat-coyote@9.0.0" ] } ] } ``` ### 在CI/CD中集成 如果是在GitLab CI中,可以这样集成: ```yaml stages: - sbom - fix - upload generate_sbom: stage: sbom image: opensca/opensca-cli:latest script: - opensca-cli -path $CI_PROJECT_DIR -out bom.json -format cyclonedx-json artifacts: paths: - bom.json fix_bom: stage: fix image: alpine/jq needs: ["generate_sbom"] script: - jq '.dependencies |= group_by(.ref) | map({ ref: .[0].ref, dependsOn: (map(.dependsOn?) | flatten | unique) })' bom.json > bom_fixed.json artifacts: paths: - bom_fixed.json upload_to_dependency_track: stage: upload image: curlimages/curl:latest needs: ["fix_bom"] script: - | curl -X POST "https://dtrack.example.com/api/v1/bom" \ -H "Content-Type: application/json" \ -H "X-Api-Key: $DT_API_KEY" \ --data-binary "@bom_fixed.json" ``` ### 数学原理 在去重过程中,我们实际上是将依赖项按照`ref`分组,然后对每个组(即每个`ref`)构造一个新的依赖项,其`dependsOn`数组是组内所有`dependsOn`数组的并集。 设$G$为分组函数,$G(ref)$返回所有具有相同`ref`的依赖项集合。对于每个组$g \in G$,新的依赖项$d_{new}$定义为: $$d_{new} = \{ ref: ref_g, dependsOn: \bigcup_{d \in g} d.dependsOn \}$$ 其中$\bigcup$表示并集运算,然后对并集结果进行去重(即集合运算)。 ### 相关问题 1. 如何验证CycloneDX BOM文件的规范性? 2. 除了`dependencies`数组重复,还有哪些常见的CycloneDX模式验证错误? 3. 如何确保生成SBOM的工具不产生重复的依赖项? 4. 在合并`dependsOn`数组时,如果遇到循环依赖,应该如何处理? 5. 如何优化大型BOM文件的处理性能? 通过以上步骤,我们可以修复SBOM文件中`dependencies`数组的重复问题,使其通过Dependency-Track的模式验证。</think>### 问题分析与解决方案 根据您提供的错误信息和历史上下文,问题核心在于 **BOM 文件中`dependencies`数组存在重复项**,违反了 CycloneDX 规范中的唯一性约束。错误信息明确提示: ``` $.dependencies: must have only unique items in the array ``` ### BOM 文件 `dependencies` 数组格式要求 在 CycloneDX 规范中,`dependencies` 数组需要满足以下关键格式要求以确保唯一性: 1. **唯一标识符规则** 每个依赖项必须通过复合键 **`(groupId, artifactId, type, classifier)`** 唯一标识[^1][^4]。 数学表示为: $$ \forall d_i, d_j \in D,\quad i \neq j \implies \text{key}(d_i) \neq \text{key}(d_j) $$ 其中 $\text{key}(d) = (d.\text{groupId}, d.\text{artifactId}, d.\text{type}, d.\text{classifier})$ 2. **版本处理原则** 同一组件的多个版本被视为不同依赖项,但需避免重复声明相同版本[^2]: ```json // 允许:不同版本 {"groupId": "org.apache", "artifactId": "commons", "version": "1.0"}, {"groupId": "org.apache", "artifactId": "commons", "version": "2.0"} // 禁止:相同版本重复 {"groupId": "org.apache", "artifactId": "commons", "version": "1.0"}, {"groupId": "org.apache", "artifactId": "commons", "version": "1.0"} // 重复! ``` 3. **空值处理** `groupId`/`classifier` 等字段为空时需显式表示为 `null` 而非省略: ```json // 正确 {"groupId": null, "artifactId": "vue", "version": "3.2.0"} // 错误(会导致唯一性判断失效) {"artifactId": "vue", "version": "3.2.0"} ``` ### 修复步骤(使用 jq 工具) #### 步骤 1:检查重复项 ```bash # 列出所有重复的依赖项标识 jq -r '.dependencies[] | "\(.group // "null"):\(.name):\(.type // "null"):\(.classifier // "null")"' bom.json | sort | uniq -d ``` #### 步骤 2:去重处理 ```bash jq ' .dependencies |= map(. + { # 构造唯一键,处理空值 _key: "\(.group // "null"):\(.name):\(.type // "null"):\(.classifier // "null")" }) | group_by(._key) | # 按唯一键分组 map(.[0]) | # 取每组的第一个元素 map(del(._key)) # 删除临时键 ' bom.json > fixed_bom.json ``` #### 步骤 3:版本冲突处理(可选) ```bash jq ' .dependencies |= group_by("\(.group // "null"):\(.name)") | # 按组件分组 map(sort_by(.version) | reverse | .[0]) # 取每组最高版本 ' fixed_bom.json > final_bom.json ``` #### 步骤 4:格式验证 ```bash # 验证所有字段符合规范 jq '.dependencies[] | has("group"), has("name"), has("version")' final_bom.json | grep false # 验证唯一性 jq -r '.dependencies[] | "\(.group // "null"):\(.name):\(.type // "null"):\(.classifier // "null")"' final_bom.json | sort | uniq -d | wc -l # 输出应为 0 ``` ### 修复原理的数学描述 设依赖项集合 $D = \{d_1, d_2, \dots, d_n\}$,定义等价关系: $$ d_i \sim d_j \iff \text{key}(d_i) = \text{key}(d_j) $$ 其中 $\text{key}(d) = (g, a, t, c)$ 表示组件的唯一标识元组。 通过商集构造: $$ D/{\sim} = \{[d]_1, [d]_2, \dots, [d]_k\} \quad (k \leq n) $$ 每个等价类 $[d]_i$ 代表唯一组件,从中选取代表元素即实现去重[^4]。 对于版本冲突,采用版本最大化策略: $$ \text{version}_{\text{selected}} = \max_{\text{SemVer}} \{ \text{ver}(d) \mid d \in [d]_i \} $$ 其中 SemVer 遵循语义化版本比较规则[^2]。 ### 预防措施 1. **生成工具配置** 在 OpenSCA 中启用内置去重: ```bash opensca-cli -path ./project -dedup true -out bom.json ``` 2. **构建文件规范** 在 Maven/Gradle 中显式声明依赖版本: ```xml <!-- Maven 示例 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.12.0</version> </dependency> </dependencies> </dependencyManagement> ``` 3. **CI/CD 集成检查** ```yaml validate_sbom: image: alpine/jq script: - 'count=$(jq -r ".dependencies[] | \"\\(.group):\\(.name)\"" bom.json | sort | uniq -d | wc -l)' - 'if [ $count -gt 0 ]; then echo "Duplicate dependencies found!"; exit 1; fi' ``` ### 相关问题 1. 如何自动化验证 BOM 文件是否符合 CycloneDX 规范? 2. 在大型项目中优化 SBOM 生成性能的策略有哪些? 3. 如何处理嵌套依赖(如 Docker 镜像中的多层依赖)? 4. Dependency-Track 的 API 速率限制如何避免? 5. 开源组件许可证冲突在 SBOM 中如何标识和报告? [^1]: 关于组件唯一标识符的 CycloneDX 规范要求 [^2]: 语义化版本比较规则在依赖解析中的应用 [^4]: 集合论在依赖去重中的实现原理
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值