Skip to content

Latest commit

 

History

History
261 lines (220 loc) · 21.1 KB

spring.md

File metadata and controls

261 lines (220 loc) · 21.1 KB

Spring

How make spring service thread-safe?

There are several ways, probably too long to list here but here are a few examples:

  • Design your beans immutable: for example have no setters and only use constructor arguments to create a bean. There are other ways, such as Builder pattern, etc..
  • Design your beans stateless: for example a bean that does something can be just a function (or several). This bean in most cases can and should be stateless, which means it does not have any state, it only does things with function arguments you provide each time (on each invocation)
  • Design your beans persistent: which is a special case of "immutable", but has some very nice properties. Usually is used in functional programming, where Spring (at least yet) not as useful as in imperative world, but I have used them with Scala/Spring projects.
  • Design your beans with locks [last resort]: I would recommend against this unless you are working on a lower level library. The reason is we (humans) are not good thinking in terms of locks. Just the way we are raised and nurtured. Everything happens in parallel without us needing to "put that rain on pause, let me get an umbrella". Computers however are all about locks when you are talking "multiple things at the same time", hence there are some of us (exceptional people) who are doing their fair share and implementing libraries based on these locks. Most of other humans can just use these libraries and worry not about concurrency.
Relative links:

What is bean?

The objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. These beans are created with the configuration metadata that you supply to the container.

Relative links:

How bean gets into the container?

The IoC container receives metadata from either an XML file, Java annotations, or Java code. The container gets its instructions on what objects to instantiate, configure, and assemble from simple Plain Old Java Objects (POJO) by reading the configuration metadata provided. These created objects through this process called Spring Beans.

The responsibilities of IoC container are:

  • Instantiating the bean
  • Wiring the beans together
  • Configuring the beans
  • Managing the bean’s entire life-cycle
Relative links:

What are the possible bean scopes?

  • singleton. Scopes a single bean definition to a single object instance per Spring IoC container.
  • prototype. Scopes a single bean definition to any number of object instances.
  • request. Scopes a single bean definition to the lifecycle of a single HTTP request; that is each and every HTTP request will have its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.
  • session. Scopes a single bean definition to the lifecycle of a HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
  • global session. Scopes a single bean definition to the lifecycle of a global HTTP Session. Typically only valid when used in a portlet context. Only valid in the context of a web-aware Spring ApplicationContext.
Relative links:

What is the difference @Service between @Component?

Apart from the fact that it's used to indicate, that it's holding the business logic, there’s nothing else noticeable in this annotation; but who knows, Spring may add some additional exceptional in future.

Relative links:

What-is-the-difference-between-service-and-bean

@Bean annotation is method-level and is used in configuration classes to fine tune special bean and add it to IoC container. @Service annotation is class-level and is used to show Spring that this class has to be managed by IoC container.

How to call a method after bean initialization?

  • afterPropertiesSet method;
  • annotation @PostConstruct;
  • init-method attribute;
  • BeanPostProcessor;
Relative links:

What is the default scope?

singleton

Relative links:

What is the prototype scope?

Scopes a single bean definition to any number of object instances.

Relative links:

What are the possible ways of Dependency Injection?

  • Constructor-Based Dependency Injection;
  • Setter-Based Dependency Injection;
  • Field-Based Dependency Injection;
Relative links:

Where better to use dependency injection via constructor? Where via setter?

  • required dependencies via constructor;
  • optional dependencies via setter;
Relative links:

How to catch the exceptions for controllers?

ExceptionHandler is a Spring annotation that provides a mechanism to treat exceptions that are thrown during execution of handlers (Controller operations). This annotation, if used on methods of controller classes, will serve as the entry point for handling exceptions thrown within this controller only. Altogether, the most common way is to use @ExceptionHandler on methods of @ControllerAdvice classes so that the exception handling will be applied globally or to a subset of controllers.

Relative links:

What is the difference between BeanFactory and FactoryBean?

  • The BeanFactory is an interface in Spring and is a factory class for managing beans. The BeanFactory is the core of the IOC container. The accusations include: instantiating, configuring objects in the application and establishing dependencies between them. The ApplicationContext is an advanced version of the BeanFactory.
  • FactoryBean is a way to customize the instantiation of the bean, through the implementation of the FactoryBean, complete the custom bean instantiation details. For example, the FactoryBean proxy object can be used to intercept all its methods to form AOP-like functions. Using FactoryBean avoids the need to configure various properties in the XML file, which is more flexible, but you need to encode the instantiation of the class in the implementation class of the FactoryBean.
Relative links:

What is the difference Spring and Spring Boot?

  • Spring Boot does all of those using AutoConfiguration and will take care of all the internal dependencies that your application needs — all you need to do is run your application. Spring Boot will auto-configure with the Dispatcher Servlet, if Spring jar is in the class path. It will auto-configue to the datasource, if Hibernate jar is in the class path. Spring Boot gives us a pre-configured set of Starter Projects to be added as a dependency in our project.
  • During web-application development, we would need the jars that we want to use, which versions of the jars to use, and how to connect them together. All web applications have similar needs, for example, Spring MVC, Jackson Databind, Hibernate core, and Log4j (for logging). So, we had to choose the compatible versions of all these jars. In order to decrease the complexity, Spring Boot has introduced what we call Spring Boot Starters.
Relative links:

How to add own auto-configurations?

  • You need to opt-in to auto-configuration by adding the @EnableAutoConfiguration or @SpringBootApplication annotations to one of your @Configuration classes.
  • Spring Boot checks for the presence of a META-INF/spring.factories file within your published jar. The file should list your configuration classes under the EnableAutoConfiguration key, as shown in the following example:
Relative links:

Where can be stored the list of auto-configurations in META-INF?

META-INF/spring.factories

Relative links:

What are the possible ways of configurations in Spring?

  • xml-based configuration, when you describe configuration in xml file;
  • java-based configuration, when configuration is Java class, marked with specific annotations;
  • groovy-based configuration, when configuration is file with Groovy code;
Relative links:

What is the lookup method?

The getter method will return us the reference that is been set but suppose you want a new instance of the dependent bean each time you invoke the getter method

Relative links:

What is declarative transaction in Spring?

  1. Allows us to manage transactions through configuration.
  2. This means separating transaction logic with business logic.
  3. We use annotations (Or XML files) to manage transactions.
  4. Easy to maintain. Boilerplate is kept away from business logic.
  5. Preferred when working with large amount of Transaction logic.
Relative links:

What creates a proxy?

After the bean instances are created they are run through a series of BeanPostProcessors - Based on the AOP advice, the proxies are created by a BeanPostProcessor called AnnotationAwareAspectJAutoProxyCreator (or possibly its subclasses)

Relative links:

Does @transactional method work in the case of execution in the same class?

The problem here is, that Spring's AOP proxies don't extend but rather wrap your service instance to intercept calls. This has the effect, that any call to "this" from within your service instance is directly invoked on that instance and cannot be intercepted by the wrapping proxy (the proxy is not even aware of any such call).

Relative links:

What is environment?

Interface representing the environment in which the current application is running. Models two key aspects of the application environment: profiles and properties. Methods related to property access are exposed via the PropertyResolver superinterface.

A profile is a named, logical group of bean definitions to be registered with the container only if the given profile is active. Beans may be assigned to a profile whether defined in XML or via annotations; see the spring-beans 3.1 schema or the @Profile annotation for syntax details. The role of the Environment object with relation to profiles is in determining which profiles (if any) are currently active, and which profiles (if any) should be active by default.

Properties play an important role in almost all applications, and may originate from a variety of sources: properties files, JVM system properties, system environment variables, JNDI, servlet context parameters, ad-hoc Properties objects, Maps, and so on. The role of the environment object with relation to properties is to provide the user with a convenient service interface for configuring property sources and resolving properties from them.

Relative links:

How does auto-configuration work?

Under the hood, auto-configuration is implemented with standard @Configuration classes. Additional @Conditional annotations are used to constrain when the auto-configuration should apply. Usually, auto-configuration classes use @ConditionalOnClass and @ConditionalOnMissingBean annotations. This ensures that auto-configuration applies only when relevant classes are found and when you have not declared your own @Configuration.

Relative links:

What is the difference between @Resource and @Autowired annotations?

  • @Resource means get me a known resource by name. The name is extracted from the name of the annotated setter or field, or it is taken from the name-Parameter.
  • @Inject or @Autowired try to wire in a suitable other component by type.
Relative links:

What is the feature in Spring Boot for setting up the dependencies?

Spring Boot Starters is one of the major key features or components of Spring Boot Framework. The main responsibility of Spring Boot Starter is to combine a group of common or related dependencies into single dependencies.

Relative links:

Where does Spring Boot application begin?

Spring Boot application begins from dependency to Spring Boot Starter.

Relative links:

What is the difference between @Controller and @RestController annotations?

The job of @Controller is to create a Map of model object and find a view but @RestController simply return the object and object data is directly written into HTTP response as JSON or XML.

This can also be done with traditional @Controller and use @ResponseBody annotation but since this is the default behavior of RESTful Web services, Spring introduced @RestController which combined the behavior of @Controller and @ResponseBody together.

Relative links:

Does @Qualifier annotation can work only with bean name from @Component annotation?

@Qualifier annotation can be used on classes instead of specifying bean name in @Component annotation.

Relative links:

Is it required to put @Repository annotation in the case of extending JpaRepository?

It is indeed not necessary to put the @Repository annotation on interfaces that extend JpaRepository; Spring recognises the repositories by the fact that they extend one of the predefined Repository interfaces.

Relative links:

How ResponseEntity can be used?

ResponseEntity is meant to represent the entire HTTP response. You can control anything that goes into it: status code, headers, and body.

Relative links:

Does entity from @Transactional method can be updated without executing save method?

Yes, because hibernate will automatically detect changes made to persistent entities and update the database accordingly.

Relative links:

Is it possible to have only one transaction in the case of executing several @Transactional methods?

Yes, it depends on the propagation attribute. PROPAGATION_REQUIRED - the same transaction for both methods, PROPAGATION_REQUIRES_NEW starts a new transaction.

Relative links:

Anti-patterns of bean validation?

  • Anti-Pattern #1: Validating Only in the Persistence Layer;
  • Anti-Pattern #2: Validating with a Shotgun;
  • Anti-Pattern #3: Using Validation Groups for Use Case Validations;
Relative links:

Is it good practice to define interface for Spring bean?

Relative links:

How does @Transactional annotation work?

Relative links:

How to use @NamedEntityGraph with Spring Data JPA?

Relative links:

Home Page