Showing posts with label Spring. Show all posts
Showing posts with label Spring. Show all posts

Friday, June 8, 2018

Spring Interview Questions for 3 Years Experienced

In this post we will learn frequently asked Spring Interview Questions and answers for Experienced Developers. They are

1. What are the types of Dependency Injection Spring supports?

Setter Injection:

Ans: Setter-based DI is realized by calling setter methods on the user’s beans after invoking a no-argument constructor or no-argument static factory method to instantiate their bean.

Constructor Injection:

Constructor-based DI is realized by invoking a constructor with a number of arguments, each representing a collaborator.

2.What bean scopes does Spring support? Explain them.
Ans: The Spring Framework supports following five scopes, three of which are available only if the users use a web-aware Application Context.

Singleton: This scopes the bean definition to a single instance per Spring IoC container.

Prototype: This scopes a single bean definition to have any number of object instances.

Request: This scopes a bean definition to an HTTP request. Only valid in the context of a web-aware Spring ApplicationContext

Session: This scopes a bean definition to an HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.

Global-session: This scopes a bean definition to a global HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.

3.What are the benefits of Spring Framework?

Ans:

  1. Extensive usage of Components
  2. Reusability
  3. Decoupling
  4. Reduces coding effort by using pattern implementations such as singleton, factory, service locator etc.
  5. Removal of leaking connections
  6. Declarative transaction management
  7. Easy to integrate with third party tools and technologies.

4. Why Spring framework is needed?

Ans: Spring framework is needed because it is –

  1. Very Light Weight Container
  2. Framework
  3. IOC
  4. AOP

5.What are the methods of bean life cycle?

Ans: There are two important methods of Bean life cycle:
Setup – called when bean is loaded into container
Teardown – called when bean is unloaded into container

6. what is Bean Factory, have you used XMLBeanFactory?
Ans: BeanFactory is factory Pattern which is based on IOC design principles.it is used to make a clear separation between application configuration and dependency from actual code. The XmlBeanFactory is one of the implementations of Bean Factory which we have used in our project. The org.springframework.beans.factory.xml.XmlBeanFactory is used to create bean instance defined in our XML file.
BeanFactory factory = new XmlBeanFactory(new FileInputStream("beans.xml"));
Or
ClassPathResource resorce = new ClassPathResource("beans.xml");
XmlBeanFactory factory = new XmlBeanFactory(resorce);


7.What is the difference between singleton and prototype bean?

Ans: Basically, a bean has scopes which define their existence on the application

Singleton: means single bean definition to a single object instance per Spring IOC container.
Prototype: means a single bean definition to any number of object instances.
Whatever beans we defined in spring framework are singleton beans. There is an attribute in bean tag named ‘singleton’ if specified true then bean becomes singleton and if set to false then the bean becomes a prototype bean. By default, it is set to true. So, all the beans in spring framework are by default singleton beans.


8.What are the benefits of IoC.

Ans: It will minimize the amount of code in your application.
It will make your application easy to test because it doesn’t require any singletons or JNDI lookup mechanisms in your unit test cases.
It promotes loose coupling with minimal effort and least intrusive mechanism.
It supports eager instantiation and lazy loading of the services.

9.What do you understand by auto wiring?

Ans: The Spring container is able to autowire relationships between the collaborating beans. That is, it is possible to let Spring resolve collaborators for your bean automatically by inspecting the contents of the BeanFactory.

10. What are the limitations with auto wiring?

Ans: Following are some of the limitations you might face with auto wiring:

Overriding possibility: You can always specify dependencies using <constructor-arg> and <property> settings which will override autowiring.
 Primitive data type: Simple properties such as primitives, Strings and Classes can’t be autowired.
Confusing nature: Always prefer using explicit wiring because autowiring is less precise.

11.Describe Spring DAO support?

Ans: The Data Access Object (DAO) support in Spring makes it easy to work with data access technologies like JDBC, Hibernate or JDO in a consistent way. This allows one to switch between the persistence technologies easily. It also allows you to code without worrying about catching exceptions that are specific to each of these technology

12.What are the different types of AutoProxying?

Ans: Following are the different types of AutoProxying:

BeanNameAutoProxyCreator
DefaultAdvisorAutoProxyCreator
Metadata autoproxying

13.How can beans be made singleton or prototype?

Ans: The bean tag has an attribute called ‘singleton’. The bean is singleton if its value is ‘TRUE’, otherwise the bean is a prototype.

14. What is ‘Throws advice’ in Spring?

Ans: ‘Throws Advice’ define the behavior when an exception occurs. It is an interface and it has no methods which need to be implemented.

A class that implements this interface should have method with this signature:

Void samplethrow (Throw table t)
Void samplethrow(Method m, Object[] o, Object target, Throw tablet)

15.How is Hibernate accessed using the Spring framework?

Ans: Hibernate can be accessed in the following two ways:

 By IOC with a Callback and HibernateTemplate.
 By applying an AOP Interceptor and broadening the HibernateDaoSupport.

16.Explain the advantages of using DAO module?

Ans: The database code can be kept clean and simple by using the DAO module. This helps in preventing problems that rise because of poor handling of closures of database resources. Also, the DAO module utilizes the AOP module to enable objects in the Spring application to use transaction management services.

Tuesday, October 17, 2017

Spring Security Interview Questions and Answers

In this post we will learn frequently asked Spring Security Interview Questions. Since every programmer must aware to give security for their software while they developing projects. Spring Security gives highly powerful and customizable authentication and access-control framework. It provides Authentication and Authorization mechanism. Now we will see some important Spring Security Interview Questions

1)What is Spring Security?

Spring security is a project under spring framework umbrella, which provides support for security requirements of enterprise Java projects. Spring Security formerly known as aegis security provides out of box support for creating login screen, remember me cookie support, securing URL, authentication provider to authenticate user from database, LDAP and in memory, concurrent active session management support and many more. In order to use Spring security in a Spring MVC based project, you need to include spring-security.jar and configure it in application-Context-security.xml file, you can name it whatever you want, but make sure to supply this to ContextLoaderListener, which is responsible for creating Spring context and initializing dispatcher servlet.


2) Why do you need method security? What type of object is typically secured at the method level?

Ans: Spring Security uses AOP for security at the method level

  • annotations based on Spring annotations or JSR-250 annotations
  • Java configuration to activate detection of annotations
  • It typically secure your services
  • Do not access repositories directly, bypasses security (and transactions) 
3) Will Spring Security secures all the Applications?

Ans: No, in web applications, we need some more things to secure full application to save from hackers.


4) How is Security mechanism implemented using Spring?
Ans:  Spring Security is a powerful and highly customizable authentication and access-control framework. It is the de-facto standard for securing Spring-based applications. Spring Security is a framework that focuses on providing both authentication and authorization to Java applications. Like all Spring projects, the real power of Spring Security is found in how easily it can be extended to meet custom requirements. 

Spring makes use of the DelegatingFilterProxy for implementing security mechanisms. It is a Proxy for standard Servlet Filter, delegating to a Spring-managed bean that implements the Filter interface. Its the starting point in the springSecurityFilterChain which instantiates the Spring Security filters according to the Spring configuration
Some of the features of Spring Security are: 
  1. Comprehensive and extensible support for both Authentication and Authorization
  2. Protection against attacks like session fixation, clickjacking, cross site request forgery, etc
  3. Servlet API integration Optional integration with Spring Web MVC

5)How to enable Spring Security in Web Application?

You can enable the Spring security by adding the Filter org.springframework.web.filter.DelegatingFilterProxy in your application’s web.xml.
6)What is the delegating filter proxy?
Spring’s DelegatingFilterProxy provides the link between web.xml and the application context. In Spring Security, the filter classes are also Spring beans defined in the application context and thus able to take advantage of Spring’s rich dependency-injection facilities and lifecycle interfaces.

Example:
filter>
<filter-name>myFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>myFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

7)What is the security filter chain? 
In Spring Security you have a lot of filters for web application and these filters are Spring Beans. Each Spring security filter bean that require in your application you have to declare in your application context file and as we know that filters would be applied to application only when they would be declared on web.xml. Now DelegatingFilterProxy comes into picture for delegating the request to fillter which declared into application context file by adding a corresponding DelegatingFilterProxy entry to web.xml for each filter and we have to make sure about ordered, it should be define correctly, but this would be cumbersome and would clutter up the web.xml file quickly if you have a lot of filters. FilterChainProxy lets us add a single entry to web.xml and deal entirely with the application context file for managing our web security beans.

Example:

<bean id="filterChainProxy" class="org.springframework.security.web.FilterChainProxy">
<constructor-arg>
 <list>
 <sec:filter-chain pattern="/restful/**" filters="
  securityContextPersistenceFilterWithASCFalse,
  basicAuthenticationFilter,
  exceptionTranslationFilter,
  filterSecurityInterceptor" />
 <sec:filter-chain pattern="/**" filters="
  securityContextPersistenceFilterWithASCTrue,
  formLoginFilter,
  exceptionTranslationFilter,
  filterSecurityInterceptor" />
 </list>
</constructor-arg>
</bean>

8)From the applications perspective, how many user roles needed in spring security?
Answer:
Three user roles are there in spring:

Supervisors
Tellers
Plain Users

9) How to configure Spring Security with jdbc configuration?
Ans: 
 @Autowired
private DataSource dataSource;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) 
  throws Exception 
{
auth.jdbcAuthentication().dataSource(dataSource)
      .withDefaultSchema()
      .withUser("user").password("password").roles("USER")
      .and()
      .withUser("admin").password("password").roles("USER", "ADMIN");

10)What is authentication and authorization? Which must come first?

  • Authentication – Establishing that a principal’s credentials are valid
  • Authorization – Deciding if a principal is allowed to perform an action
Authentication comes first before Authorization because authorization process needs principal object with authority votes to decide user allow to perform a action for secured resource.

11)Does Spring Security support password hashing? What is salting?

Ans: Yes, Spring Security provides support for password hashing. The salt is used to prevent dictionary attacks against the key in the event your encrypted data is compromised.

12)What's the difference between @Secured and @PreAuthorize in spring security?
Ans:  if you wanted to do something like access the method only if the user has Role1 and Role2 the you would have to use @PreAuthorize @PreAuthorize("hasRole('ROLE_role1') and hasRole('ROLE_role2')") Using @Secured({"role1", "role2"}) is treated as an OR 

That is all about Spring Security Interview Questions subscribe me for latest updates.



Wednesday, March 22, 2017

Spring Interview Questions for Experienced

In this post i am sharing spring interview questions for experienced candidates.In my previous post i have shared spring interview questions for freshers,so first read basic spring interview questions and then come to this post.

1) What is JDBC abstraction and DAO module?

Ans: To keep database code clean and simple we use JDBC abstraction and DAO module and also prevent problems that result from a failure to close database resources. It also makes use of Spring's AOP module to provide transaction management services for objects in a Spring application.

2) How do you provide Configuration metadata to the Spring Container?

Ans: There are three important methods to provide configuration metadata to the Spring Container. They are

  1. XML based configuration file
  2. Annotation based Configuration
  3. Java-based configuration
3) Define Auto Wiring?

Ans: AutoWiring is used to build relationship between the collaborating beans. Spring container can automatically resolve collaborators for beans. There is @Autowired annotation provides more fine-grained control over where and how autowiring should be accomplished. This annotation can be used to autowire bean on the setter method just like @Required annotation,constructor,a property or methods with arbitrary names and/or multiple arguments.

4)What are the advantages of Spring AOP?

Ans: The advantages of Spring AOP are:

  • It increases the modularity and the key unit is Aspect
  • AOP breaks the program logic into distinct parts
  • It provides the pluggable way to dynamically add the additional concern before,after or around the actual logic.
5) What is Autoproxying?

Ans: Autoproxying is used to create proxy automatically for the spring users. It provides following two classes to support this automatic proxy creation. There are different types of AutoProxying. They are

  • BeanNameAutoProxyCreator
  • DefaultAdvisorAutoProxyCreator
  • MetadataAutoProxying

6) What is DAO in Spring?

Ans: DAO is used to provide integration of java database connectivity and Object relational mapping objects. DAO is spring framework provides connection for JDBC,hibernate,JDO,JPA,Common client interface and Oracle.The database code can be kept clean and simple by using the DAO module.Also,DAO module utilizes the AOP module to enable objects in the Spring application to use transaction management services.


7) What are the ways to access Hibernate by using Spring?

Ans:  There are two ways to access Hibernate with Spring: They are

  • Inversion of Control with a Hibernate Template and Callback
  • Extending HibernateDAOSupport and Applying an AOP interceptor node.
8) How can we integrate Spring and Hibernate using HibernateDaoSupport?

Ans: Using Spring's SessionFactory called LocalSessionFactory. The integration process is 3 steps. They are

  • Configure the Hibernate SessionFactory
  • Extend a DAO implementation from HibernateDaoSupport
  • Wire in Transaction Support with AOP
9) What are the benefits of the Spring Framework's transaction management?

Ans: There are mainly 3 types of benefits. They are

  • It integrates very will with spring's various data access abstractions.
  • It provides consistent programming model across different transaction APIs such as JDBC,JPA,Hibernate and JDO.
  • It supports declarative transaction management
10) How many types of Transaction Management Spring support?

Ans: Spring support two types of Transaction Management. They are
Programmatic transaction management :
This means that you have managed the transaction with the help of programming. That gives you extreme flexibility,but it is difficult to maintain

Declarative transaction management:

This means you separate transaction management from the business code. You only use annotations or XML based configuration to manage the transactions.

11) Is Singleton beans are thread-safe in Spring framework?

Ans: NO, singleton beans are not thread-safe in Spring framework.

12) Can you inject null and empty string values in Spring?

Ans: YES, you can

13) What is web module?

Ans: The Spring web module is built on the application context module,providing a context that is appropriate for web-based applications. This module also contains support for several web-oriented tasks such as transparently handling multipart requests for file uploads and programmatic binding of request parameters to your business objects. It also contains integration support with Jakarta Struts.

14) What is Cross cutting concern and concern in Spring AOP?

Ans: Cross cutting concern: It is a concern which is applicable throughout the application and it affects the entire application. Example: security,logging and data transfer are the concerns which are needed in almost every module of an application

Concern: It is a behavior that we want to have in a module of an application. Issues in which we are interested defines our concern

15) What is difference between singleton and prototype bean?

Ans: 
Single bean definition to a single object instance per Spring IoC container.
single bean definition to any number of object instance per Spring IoC container.

Watch Video for more details: Spring Interview Questions for experienced

See Also:

Spring MVC Architecture
Basic Spring Interview Questions
Spring IoC container with examples
Hibernate Interview Questions


High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...