XAb8AaA4QNiG AGgOBDY7A 2 2 4 Spring-Framework Annotations
XAb8AaA4QNiG AGgOBDY7A 2 2 4 Spring-Framework Annotations
Annotations
DI-Related Annotations
@Primary @Scope
@Autowired annotation Constructor injection:
class Car {
Engine engine;
@Autowired annotation can be used to mark a
dependency which Spring is going to resolve @Autowired
and inject. This annotation can be used with a Car(Engine engine) {
this.engine = engine;
constructor, setter, or field injection. }
@Autowired has a boolean argument called }
@Autowired
@Qualifier("bike")
void setVehicle(Vehicle vehicle) {
this.vehicle = vehicle;
}
@Required annotation @Bean annotation
The @Required annotation applies to bean @Bean marks a factory method which instantiates a
property setter methods and it indicates Spring bean:
that the affected bean property must be @Bean
populated in XML configuration file at Engine engine() {
return new Engine();
configuration time. }
Otherwise, the container throws a Spring calls these methods when a new instance of
BeanInitializationException exception. the return type is required.
The resulting bean has the same name as the factory
@Required
void setColor(String color) { method. To name it differently the name or the value
this.color = color; arguments of this annotation can be used (the
} argument value is an alias for the argument name)
@Bean("engine")
<bean class="com.test.annotations.Bike">
Engine getEngine() {
<property name="color" value="green" />
return new Engine();
</bean> }