or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

annotation-config.mdaot-support.mdbean-definition.mdbean-factory.mdindex.mdproperty-access.mdxml-config.md

bean-definition.mddocs/

0

# Bean Definition and Configuration

1

2

Bean definition infrastructure supporting multiple configuration sources (XML, annotations, programmatic) with comprehensive metadata management and validation. This system defines how beans should be created, configured, and managed within the Spring IoC container.

3

4

## Capabilities

5

6

### Bean Definition Interface

7

8

Core metadata interface that describes a bean instance, including property values, constructor arguments, and other configuration details.

9

10

```java { .api }

11

/**

12

* A BeanDefinition describes a bean instance, which has property values, constructor argument values, and further information supplied by concrete implementations.

13

*/

14

interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {

15

/** Scope identifier for the standard singleton scope. */

16

String SCOPE_SINGLETON = ConfigurableBeanFactory.SCOPE_SINGLETON;

17

/** Scope identifier for the standard prototype scope. */

18

String SCOPE_PROTOTYPE = ConfigurableBeanFactory.SCOPE_PROTOTYPE;

19

20

/** Role hint indicating that a BeanDefinition is a major part of the application. */

21

int ROLE_APPLICATION = 0;

22

/** Role hint indicating that a BeanDefinition is a supporting part of some larger configuration. */

23

int ROLE_SUPPORT = 1;

24

/** Role hint indicating that a BeanDefinition is providing an entirely background role. */

25

int ROLE_INFRASTRUCTURE = 2;

26

27

/**

28

* Set the name of the parent definition of this bean definition, if any.

29

* @param parentName the parent name

30

*/

31

void setParentName(String parentName);

32

33

/**

34

* Return the name of the parent definition of this bean definition, if any.

35

* @return the parent name, or null if none is set

36

*/

37

String getParentName();

38

39

/**

40

* Specify the bean class name of this bean definition.

41

* @param beanClassName the bean class name

42

*/

43

void setBeanClassName(String beanClassName);

44

45

/**

46

* Return the current bean class name of this bean definition.

47

* @return the bean class name, or null if none defined

48

*/

49

String getBeanClassName();

50

51

/**

52

* Override the target scope of this bean, specifying a new scope name.

53

* @param scope the new scope name

54

*/

55

void setScope(String scope);

56

57

/**

58

* Return the name of the current target scope for this bean, or null if not known yet.

59

* @return the scope name, or null if none specified

60

*/

61

String getScope();

62

63

/**

64

* Set whether this bean should be lazily initialized.

65

* @param lazyInit whether to apply lazy-init

66

*/

67

void setLazyInit(boolean lazyInit);

68

69

/**

70

* Return whether this bean should be lazily initialized.

71

* @return whether to apply lazy-init

72

*/

73

boolean isLazyInit();

74

75

/**

76

* Set the names of the beans that this bean depends on being initialized.

77

* @param dependsOn the bean names that this bean depends on

78

*/

79

void setDependsOn(String... dependsOn);

80

81

/**

82

* Return the bean names that this bean depends on.

83

* @return the bean names that this bean depends on, or null if none

84

*/

85

String[] getDependsOn();

86

87

/**

88

* Set whether this bean is a candidate for getting autowired into some other bean.

89

* @param autowireCandidate whether this bean is a candidate for autowiring

90

*/

91

void setAutowireCandidate(boolean autowireCandidate);

92

93

/**

94

* Return whether this bean is a candidate for getting autowired into some other bean.

95

* @return whether this bean is a candidate for autowiring

96

*/

97

boolean isAutowireCandidate();

98

99

/**

100

* Set whether this bean is a primary autowire candidate.

101

* @param primary whether this bean is a primary autowire candidate

102

*/

103

void setPrimary(boolean primary);

104

105

/**

106

* Return whether this bean is a primary autowire candidate.

107

* @return whether this bean is a primary autowire candidate

108

*/

109

boolean isPrimary();

110

111

/**

112

* Specify the factory bean to use, if any.

113

* @param factoryBeanName the factory bean name, or null if none

114

*/

115

void setFactoryBeanName(String factoryBeanName);

116

117

/**

118

* Return the factory bean name, if any.

119

* @return the factory bean name, or null if none set

120

*/

121

String getFactoryBeanName();

122

123

/**

124

* Specify a factory method, if any.

125

* @param factoryMethodName the factory method name, or null if none

126

*/

127

void setFactoryMethodName(String factoryMethodName);

128

129

/**

130

* Return a factory method, if any.

131

* @return the factory method name, or null if none specified

132

*/

133

String getFactoryMethodName();

134

135

/**

136

* Return the constructor argument values for this bean.

137

* @return the constructor argument values (never null)

138

*/

139

ConstructorArgumentValues getConstructorArgumentValues();

140

141

/**

142

* Return if there are constructor argument values defined for this bean.

143

* @return if there are constructor argument values defined for this bean

144

*/

145

default boolean hasConstructorArgumentValues() {

146

return !getConstructorArgumentValues().isEmpty();

147

}

148

149

/**

150

* Return the property values to be applied to a new instance of the bean.

151

* @return the property values for this bean (never null)

152

*/

153

MutablePropertyValues getPropertyValues();

154

155

/**

156

* Return if there are property values defined for this bean.

157

* @return if there are property values defined for this bean

158

*/

159

default boolean hasPropertyValues() {

160

return !getPropertyValues().isEmpty();

161

}

162

163

/**

164

* Set the name of the initializer method.

165

* @param initMethodName the initializer method name

166

*/

167

void setInitMethodName(String initMethodName);

168

169

/**

170

* Return the name of the initializer method.

171

* @return the initializer method name, or null if none set

172

*/

173

String getInitMethodName();

174

175

/**

176

* Set the name of the destroy method.

177

* @param destroyMethodName the destroy method name

178

*/

179

void setDestroyMethodName(String destroyMethodName);

180

181

/**

182

* Return the name of the destroy method.

183

* @return the destroy method name, or null if none set

184

*/

185

String getDestroyMethodName();

186

187

/**

188

* Set the role hint for this BeanDefinition.

189

* @param role the role hint

190

*/

191

void setRole(int role);

192

193

/**

194

* Get the role hint for this BeanDefinition.

195

* @return the role hint

196

*/

197

int getRole();

198

199

/**

200

* Set a human-readable description of this bean definition.

201

* @param description the description

202

*/

203

void setDescription(String description);

204

205

/**

206

* Return a human-readable description of this bean definition.

207

* @return the description, or null if none set

208

*/

209

String getDescription();

210

211

/**

212

* Return a resolvable type for this bean definition.

213

* @return a resolvable type (never null)

214

*/

215

ResolvableType getResolvableType();

216

217

/**

218

* Return whether this a Singleton, with a single, shared instance returned on all calls.

219

* @return whether this is a singleton

220

*/

221

boolean isSingleton();

222

223

/**

224

* Return whether this a Prototype, with an independent instance returned for each call.

225

* @return whether this is a prototype

226

*/

227

boolean isPrototype();

228

229

/**

230

* Return whether this bean is "abstract", that is, not meant to be instantiated.

231

* @return whether this bean is abstract

232

*/

233

boolean isAbstract();

234

}

235

```

236

237

### Bean Definition Registry

238

239

Central interface for registering bean definitions with the Spring container.

240

241

```java { .api }

242

/**

243

* Interface for registries that hold bean definitions, for example RootBeanDefinition and ChildBeanDefinition instances.

244

*/

245

interface BeanDefinitionRegistry extends AliasRegistry {

246

/**

247

* Register a new bean definition with this registry.

248

* @param beanName the name of the bean instance to register

249

* @param beanDefinition definition of the bean instance to register

250

* @throws BeanDefinitionStoreException if the BeanDefinition is invalid

251

* @throws BeanDefinitionOverrideException if there is already a BeanDefinition for the specified bean name and we are not allowed to override it

252

*/

253

void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) throws BeanDefinitionStoreException;

254

255

/**

256

* Remove the BeanDefinition for the given name.

257

* @param beanName the name of the bean instance to register

258

* @throws NoSuchBeanDefinitionException if there is no such bean definition

259

*/

260

void removeBeanDefinition(String beanName) throws NoSuchBeanDefinitionException;

261

262

/**

263

* Return the BeanDefinition for the given bean name.

264

* @param beanName name of the bean to find the definition for

265

* @return the BeanDefinition for the given name (never null)

266

* @throws NoSuchBeanDefinitionException if there is no such bean definition

267

*/

268

BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException;

269

270

/**

271

* Check if this registry contains a bean definition with the given name.

272

* @param beanName the name of the bean to look for

273

* @return if this registry contains a bean definition with the given name

274

*/

275

boolean containsBeanDefinition(String beanName);

276

277

/**

278

* Return the names of all beans defined in this registry.

279

* @return the names of all beans defined in this registry, or an empty array if none defined

280

*/

281

String[] getBeanDefinitionNames();

282

283

/**

284

* Return the number of beans defined in the registry.

285

* @return the number of beans defined in the registry

286

*/

287

int getBeanDefinitionCount();

288

289

/**

290

* Determine whether the given bean name is already in use within this registry.

291

* @param beanName the name to check

292

* @return whether the given bean name is already in use

293

*/

294

boolean isBeanNameInUse(String beanName);

295

}

296

```

297

298

### Root Bean Definition

299

300

Most commonly used concrete BeanDefinition class which supports property values, constructor arguments, and more.

301

302

```java { .api }

303

/**

304

* A root bean definition represents the merged bean definition at runtime of a specific bean in a Spring BeanFactory.

305

*/

306

class RootBeanDefinition extends AbstractBeanDefinition {

307

/**

308

* Create a new RootBeanDefinition for a singleton.

309

*/

310

public RootBeanDefinition();

311

312

/**

313

* Create a new RootBeanDefinition for a singleton.

314

* @param beanClass the class of the bean to instantiate

315

*/

316

public RootBeanDefinition(Class<?> beanClass);

317

318

/**

319

* Create a new RootBeanDefinition for a scoped bean.

320

* @param beanClass the class of the bean to instantiate

321

* @param scope the name of the corresponding scope

322

*/

323

public RootBeanDefinition(Class<?> beanClass, String scope);

324

325

/**

326

* Create a new RootBeanDefinition for a singleton, using an instance supplier.

327

* @param beanClass the class of the bean to instantiate (may be null)

328

* @param instanceSupplier the supplier to use for creating bean instances

329

*/

330

public <T> RootBeanDefinition(Class<T> beanClass, Supplier<T> instanceSupplier);

331

332

/**

333

* Create a new RootBeanDefinition for a scoped bean, using an instance supplier.

334

* @param beanClass the class of the bean to instantiate (may be null)

335

* @param scope the name of the corresponding scope

336

* @param instanceSupplier the supplier to use for creating bean instances

337

*/

338

public <T> RootBeanDefinition(Class<T> beanClass, String scope, Supplier<T> instanceSupplier);

339

340

/**

341

* Create a new RootBeanDefinition as deep copy of the given bean definition.

342

* @param original the original bean definition to copy from

343

*/

344

public RootBeanDefinition(RootBeanDefinition original);

345

346

/**

347

* Create a new RootBeanDefinition as deep copy of the given bean definition.

348

* @param original the original bean definition to copy from

349

*/

350

public RootBeanDefinition(BeanDefinition original);

351

352

/**

353

* Set a resolved Java Class for this bean definition.

354

* @param beanClass the resolved bean class (never null)

355

*/

356

public void setBeanClass(Class<?> beanClass);

357

358

/**

359

* Return the resolved type of this bean definition.

360

* @return the resolved type (never null)

361

* @throws IllegalStateException if the bean class is not resolvable

362

*/

363

public Class<?> getBeanClass() throws IllegalStateException;

364

365

/**

366

* Return whether this definition specifies a bean class.

367

* @return true if a bean class is specified

368

*/

369

public boolean hasBeanClass();

370

371

/**

372

* Determine the class of the wrapped bean, resolving it from a specified class name if necessary.

373

* @param classLoader the ClassLoader to use for resolving a (potential) class name

374

* @return the resolved bean class

375

* @throws ClassNotFoundException if the class name could be resolved

376

*/

377

public Class<?> resolveBeanClass(ClassLoader classLoader) throws ClassNotFoundException;

378

379

/**

380

* Set a callback for creating an instance of the bean, as an alternative to a declaratively specified factory method.

381

* @param instanceSupplier the supplier to use for creating bean instances (may be null)

382

*/

383

public void setInstanceSupplier(Supplier<?> instanceSupplier);

384

385

/**

386

* Return a callback for creating an instance of the bean, if any.

387

* @return the instance supplier, or null if none set

388

*/

389

public Supplier<?> getInstanceSupplier();

390

391

/**

392

* Specify a factory method name that refers to a non-overloaded method.

393

* @param factoryMethodName the factory method name, or null if none

394

*/

395

public void setUniqueFactoryMethodName(String factoryMethodName);

396

397

/**

398

* Return the resolved factory method as a Java Method object, if available.

399

* @return the factory method, or null if not found or not resolved yet

400

*/

401

public Method getResolvedFactoryMethod();

402

403

/**

404

* Set the resolved constructor to use for this bean definition.

405

* @param constructor the constructor to use (may be null)

406

*/

407

public void setResolvedConstructorOrFactoryMethod(Executable constructor);

408

409

/**

410

* Return the resolved constructor or factory method.

411

* @return the constructor or factory method, or null if not resolved yet

412

*/

413

public Executable getResolvedConstructorOrFactoryMethod();

414

415

/**

416

* Mark the specified constructor as resolved.

417

* @param constructorIndex the constructor index

418

*/

419

public void markConstructorArgumentsResolved();

420

421

/**

422

* Return whether the constructor arguments for this bean have been resolved.

423

* @return whether the constructor arguments have been resolved

424

*/

425

public boolean constructorArgumentsResolved();

426

427

/**

428

* Set pre-resolved constructor arguments.

429

* @param constructorArgumentsResolved the constructor arguments (may be null)

430

*/

431

public void setResolvedConstructorArguments(Object[] constructorArgumentsResolved);

432

433

/**

434

* Return the resolved constructor arguments.

435

* @return the resolved constructor arguments, or null if not resolved yet

436

*/

437

public Object[] getResolvedConstructorArguments();

438

439

/**

440

* Set "prepared" constructor arguments.

441

* @param preparedConstructorArguments the prepared constructor arguments (may be null)

442

*/

443

public void setPreparedConstructorArguments(Object[] preparedConstructorArguments);

444

445

/**

446

* Return the "prepared" constructor arguments.

447

* @return the prepared constructor arguments, or null if none

448

*/

449

public Object[] getPreparedConstructorArguments();

450

}

451

```

452

453

### Generic Bean Definition

454

455

Generic bean definition class for any kind of configuration purpose.

456

457

```java { .api }

458

/**

459

* GenericBeanDefinition is a one-stop shop for standard bean definition purposes.

460

*/

461

class GenericBeanDefinition extends AbstractBeanDefinition {

462

/**

463

* Create a new GenericBeanDefinition for the given bean class.

464

*/

465

public GenericBeanDefinition();

466

467

/**

468

* Create a new GenericBeanDefinition for the given bean class.

469

* @param beanClass the class of the bean to instantiate

470

*/

471

public GenericBeanDefinition(Class<?> beanClass);

472

473

/**

474

* Create a new GenericBeanDefinition as deep copy of the given bean definition.

475

* @param original the original bean definition to copy from

476

*/

477

public GenericBeanDefinition(BeanDefinition original);

478

479

/**

480

* Set the name of the parent definition of this bean definition, if any.

481

* @param parentName the parent name

482

*/

483

public void setParentName(String parentName);

484

485

/**

486

* Return the name of the parent definition of this bean definition, if any.

487

* @return the parent name, or null if none is set

488

*/

489

public String getParentName();

490

}

491

```

492

493

### Constructor Argument Values

494

495

Holder for constructor argument values for a bean.

496

497

```java { .api }

498

/**

499

* Holder for constructor argument values, typically as part of a bean definition.

500

*/

501

class ConstructorArgumentValues {

502

/**

503

* Create a new empty ConstructorArgumentValues object.

504

*/

505

public ConstructorArgumentValues();

506

507

/**

508

* Deep copy constructor.

509

* @param original the ConstructorArgumentValues to copy

510

*/

511

public ConstructorArgumentValues(ConstructorArgumentValues original);

512

513

/**

514

* Copy all given argument values into this object.

515

* @param other the ConstructorArgumentValues to copy from

516

*/

517

public void addArgumentValues(ConstructorArgumentValues other);

518

519

/**

520

* Add an argument value for the given index in the constructor argument list.

521

* @param index the index in the constructor argument list

522

* @param value the argument value

523

*/

524

public void addIndexedArgumentValue(int index, Object value);

525

526

/**

527

* Add an argument value for the given index in the constructor argument list.

528

* @param index the index in the constructor argument list

529

* @param value the argument value

530

* @param type the type of the constructor argument

531

*/

532

public void addIndexedArgumentValue(int index, Object value, String type);

533

534

/**

535

* Add an argument value for the given index in the constructor argument list.

536

* @param index the index in the constructor argument list

537

* @param newValue the argument value in the form of a ValueHolder

538

*/

539

public void addIndexedArgumentValue(int index, ValueHolder newValue);

540

541

/**

542

* Check whether an argument value has been registered for the given index.

543

* @param index the index in the constructor argument list

544

* @return whether an argument value has been registered

545

*/

546

public boolean hasIndexedArgumentValue(int index);

547

548

/**

549

* Get argument value for the given index in the constructor argument list.

550

* @param index the index in the constructor argument list

551

* @param requiredType the type to match (can be null)

552

* @return the ValueHolder for the argument, or null if none set

553

*/

554

public ValueHolder getIndexedArgumentValue(int index, Class<?> requiredType);

555

556

/**

557

* Return a Map of the indexed argument values held in this instance.

558

* @return an unmodifiable Map with Integer index as key and ValueHolder as value

559

*/

560

public Map<Integer, ValueHolder> getIndexedArgumentValues();

561

562

/**

563

* Add a generic argument value to be matched by type.

564

* @param value the argument value

565

*/

566

public void addGenericArgumentValue(Object value);

567

568

/**

569

* Add a generic argument value to be matched by type.

570

* @param value the argument value

571

* @param type the type of the constructor argument

572

*/

573

public void addGenericArgumentValue(Object value, String type);

574

575

/**

576

* Add a generic argument value to be matched by type or name (if available).

577

* @param newValue the argument value in the form of a ValueHolder

578

*/

579

public void addGenericArgumentValue(ValueHolder newValue);

580

581

/**

582

* Look for a generic argument value that matches the given type.

583

* @param requiredType the type to match

584

* @return the ValueHolder for the argument, or null if none set

585

*/

586

public ValueHolder getGenericArgumentValue(Class<?> requiredType);

587

588

/**

589

* Look for a generic argument value that matches the given type.

590

* @param requiredType the type to match (can be null)

591

* @param requiredName the name to match (can be null)

592

* @return the ValueHolder for the argument, or null if none set

593

*/

594

public ValueHolder getGenericArgumentValue(Class<?> requiredType, String requiredName);

595

596

/**

597

* Look for a generic argument value that matches the given type.

598

* @param requiredType the type to match (can be null)

599

* @param requiredName the name to match (can be null)

600

* @param usedValueHolders a Set of ValueHolder objects that have already been used in the current resolution process

601

* @return the ValueHolder for the argument, or null if none found

602

*/

603

public ValueHolder getGenericArgumentValue(Class<?> requiredType, String requiredName, Set<ValueHolder> usedValueHolders);

604

605

/**

606

* Return the list of generic argument values held in this instance.

607

* @return an unmodifiable List of ValueHolders

608

*/

609

public List<ValueHolder> getGenericArgumentValues();

610

611

/**

612

* Look for the next generic argument value that matches the given type, ignoring argument values that have already been used.

613

* @param requiredType the type to match (can be null)

614

* @param usedValueHolders a Set of ValueHolder objects that have already been used

615

* @return the ValueHolder for the argument, or null if none found

616

*/

617

public ValueHolder getArgumentValue(int index, Class<?> requiredType, String requiredName, Set<ValueHolder> usedValueHolders);

618

619

/**

620

* Return the number of argument values held in this instance.

621

* @return the number of argument values

622

*/

623

public int getArgumentCount();

624

625

/**

626

* Return if this holder does not contain any argument values, neither indexed ones nor generic ones.

627

* @return whether this holder does not contain any argument values

628

*/

629

public boolean isEmpty();

630

631

/**

632

* Clear this holder, removing all argument values.

633

*/

634

public void clear();

635

636

/**

637

* Holder for a constructor argument value, with an optional type attribute indicating the target type of the actual constructor argument.

638

*/

639

public static class ValueHolder implements BeanMetadataElement {

640

/**

641

* Create a new ValueHolder for the given value.

642

* @param value the argument value

643

*/

644

public ValueHolder(Object value);

645

646

/**

647

* Create a new ValueHolder for the given value and type.

648

* @param value the argument value

649

* @param type the type of the constructor argument

650

*/

651

public ValueHolder(Object value, String type);

652

653

/**

654

* Create a new ValueHolder for the given value, type and name.

655

* @param value the argument value

656

* @param type the type of the constructor argument

657

* @param name the name of the constructor argument

658

*/

659

public ValueHolder(Object value, String type, String name);

660

661

/**

662

* Set the value for the constructor argument.

663

* @param value the argument value

664

*/

665

public void setValue(Object value);

666

667

/**

668

* Return the value for the constructor argument.

669

* @return the argument value

670

*/

671

public Object getValue();

672

673

/**

674

* Set the type of the constructor argument.

675

* @param type the type of the constructor argument

676

*/

677

public void setType(String type);

678

679

/**

680

* Return the type of the constructor argument.

681

* @return the type of the constructor argument

682

*/

683

public String getType();

684

685

/**

686

* Set the name of the constructor argument.

687

* @param name the name of the constructor argument

688

*/

689

public void setName(String name);

690

691

/**

692

* Return the name of the constructor argument.

693

* @return the name of the constructor argument

694

*/

695

public String getName();

696

697

/**

698

* Return whether this holder contains a converted value already.

699

* @return whether this holder contains a converted value already

700

*/

701

public synchronized boolean isConverted();

702

703

/**

704

* Set the converted value of the constructor argument, after processed type conversion.

705

* @param value the converted value

706

*/

707

public synchronized void setConvertedValue(Object value);

708

709

/**

710

* Return the converted value of the constructor argument, after processed type conversion.

711

* @return the converted value

712

*/

713

public synchronized Object getConvertedValue();

714

}

715

}

716

```

717

718

**Usage Examples:**

719

720

```java

721

import org.springframework.beans.factory.support.BeanDefinitionRegistry;

722

import org.springframework.beans.factory.support.DefaultListableBeanFactory;

723

import org.springframework.beans.factory.support.RootBeanDefinition;

724

import org.springframework.beans.factory.support.GenericBeanDefinition;

725

import org.springframework.beans.factory.config.ConstructorArgumentValues;

726

import org.springframework.beans.MutablePropertyValues;

727

728

// Create bean factory and registry

729

DefaultListableBeanFactory registry = new DefaultListableBeanFactory();

730

731

// Create a simple bean definition

732

RootBeanDefinition beanDef = new RootBeanDefinition(MyService.class);

733

beanDef.setScope("singleton");

734

beanDef.setLazyInit(false);

735

736

// Set property values

737

MutablePropertyValues pvs = new MutablePropertyValues();

738

pvs.addPropertyValue("name", "My Service");

739

pvs.addPropertyValue("timeout", 30);

740

beanDef.setPropertyValues(pvs);

741

742

// Set constructor arguments

743

ConstructorArgumentValues args = new ConstructorArgumentValues();

744

args.addIndexedArgumentValue(0, "config.properties");

745

args.addGenericArgumentValue(true); // enable logging

746

beanDef.setConstructorArgumentValues(args);

747

748

// Register the bean definition

749

registry.registerBeanDefinition("myService", beanDef);

750

751

// Create generic bean definition with parent

752

GenericBeanDefinition childDef = new GenericBeanDefinition();

753

childDef.setParentName("myService");

754

childDef.setBeanClassName("com.example.ExtendedService");

755

childDef.setScope("prototype");

756

757

// Override some property values

758

MutablePropertyValues childPvs = new MutablePropertyValues();

759

childPvs.addPropertyValue("name", "Extended Service");

760

childDef.setPropertyValues(childPvs);

761

762

registry.registerBeanDefinition("extendedService", childDef);

763

764

// Query bean definitions

765

String[] beanNames = registry.getBeanDefinitionNames();

766

int count = registry.getBeanDefinitionCount();

767

boolean exists = registry.containsBeanDefinition("myService");

768

769

// Retrieve and modify bean definition

770

BeanDefinition retrievedDef = registry.getBeanDefinition("myService");

771

retrievedDef.setDescription("Service for handling business logic");

772

```

773

774

### Bean Definition Holder

775

776

Simple holder for a bean definition with a name and aliases.

777

778

```java { .api }

779

/**

780

* Holder for a BeanDefinition with name and aliases.

781

*/

782

class BeanDefinitionHolder implements BeanMetadataElement {

783

/**

784

* Create a new BeanDefinitionHolder.

785

* @param beanDefinition the BeanDefinition to wrap

786

* @param beanName the name of the bean, as specified for the bean definition

787

*/

788

public BeanDefinitionHolder(BeanDefinition beanDefinition, String beanName);

789

790

/**

791

* Create a new BeanDefinitionHolder.

792

* @param beanDefinition the BeanDefinition to wrap

793

* @param beanName the name of the bean, as specified for the bean definition

794

* @param aliases alias names for the bean, or null if none

795

*/

796

public BeanDefinitionHolder(BeanDefinition beanDefinition, String beanName, String[] aliases);

797

798

/**

799

* Copy constructor: Create a new BeanDefinitionHolder with the same contents as the given BeanDefinitionHolder instance.

800

* @param beanDefinitionHolder the BeanDefinitionHolder to copy

801

*/

802

public BeanDefinitionHolder(BeanDefinitionHolder beanDefinitionHolder);

803

804

/**

805

* Return the wrapped BeanDefinition.

806

* @return the wrapped BeanDefinition

807

*/

808

public BeanDefinition getBeanDefinition();

809

810

/**

811

* Return the primary name of the bean, as specified for the bean definition.

812

* @return the primary name of the bean

813

*/

814

public String getBeanName();

815

816

/**

817

* Return the alias names for the bean, as specified directly for the bean definition.

818

* @return the alias names, or null if none specified

819

*/

820

public String[] getAliases();

821

822

/**

823

* Expose the bean definition's source object.

824

* @return the source object (may be null)

825

*/

826

public Object getSource();

827

828

/**

829

* Determine whether the given candidate name matches the bean name or the aliases stored in this bean definition.

830

* @param candidateName the candidate name to check

831

* @return whether the candidate name matches

832

*/

833

public boolean matchesName(String candidateName);

834

835

/**

836

* Return a friendly, short description for the bean, stating name and aliases.

837

* @return the short description

838

*/

839

public String getShortDescription();

840

841

/**

842

* Return a long description for the bean, including name and aliases as well as a description of the contained BeanDefinition.

843

* @return the long description

844

*/

845

public String getLongDescription();

846

}

847

```

848

849

### Bean Definition Builder

850

851

Utility class that provides a fluent API for programmatically building bean definitions.

852

853

```java { .api }

854

/**

855

* Utility class for programmatically building bean definitions.

856

*/

857

class BeanDefinitionBuilder {

858

/**

859

* Create a new BeanDefinitionBuilder used to construct a GenericBeanDefinition.

860

* @param beanClass the class of the bean that the definition is being created for

861

* @return the BeanDefinitionBuilder

862

*/

863

public static BeanDefinitionBuilder genericBeanDefinition(Class<?> beanClass);

864

865

/**

866

* Create a new BeanDefinitionBuilder used to construct a GenericBeanDefinition.

867

* @param beanClassName the className for the bean that the definition is being created for

868

* @return the BeanDefinitionBuilder

869

*/

870

public static BeanDefinitionBuilder genericBeanDefinition(String beanClassName);

871

872

/**

873

* Create a new BeanDefinitionBuilder used to construct a RootBeanDefinition.

874

* @param beanClass the class of the bean that the definition is being created for

875

* @return the BeanDefinitionBuilder

876

*/

877

public static BeanDefinitionBuilder rootBeanDefinition(Class<?> beanClass);

878

879

/**

880

* Create a new BeanDefinitionBuilder used to construct a RootBeanDefinition.

881

* @param beanClassName the className for the bean that the definition is being created for

882

* @return the BeanDefinitionBuilder

883

*/

884

public static BeanDefinitionBuilder rootBeanDefinition(String beanClassName);

885

886

/**

887

* Create a new BeanDefinitionBuilder used to construct a ChildBeanDefinition.

888

* @param parentName the name of the parent bean

889

* @return the BeanDefinitionBuilder

890

*/

891

public static BeanDefinitionBuilder childBeanDefinition(String parentName);

892

893

/**

894

* Set the name of the parent definition of this bean definition.

895

* @param parentName the parent name

896

* @return the current BeanDefinitionBuilder instance (for method chaining)

897

*/

898

public BeanDefinitionBuilder setParentName(String parentName);

899

900

/**

901

* Set the factory method to use for creating this bean.

902

* @param factoryMethod the factory method name

903

* @return the current BeanDefinitionBuilder instance (for method chaining)

904

*/

905

public BeanDefinitionBuilder setFactoryMethod(String factoryMethod);

906

907

/**

908

* Set the factory bean to use for creating this bean.

909

* @param factoryBean the factory bean name

910

* @return the current BeanDefinitionBuilder instance (for method chaining)

911

*/

912

public BeanDefinitionBuilder setFactoryBean(String factoryBean);

913

914

/**

915

* Add a constructor argument value.

916

* @param value the constructor argument value

917

* @return the current BeanDefinitionBuilder instance (for method chaining)

918

*/

919

public BeanDefinitionBuilder addConstructorArgValue(Object value);

920

921

/**

922

* Add an indexed constructor argument value.

923

* @param index the index of the constructor argument

924

* @param value the constructor argument value

925

* @return the current BeanDefinitionBuilder instance (for method chaining)

926

*/

927

public BeanDefinitionBuilder addConstructorArgValue(int index, Object value);

928

929

/**

930

* Add a property value.

931

* @param name the property name

932

* @param value the property value

933

* @return the current BeanDefinitionBuilder instance (for method chaining)

934

*/

935

public BeanDefinitionBuilder addPropertyValue(String name, Object value);

936

937

/**

938

* Add a property reference.

939

* @param name the property name

940

* @param beanName the bean name to reference

941

* @return the current BeanDefinitionBuilder instance (for method chaining)

942

*/

943

public BeanDefinitionBuilder addPropertyReference(String name, String beanName);

944

945

/**

946

* Set the init method for this definition.

947

* @param methodName the name of the init method

948

* @return the current BeanDefinitionBuilder instance (for method chaining)

949

*/

950

public BeanDefinitionBuilder setInitMethodName(String methodName);

951

952

/**

953

* Set the destroy method for this definition.

954

* @param methodName the name of the destroy method

955

* @return the current BeanDefinitionBuilder instance (for method chaining)

956

*/

957

public BeanDefinitionBuilder setDestroyMethodName(String methodName);

958

959

/**

960

* Set the scope of this definition.

961

* @param scope the scope

962

* @return the current BeanDefinitionBuilder instance (for method chaining)

963

*/

964

public BeanDefinitionBuilder setScope(String scope);

965

966

/**

967

* Set whether this bean should be lazily initialized.

968

* @param lazy whether the bean should be lazily initialized

969

* @return the current BeanDefinitionBuilder instance (for method chaining)

970

*/

971

public BeanDefinitionBuilder setLazyInit(boolean lazy);

972

973

/**

974

* Set whether this bean is a primary candidate.

975

* @param primary whether this bean is primary

976

* @return the current BeanDefinitionBuilder instance (for method chaining)

977

*/

978

public BeanDefinitionBuilder setPrimary(boolean primary);

979

980

/**

981

* Set whether this bean is an autowire candidate.

982

* @param autowireCandidate whether this bean is an autowire candidate

983

* @return the current BeanDefinitionBuilder instance (for method chaining)

984

*/

985

public BeanDefinitionBuilder setAutowireCandidate(boolean autowireCandidate);

986

987

/**

988

* Return the current BeanDefinition object in its raw (unvalidated) form.

989

* @return the current BeanDefinition

990

*/

991

public AbstractBeanDefinition getRawBeanDefinition();

992

993

/**

994

* Validate and return the created BeanDefinition object.

995

* @return the BeanDefinition

996

*/

997

public AbstractBeanDefinition getBeanDefinition();

998

}

999

```

1000

1001

### Bean Definition Support Classes

1002

1003

Additional support classes for working with bean definitions.

1004

1005

```java { .api }

1006

/**

1007

* Abstract base class for bean definitions.

1008

*/

1009

abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccessor implements BeanDefinition, Cloneable {

1010

/** Constant for the default scope name. */

1011

public static final String SCOPE_DEFAULT = "";

1012

1013

/** Constant that indicates no autowiring at all. */

1014

public static final int AUTOWIRE_NO = AutowireCapableBeanFactory.AUTOWIRE_NO;

1015

1016

/** Constant that indicates autowiring bean properties by name. */

1017

public static final int AUTOWIRE_BY_NAME = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME;

1018

1019

/** Constant that indicates autowiring bean properties by type. */

1020

public static final int AUTOWIRE_BY_TYPE = AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE;

1021

1022

/** Constant that indicates autowiring the greediest constructor. */

1023

public static final int AUTOWIRE_CONSTRUCTOR = AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR;

1024

1025

/**

1026

* Set the autowire mode for this bean definition.

1027

* @param autowireMode the autowire mode to set

1028

*/

1029

public void setAutowireMode(int autowireMode);

1030

1031

/**

1032

* Return the autowire mode as specified in the bean definition.

1033

* @return the autowire mode

1034

*/

1035

public int getAutowireMode();

1036

1037

/**

1038

* Set the dependency check code.

1039

* @param dependencyCheck the dependency check code

1040

*/

1041

public void setDependencyCheck(int dependencyCheck);

1042

1043

/**

1044

* Return the dependency check code.

1045

* @return the dependency check code

1046

*/

1047

public int getDependencyCheck();

1048

1049

/**

1050

* Override this bean definition from the given bean definition.

1051

* @param other the other bean definition

1052

*/

1053

public void overrideFrom(BeanDefinition other);

1054

1055

/**

1056

* Apply the provided default values to this bean.

1057

* @param defaults the default settings to apply

1058

*/

1059

public void applyDefaults(BeanDefinitionDefaults defaults);

1060

1061

/**

1062

* Return whether this definition specifies a bean class.

1063

* @return whether a bean class is specified

1064

*/

1065

public boolean hasBeanClass();

1066

1067

/**

1068

* Determine the class of the wrapped bean.

1069

* @param classLoader the ClassLoader to use for resolving a (potential) class name

1070

* @return the resolved bean class

1071

* @throws ClassNotFoundException if the class name could be resolved

1072

*/

1073

public Class<?> resolveBeanClass(ClassLoader classLoader) throws ClassNotFoundException;

1074

1075

/**

1076

* Validate this bean definition.

1077

* @throws BeanDefinitionValidationException in case of validation failure

1078

*/

1079

public void validate() throws BeanDefinitionValidationException;

1080

1081

/**

1082

* Return a deep copy of this bean definition.

1083

* @return the cloned bean definition object

1084

*/

1085

public abstract AbstractBeanDefinition cloneBeanDefinition();

1086

}

1087

1088

/**

1089

* Bean definition for beans that can inherit settings from their parent.

1090

*/

1091

class ChildBeanDefinition extends AbstractBeanDefinition {

1092

/**

1093

* Create a new ChildBeanDefinition for the given parent.

1094

* @param parentName the name of the parent bean

1095

*/

1096

public ChildBeanDefinition(String parentName);

1097

1098

/**

1099

* Create a new ChildBeanDefinition for the given parent, providing constructor arguments and property values.

1100

* @param parentName the name of the parent bean

1101

* @param cargs the constructor argument values to apply

1102

* @param pvs the property values to apply

1103

*/

1104

public ChildBeanDefinition(String parentName, ConstructorArgumentValues cargs, MutablePropertyValues pvs);

1105

1106

/**

1107

* Create a new ChildBeanDefinition for the given parent.

1108

* @param parentName the name of the parent bean

1109

* @param beanClass the class of the bean to instantiate

1110

*/

1111

public ChildBeanDefinition(String parentName, Class<?> beanClass);

1112

1113

/**

1114

* Create a new ChildBeanDefinition for the given parent.

1115

* @param parentName the name of the parent bean

1116

* @param beanClassName the name of the class to instantiate

1117

*/

1118

public ChildBeanDefinition(String parentName, String beanClassName);

1119

1120

/**

1121

* Create a new ChildBeanDefinition as deep copy of the given bean definition.

1122

* @param original the original bean definition to copy from

1123

*/

1124

public ChildBeanDefinition(ChildBeanDefinition original);

1125

}

1126

1127

/**

1128

* Bean definition defaults that can be applied to beans in a given context.

1129

*/

1130

class BeanDefinitionDefaults {

1131

/**

1132

* Return whether beans are lazy-init by default.

1133

* @return whether beans are lazy-init by default

1134

*/

1135

public boolean isLazyInit();

1136

1137

/**

1138

* Set whether beans should be lazy-init by default.

1139

* @param lazyInit whether beans should be lazy-init by default

1140

*/

1141

public void setLazyInit(boolean lazyInit);

1142

1143

/**

1144

* Return the autowire mode.

1145

* @return the autowire mode

1146

*/

1147

public int getAutowireMode();

1148

1149

/**

1150

* Set the autowire mode.

1151

* @param autowireMode the autowire mode

1152

*/

1153

public void setAutowireMode(int autowireMode);

1154

1155

/**

1156

* Return the dependency check setting.

1157

* @return the dependency check setting

1158

*/

1159

public int getDependencyCheck();

1160

1161

/**

1162

* Set the dependency check setting.

1163

* @param dependencyCheck the dependency check setting

1164

*/

1165

public void setDependencyCheck(int dependencyCheck);

1166

1167

/**

1168

* Return the name of the default init method.

1169

* @return the name of the default init method

1170

*/

1171

public String getInitMethodName();

1172

1173

/**

1174

* Set the name of the default init method.

1175

* @param initMethodName the name of the default init method

1176

*/

1177

public void setInitMethodName(String initMethodName);

1178

1179

/**

1180

* Return the name of the default destroy method.

1181

* @return the name of the default destroy method

1182

*/

1183

public String getDestroyMethodName();

1184

1185

/**

1186

* Set the name of the default destroy method.

1187

* @param destroyMethodName the name of the default destroy method

1188

*/

1189

public void setDestroyMethodName(String destroyMethodName);

1190

}

1191

```

1192

1193

### Bean Definition Validation and Exceptions

1194

1195

Exception classes for bean definition validation and processing errors.

1196

1197

```java { .api }

1198

/**

1199

* Exception thrown when validation of a bean definition fails.

1200

*/

1201

class BeanDefinitionValidationException extends BeansException {

1202

/**

1203

* Create a new BeanDefinitionValidationException with the specified message.

1204

* @param msg the detail message

1205

*/

1206

public BeanDefinitionValidationException(String msg);

1207

1208

/**

1209

* Create a new BeanDefinitionValidationException with the specified message and root cause.

1210

* @param msg the detail message

1211

* @param cause the root cause

1212

*/

1213

public BeanDefinitionValidationException(String msg, Throwable cause);

1214

}

1215

1216

/**

1217

* Exception thrown when a BeanDefinition could not be parsed.

1218

*/

1219

class BeanDefinitionParsingException extends BeansException {

1220

/**

1221

* Create a new BeanDefinitionParsingException.

1222

* @param problem the configuration problem that was detected during the parsing process

1223

*/

1224

public BeanDefinitionParsingException(Problem problem);

1225

1226

/**

1227

* Return an array of the Problems that were encountered during the parsing process.

1228

* @return the problems that were encountered during the parsing process

1229

*/

1230

public Problem[] getProblems();

1231

}

1232

1233

/**

1234

* Exception thrown when an attempt is made to override a bean definition inappropriately.

1235

*/

1236

class BeanDefinitionOverrideException extends BeanDefinitionStoreException {

1237

/**

1238

* Create a new BeanDefinitionOverrideException for the given new and existing definition.

1239

* @param beanName the name of the bean being overridden

1240

* @param beanDefinition the newly registered bean definition

1241

* @param existingDefinition the existing bean definition for the same name

1242

*/

1243

public BeanDefinitionOverrideException(String beanName, BeanDefinition beanDefinition, BeanDefinition existingDefinition);

1244

1245

/**

1246

* Return the description of the resource that the bean definition came from.

1247

* @return the description of the resource

1248

*/

1249

public String getResourceDescription();

1250

1251

/**

1252

* Return the name of the bean.

1253

* @return the name of the bean

1254

*/

1255

public String getBeanName();

1256

1257

/**

1258

* Return the newly registered bean definition.

1259

* @return the newly registered bean definition

1260

*/

1261

public BeanDefinition getBeanDefinition();

1262

1263

/**

1264

* Return the existing bean definition for the same name.

1265

* @return the existing bean definition

1266

*/

1267

public BeanDefinition getExistingDefinition();

1268

}

1269

```

1270