このバージョンはまだ開発中であり、まだ安定しているとは見なされていません。最新の安定バージョンについては、Spring Framework 6.2.12 を使用してください! |
@Autowired を使用する
このセクションに含まれる例では、Spring の |
次の例に示すように、@Autowired アノテーションをコンストラクターに適用できます。
Java
Kotlin
public class MovieRecommender {
private final CustomerPreferenceDao customerPreferenceDao;
@Autowired
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
this.customerPreferenceDao = customerPreferenceDao;
}
// ...
}class MovieRecommender @Autowired constructor(
private val customerPreferenceDao: CustomerPreferenceDao)An |
次の例に示すように、従来の setter メソッドに @Autowired アノテーションを適用できます。
Java
Kotlin
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Autowired
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// ...
}class SimpleMovieLister {
@set:Autowired
lateinit var movieFinder: MovieFinder
// ...
}You can apply @Autowired to methods with arbitrary names and multiple arguments, as the following example shows:
Java
Kotlin
public class MovieRecommender {
private MovieCatalog movieCatalog;
private CustomerPreferenceDao customerPreferenceDao;
@Autowired
public void prepare(MovieCatalog movieCatalog,
CustomerPreferenceDao customerPreferenceDao) {
this.movieCatalog = movieCatalog;
this.customerPreferenceDao = customerPreferenceDao;
}
// ...
}class MovieRecommender {
private lateinit var movieCatalog: MovieCatalog
private lateinit var customerPreferenceDao: CustomerPreferenceDao
@Autowired
fun prepare(movieCatalog: MovieCatalog,
customerPreferenceDao: CustomerPreferenceDao) {
this.movieCatalog = movieCatalog
this.customerPreferenceDao = customerPreferenceDao
}
// ...
} 次の例に示すように、@Autowired をフィールドにも適用し、コンストラクターと組み合わせることもできます。
Java
Kotlin
public class MovieRecommender {
private final CustomerPreferenceDao customerPreferenceDao;
@Autowired
private MovieCatalog movieCatalog;
@Autowired
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
this.customerPreferenceDao = customerPreferenceDao;
}
// ...
}class MovieRecommender @Autowired constructor(
private val customerPreferenceDao: CustomerPreferenceDao) {
@Autowired
private lateinit var movieCatalog: MovieCatalog
// ...
} ターゲットコンポーネント(たとえば、 For XML-defined beans or component classes found via classpath scanning, the container usually knows the concrete type up front. However, for |
次の例に示すように、ApplicationContext から特定の型のすべての Bean を提供するように Spring に指示して、@Autowired アノテーションをその型の配列を想定するフィールドまたはメソッドに追加することもできます。
Java
Kotlin
public class MovieRecommender {
@Autowired
private MovieCatalog[] movieCatalogs;
// ...
}class MovieRecommender {
@Autowired
private lateinit var movieCatalogs: Array<MovieCatalog>
// ...
}次の例に示すように、型付きコレクションにも同じことが当てはまります。
Java
Kotlin
public class MovieRecommender {
private Set<MovieCatalog> movieCatalogs;
@Autowired
public void setMovieCatalogs(Set<MovieCatalog> movieCatalogs) {
this.movieCatalogs = movieCatalogs;
}
// ...
}class MovieRecommender {
@Autowired
lateinit var movieCatalogs: Set<MovieCatalog>
// ...
} 配列またはリスト内の項目を特定の順序でソートする場合、ターゲット Bean は
構成クラスの 標準の |
Even typed Map instances can be autowired as long as the expected key type is String. The map values are all beans of the expected type, and the keys are the corresponding bean names, as the following example shows:
Java
Kotlin
public class MovieRecommender {
private Map<String, MovieCatalog> movieCatalogs;
@Autowired
public void setMovieCatalogs(Map<String, MovieCatalog> movieCatalogs) {
this.movieCatalogs = movieCatalogs;
}
// ...
}class MovieRecommender {
@Autowired
lateinit var movieCatalogs: Map<String, MovieCatalog>
// ...
}デフォルトでは、特定のインジェクションポイントに一致する候補 Bean がない場合、オートワイヤーは失敗します。宣言された配列、コレクション、マップの場合、少なくとも 1 つの一致する要素が期待されます。
デフォルトの動作では、アノテーション付きのメソッドとフィールドを必要な依存関係を示すものとして扱います。次の例に示すように、この動作を変更して、フレームワークが不必要なものとしてマークすることで不満足なインジェクションポイントをスキップできるようにします(つまり、@Autowired の required 属性を false に設定することにより)。
Java
Kotlin
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Autowired(required = false)
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// ...
}class SimpleMovieLister {
@Autowired(required = false)
var movieFinder: MovieFinder? = null
// ...
}依存関係(または、複数の引数の場合はその依存関係の 1 つ)が利用できない場合、非必須メソッドはまったく呼び出されません。このような場合、必須ではないフィールドはまったく入力されず、デフォルト値がそのまま残ります。 つまり、 |
@Autowired の required 属性は、複数のコンストラクターを処理する可能性のある Spring のコンストラクター解決アルゴリズムのために、注入されたコンストラクターとファクトリメソッドの引数は特別な場合があります。コンストラクターとファクトリメソッドの引数はデフォルトで効果的に必要ですが、単一のコンストラクターシナリオでは、一致する Bean が利用できない場合に空のインスタンスに解決する複数要素のインジェクションポイント(配列、コレクション、マップ)などのいくつかの特別なルールがありますこれにより、すべての依存関係を一意の複数引数コンストラクターで宣言できる共通の実装パターンが可能になります。たとえば、@Autowired アノテーションなしで単一の public コンストラクターとして宣言できます。
特定の Bean クラスの 1 つのコンストラクターのみが、 |
Alternatively, you can express the non-required nature of a particular dependency through Java’s java.util.Optional, as the following example shows:
public class SimpleMovieLister {
@Autowired
public void setMovieFinder(Optional<MovieFinder> movieFinder) {
...
}
}You can also use a parameter-level @Nullable annotation (of any kind in any package — for example, org.jspecify.annotations.Nullable from JSpecify) or just leverage Kotlin’s built-in null-safety support:
Java
Kotlin
public class SimpleMovieLister {
@Autowired
public void setMovieFinder(@Nullable MovieFinder movieFinder) {
...
}
}class SimpleMovieLister {
@Autowired
var movieFinder: MovieFinder? = null
// ...
} よく知られている解決可能な依存関係であるインターフェースに @Autowired を使用することもできます: BeanFactory、ApplicationContext、Environment、ResourceLoader、ApplicationEventPublisher、MessageSource。これらのインターフェースと、ConfigurableApplicationContext や ResourcePatternResolver などの拡張インターフェースは、特別な設定を必要とせずに自動的に解決されます。次の例では、ApplicationContext オブジェクトをオートワイヤーします。
Java
Kotlin
public class MovieRecommender {
@Autowired
private ApplicationContext context;
public MovieRecommender() {
}
// ...
}class MovieRecommender {
@Autowired
lateinit var context: ApplicationContext
// ...
}The These types must be 'wired up' explicitly by using XML or a Spring |