Spring Core Annotation
Spring Core Annotation
@Autowired @Qualifier @Required @ComponentScan @Configuration @ComponentScan @Bean @Value
@Autowired
This annotation in applied on fields, setter methods, and constructors.When, we pass the values and fiends using the property name. Spring will automatically assign the fields with the passed values. We can use @Autowired on private properties.
@Qualifier
This annotation is used along with @Autowired annotation. When we need more control of the dependency injection process, @Qualifier can be used. @Qualifier can be specified on individual constructor arguments or method parameters. This annotation is used to avoid confusion which occurs when we create more than one bean of the same type and want to wire only one of them with a property.
@Autowired
@Qualifier("heartObjectValue")
public void setHeart(Heart heart) {
this.heart = heart;
@Required
This annotation is applied on bean setter methods. Consider a scenario where you need to enforce a required property. The @Required annotation indicates that the affected bean must be populated at configuration time with the required property. Otherwise an exception of type BeanInitializationException is thrown.
@Required
@Value("${Student.hobby}")
public void setHobby(String hobby) {
this.hobby = hobby;
}
@Component
The @Component annotation marks a java class as a bean so the component-scanning mechanism of spring can pick it up and pull it into the application context. To use this annotation, apply it over class as below:
import org.springframework.stereotype.Component;
@Component("collegeBean")
public class College {
public void test()
{
System.out.println("college class method tested");
}
}
@Configuration
Using for XML configuration file – it is configuration using Java class. Java class annotated with @Configuration is a configuration by itself and will have methods to instantiate and configure the dependencies.
@ComponentScan
This annotation is used with @Configuration annotation to allow Spring to know the packages to scan for annotated components. @ComponentScan is also used to specify base packages using basePackage attributes to scan.
package com.upgrad.removexml;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration // this is config class
@ComponentScan(basePackages ="com.upgrad.removexml")
public class CollegeConfig {
}
@Bean
This annotation is used at the method level. @Bean annotation works with @Configuration to create Spring beans. As mentioned earlier, @Configuration will have methods to instantiate and configure dependencies. Such methods will be annotated with @Bean. The method annotated with this annotation works as bean ID and it creates and returns the actual bean.
package com.upgrad.removexml.withbean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration // this is config class
public class CollegeConfig {
@Bean
public Principal principalBean() {
return new Principal();
}
@Bean(name="col")
public College collegeBean() {// method name is our bean id- collegeBean
College college=new College(principalBean());
return college;
}
}
@Value
This annotation can be used for injecting values into fields in Spring-managed beans and it can be applied at the field or constructor/method parameter level.
@Value("${Student.intrestedCourse}")
public void setIntrestedCourse(String intrestedCourse) {
this.intrestedCourse = intrestedCourse;
}
Great dude, Most used ones are listed here
ReplyDelete