

这是 AWS CDK v2 开发者指南。旧版 CDK v1 于 2022 年 6 月 1 日进入维护阶段，并于 2023 年 6 月 1 日终止支持。

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# mixins
<a name="mixins"></a>

Mixin 是可重复使用的功能，你可以使用该`.with()`方法将其应用于构造。它们为 L1（CloudFormation级别）和 L2（基于意图）结构添加了功能，例如版本控制、自动删除对象或阻止公共访问。每个 mixin 都在单个资源上运行。要连接两个资源，请改用 [Facades](facades.md)。

每个 mixin 都以特定的资源类型为目标，并以该资源命名。例如，以 Amazon S3 存储桶为`BucketVersioning`目标。您可以通过每个服务模块的`mixins`命名空间访问 mixin，例如`s3.mixins`。

## 应用 Mixins
<a name="mixins-basic"></a>

您可以使用该方法应用混合，该`.with()`方法适用于所有构造。你可以将多个混音链接在一起：

**Example**  

```
import * as s3 from 'aws-cdk-lib/aws-s3';

const bucket = new s3.CfnBucket(this, 'MyBucket')
  .with(new s3.mixins.BucketVersioning())
  .with(new s3.mixins.BucketBlockPublicAccess());
```

```
const s3 = require('aws-cdk-lib/aws-s3');

const bucket = new s3.CfnBucket(this, 'MyBucket')
  .with(new s3.mixins.BucketVersioning())
  .with(new s3.mixins.BucketBlockPublicAccess());
```

```
import aws_cdk.aws_s3 as s3

bucket = s3.CfnBucket(self, "MyBucket") \
    .with_(s3.mixins.BucketVersioning()) \
    .with_(s3.mixins.BucketBlockPublicAccess())
```

```
import software.amazon.awscdk.services.s3.*;

CfnBucket bucket = new CfnBucket(this, "MyBucket");
bucket.with(new BucketVersioning());
bucket.with(new BucketBlockPublicAccess());
```

```
using Amazon.CDK.AWS.S3;

var bucket = new CfnBucket(this, "MyBucket");
bucket.With(new BucketVersioning());
bucket.With(new BucketBlockPublicAccess());
```

```
bucket := awss3.NewCfnBucket(stack, jsii.String("MyBucket"), nil)
bucket.With(awss3.NewBucketVersioning())
bucket.With(awss3.NewBucketBlockPublicAccess())
```

每个 mixin 都声明它支持哪些资源类型。如果你将 mixin 应用于它不支持的构造，它将被静默跳过。这意味着您可以安全地广泛应用混合，而不必担心类型不匹配。如果您需要确保使用混合，请使用[`requireAll()`或`requireAny()`](#mixins-advanced)。

## 使用带有 L1 和 L2 结构的 Mixins
<a name="mixins-l1-l2"></a>

Mixin 可与 L1 和 L2 结构一起使用。当你将 mixin 应用于 L2 构造时，它也适用于其背后的 L1 资源。

以下示例说明如何将 mixin 应用于 L1 和 L2 构造：

**Example**  

```
import * as cdk from 'aws-cdk-lib';
import * as s3 from 'aws-cdk-lib/aws-s3';

// Using a mixin with an L1 construct
new s3.CfnBucket(this, 'L1Bucket')
  .with(new s3.mixins.BucketVersioning());

// Using a mixin with an L2 construct
new s3.Bucket(this, 'L2Bucket', {
  removalPolicy: cdk.RemovalPolicy.DESTROY,
}).with(new s3.mixins.BucketAutoDeleteObjects());
```

```
const cdk = require('aws-cdk-lib');
const s3 = require('aws-cdk-lib/aws-s3');

// Using a mixin with an L1 construct
new s3.CfnBucket(this, 'L1Bucket')
  .with(new s3.mixins.BucketVersioning());

// Using a mixin with an L2 construct
new s3.Bucket(this, 'L2Bucket', {
  removalPolicy: cdk.RemovalPolicy.DESTROY,
}).with(new s3.mixins.BucketAutoDeleteObjects());
```

```
import aws_cdk as cdk
import aws_cdk.aws_s3 as s3

# Using a mixin with an L1 construct
s3.CfnBucket(self, "L1Bucket") \
    .with_(s3.mixins.BucketVersioning())

# Using a mixin with an L2 construct
s3.Bucket(self, "L2Bucket",
    removal_policy=cdk.RemovalPolicy.DESTROY,
).with_(s3.mixins.BucketAutoDeleteObjects())
```

```
import software.amazon.awscdk.*;
import software.amazon.awscdk.services.s3.*;

// Using a mixin with an L1 construct
CfnBucket l1Bucket = new CfnBucket(this, "L1Bucket");
l1Bucket.with(new BucketVersioning());

// Using a mixin with an L2 construct
Bucket l2Bucket = Bucket.Builder.create(this, "L2Bucket")
        .removalPolicy(RemovalPolicy.DESTROY)
        .build();
l2Bucket.with(new BucketAutoDeleteObjects());
```

```
using Amazon.CDK;
using Amazon.CDK.AWS.S3;

// Using a mixin with an L1 construct
var l1Bucket = new CfnBucket(this, "L1Bucket");
l1Bucket.With(new BucketVersioning());

// Using a mixin with an L2 construct
var l2Bucket = new Bucket(this, "L2Bucket", new BucketProps
{
    RemovalPolicy = RemovalPolicy.DESTROY
});
l2Bucket.With(new BucketAutoDeleteObjects());
```

```
l1Bucket := awss3.NewCfnBucket(stack, jsii.String("L1Bucket"), nil)
l1Bucket.With(awss3.NewBucketVersioning())

l2Bucket := awss3.NewBucket(stack, jsii.String("L2Bucket"), &awss3.BucketProps{
    RemovalPolicy: awscdk.RemovalPolicy_DESTROY,
})
l2Bucket.With(awss3.NewBucketAutoDeleteObjects())
```

## 混合物与构造属性
<a name="mixins-vs-props"></a>

Mixin 和构造属性可以协同工作。L2 构造属性会在您创建资源时对其进行设置。Mixins 可以随时使用。

在以下情况下使用 L2 构造属性  
您使用的是 L2 构造，并且所需的属性已可用。这是最简单的方法。

在以下情况下使用 Mixins  
+ 你正在使用 L1 构造，想要类似 L2 的功能。
+ 您想向 L2 构造中添加不可用作属性的功能。
+ 你想在多个不同类型的构造中应用相同的功能。

Mixin 不能取代构造属性。他们不能将必需的属性设为可选属性或更改默认值。

## 将 Mixins 应用于多个构造
<a name="mixins-advanced"></a>

该 `Mixins.of()` API 可以更好地控制如何跨作用域应用混合。与其调用`.with()`单个构造，不如同时将 mixin 应用于堆栈或作用域中的所有匹配构造：

**Example**  

```
import { Mixins } from 'aws-cdk-lib';
import * as s3 from 'aws-cdk-lib/aws-s3';

// Apply to all supported constructs in the stack
Mixins.of(stack).apply(new s3.mixins.BucketVersioning());
```

```
const { Mixins } = require('aws-cdk-lib');
const s3 = require('aws-cdk-lib/aws-s3');

// Apply to all supported constructs in the stack
Mixins.of(stack).apply(new s3.mixins.BucketVersioning());
```

```
from aws_cdk import Mixins
import aws_cdk.aws_s3 as s3

# Apply to all supported constructs in the stack
Mixins.of(stack).apply(s3.mixins.BucketVersioning())
```

```
import software.amazon.awscdk.Mixins;
import software.amazon.awscdk.services.s3.*;

// Apply to all supported constructs in the stack
Mixins.of(stack).apply(new BucketVersioning());
```

```
using Amazon.CDK;
using Amazon.CDK.AWS.S3;

// Apply to all supported constructs in the stack
Mixins.Of(stack).Apply(new BucketVersioning());
```

```
awscdk.Mixins_Of(stack, nil).Apply(awss3.NewBucketVersioning())
```

默认情况下，不支持 mixin 的构造会被静默跳过。`requireAll()`用于断言 mixin 已应用于选择中的每个构造，或者`requireAny()`断言它已应用于至少一个构造。这对于强制资源具有必需的配置很有用：

**Example**  

```
// Throws an error if any construct in the scope doesn't support the mixin
Mixins.of(stack)
  .requireAll()
  .apply(new s3.mixins.BucketVersioning());
```

```
// Throws an error if any construct in the scope doesn't support the mixin
Mixins.of(stack)
  .requireAll()
  .apply(new s3.mixins.BucketVersioning());
```

```
# Throws an error if any construct in the scope doesn't support the mixin
Mixins.of(stack) \
    .require_all() \
    .apply(s3.mixins.BucketVersioning())
```

```
// Throws an error if any construct in the scope doesn't support the mixin
Mixins.of(stack)
        .requireAll()
        .apply(new BucketVersioning());
```

```
// Throws an error if any construct in the scope doesn't support the mixin
Mixins.Of(stack)
    .RequireAll()
    .Apply(new BucketVersioning());
```

```
awscdk.Mixins_Of(stack, nil).RequireAll().Apply(awss3.NewBucketVersioning())
```

## 混音和方面
<a name="mixins-aspects"></a>

Mixins 和 As [p](aspects.md) ects 是相关的，但用途不同：
+  当你打电话`.with()`时，**混音**会立即生效。您可以精确地选择要将它们应用于哪些构造。
+  在合成过程中，**各个方面**适用于作用域中的所有构造。使用它们来制定广泛的策略和检查。

使用 Mixins 向特定构造添加功能。使用 Aspects 在整个应用程序中强制执行规则或应用更改。

## 相关资源
<a name="mixins-related"></a>
+  [外观 —](facades.md) 将资源与 IAM 委托人和其他服务连接起来。
+  [方面](aspects.md)-在整个应用程序中应用更改或验证结构。
+  [构造](constructs.md) — 了解 L1、L2 和 L3 结构。
+  [自定义构造](cfn-layer.md) — 使用逃生舱口和原始覆盖自定义构造。