Table of Contents
Spring Core Interview Questions
1. How many modules are there in Spring Framework and what are they?
For more information about each module visit :
https://onlyfullstack.blogspot.com/2019/06/spring-framework-modules.html
2. What are the Advantages of Spring Framework
Lightweight:
Spring is lightweight when it comes to size and transparency. The basic version of spring framework is around 1MB. And the processing overhead is also very negligible.
Inversion of control (IOC):
Loose coupling is achieved in spring using the technique Inversion of Control. The objects give their dependencies instead of creating or looking for dependent objects.
Aspect oriented (AOP):
Spring supports Aspect oriented programming and enables cohesive development by separating application business logic from system services.
Container:
Spring contains and manages the life cycle and configuration of application objects.
MVC Framework:
Spring comes with MVC web application framework, built on core Spring functionality. This framework is highly configurable via strategy interfaces, and accommodates multiple view technologies like JSP, Velocity, Tiles, iText, and POI. But other frameworks can be easily used instead of Spring MVC Framework.
JDBC Exception Handling:
The JDBC abstraction layer of the Spring offers a meaningful exception hierarchy, which simplifies the error handling strategy. Integration with Hibernate, JDO, and iBATIS: Spring provides best Integration services with Hibernate, JDO and iBATIS.
Transaction Management:
Spring framework provides a generic abstraction layer for transaction management. This allowing the developer to add the pluggable transaction managers, and making it easy to demarcate transactions without dealing with low-level issues. Spring’s transaction support is not tied to J2EE environments and it can be also used in container less environments.
3. In how many ways you can use Spring Framework?
Spring Framework can be used in four ways. They are listed as follows:
1. As a Full-fledged Spring web application.
2. As a third-party web framework, using Spring Frameworks middle-tier.
3. For remote usage
4. As Enterprise Java Bean which can wrap existing POJOs (Plain Old Java Objects).
Please follow below url to get more information about each module:
https://onlyfullstack.blogspot.com/2019/06/spring-framework-usage.html
Spring Core Annotations – Spring Interview Questions
4. What is the difference in between @Resource, @Autowired and @Inject?
ANNOTATION
|
PACKAGE
|
SOURCE
|
@Resource
|
javax.annotation
|
Java
|
@Inject
|
javax.inject
|
Java
|
@Qualifier
|
javax.inject
|
Java
|
@Autowired
|
org.springframework.bean.factory
|
Spring
|
@Autowired and @Inject injects the dependancy based on below priority
Matches by Type
Restricts by Qualifiers
Matches by Name
@Resource injects the dependancy based on below priority
Matches by Name
Matches by Type
Restricts by Qualifiers (ignoredif match is found by name)
Spring recommends to use Autowire as it supports in constructor.
For more details please refer : https://onlyfullstack.blogspot.com/2019/06/difference-between-resource-autowired-inject-spring-core.html
5. How to inject the right dependancy when more than one dependancy found in the spring application?
1. Using @Qualifier with @Autowired
You can specify the dependancy name with Qualifier and Spring will pick up for you.
@Autowired @Qualifier("student1") private Student student;
2. Name with @Resource
Specifying a name in @Resource. You can provide to @Resource a name attribute having value smtpMailSender:
@Resource(name="smtpMailSender") private MailSender mailSender;
3. Using @Primary
The @Primary annotation can be used to tell Spring to choose a bean when multiple beans become eligible for an injection. Suppose we have 3 implementation of the MailSender and we can annotate SmtpMailSender with @Primary. So now Spring will always take SmtpMailSender as the default dependancy whenever the MailSender is @Autowired, as below:
@Primary @Component("mailSender") public class SmtpMailSender implements MailSender { }
6. What is the difference between @Component and @Bean?When should you use @Bean?
@Component Preferable for component scanning and automatic wiring.
Sometimes automatic configuration is not an option. When? Let’s imagine that you want to wire components from 3rd-party libraries (you don’t have the source code so you can’t annotate its classes with @Component), so automatic configuration is not possible.
The @Bean annotation returns an object that spring should register as bean in application context. The body of the method bears the logic responsible for creating the instance.
7. What is the use of @Configuration?
If we want to use the mail sender class which is a part of some third party JAR, we’ll not be able to annotate those with @Component, because we’ll not have access to their source code. so @Configuration class is used which defines method which return these objects with the help of @Bean annotation.
The @Configuration annotation tells Spring that it’s a configuration class. When the application context is built, the methods that are annotated with @Bean inside such configuration classes are called, and their return values are stored as beans. The name of the methods become the names of the beans by default.
The @Bean annotation works not only in the classes annotated with @Configuration, but in any component class. For example, in MailConfig.java, replace @Configuration with @Component, and the application will still work. But, if you see the log, now you ‘ll notice that the DemoBean is being created twice. That’s because Spring caches the return values of only those bean creation methods that are inside classes annotated with @Configuration.
Although @Bean methods can be placed in any class annotated with @Component or any of its specialization, @Configuration classes are the best place for those. So, avoid placing @Bean methods in classes other than those annotated with @Configuration.
8. What is the use of @Import and @ImportResource? When to use them in Spring Application?
If the configuration class of the bean is defined in other package then we can import that bean for autowiring with @Import. Here I have create MailConfigCopy which defines the importMockMail object that we are using in the MailConfig class.
@Configuration @Import(MailConfigCopy.class) public class MailConfig { @Bean public SmtpMailService smtpMailService(MockMailService importMockMail) { System.out.println("inside import annotaion test"); return new SmtpMailService(); } }
We can use @ImportResource to import the xml configuration.
@ImportResource(classpath:sample.xml)
9. How to configure the environment specific classes? How to use @Profiles in Spring Boot?
Each environment requires a setting that is specific to them. For example, in DEV, we do not need to constantly check database consistency. Whereas in TEST and STAGE, we need to. These environments host specific configurations called Profiles.
We need to create three application.properties, each property file have different properties based on the environment.
application-dev.properties application-test.properties application-prod.properties
Lets create DBConfiguration objects with @Profile
@Profile("dev") @Bean public String devDatabaseConnection() { System.out.println("DB connection for DEV - H2"); System.out.println(driverClassName); System.out.println(url); return "DB connection for DEV - H2"; } @Profile("test") @Bean public String testDatabaseConnection() { System.out.println("DB Connection to RDS_TEST - Low Cost Instance"); System.out.println(driverClassName); System.out.println(url); return "DB Connection to RDS_TEST - Low Cost Instance"; } @Profile("prod") @Bean public String prodDatabaseConnection() { System.out.println("DB Connection to RDS_PROD - High Performance Instance"); System.out.println(driverClassName); System.out.println(url); return "DB Connection to RDS_PROD - High Performance Instance"; }
A. How to implement Profiles in Spring Boot?
B. How to Activate Profile?
10. How to read the values from properties file?
We can get the dynamic values from properties file with the help of @Value annotation and with Environment object.
With @Value annotation – that can be used on @Component or a Bean to assign some value to it. We can use it with the help of annotation as below, always put values in below format.
@Value(“${property name}”)
Example:
@Configuration @ComponentScan(basePackages = "com.websystique.spring") @PropertySource(value = { "classpath:application.properties" }) public class AppConfig { /* * PropertySourcesPlaceHolderConfigurer Bean only required for @Value("{}") annotations. * Remove this bean if you are not using @Value annotations for injecting properties. */ @Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } }