Skip to main content

Posts

Showing posts with the label Spring Core

Spring Core - PropertyPlaceHolderConfigurer example

In this post, we will externalize the properties used in the application in a property file and will use PropertyPlaceHolderConfigurer to resolve the placeholder at application startup time. Java Configuration for PropertyPlaceHolderConfigurer @Configuration public class AppConfig { @Bean public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer(); propertySourcesPlaceholderConfigurer.setLocations(new ClassPathResource("application-db.properties")); //propertySourcesPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(true); //propertySourcesPlaceholderConfigurer.setIgnoreResourceNotFound(true); return propertySourcesPlaceholderConfigurer; } } We created object of PropertySourcesPlaceholderConfigurer and set the Locations to search. In this example we used ClassPathResource to resolve the properti

Spring Core - Bean scopes (Prototype vs Singleton)

In this post, we will discuss about various bean scopes and their differences. Bean scopes There are seven bean scopes Spring supports out of which five are only available if your ApplicationContext is web-aware. # Scope Explanation 1 singleton There will be single object of the bean per Spring IoC Container (Default). 2 prototype Scope beans to any number of object instances. Every time you get object of prototype bean from context, it will be brand new. 3 request Scope of the bean definition mapped to the lifecycle of HTTP Request. This is only available web-aware ApplicationContext . 4 session Scope of the bean definition mapped to the lifecycle of HTTP session. This is only available to web-aware ApplicationContext . 5 globalSession Scope of the bean definition mapped to the lifecycle of HTTP session usually used within Portlet context. This is only available to web-aware ApplicationContext . 6 application Scope of the bean definition mapp

Spring Core - Dependency Injection

What is Dependency Injection? Dependency injection is a process in which objects define their dependencies i.e. other objects they require to work, through a constructor, setter methods, factory methods. The container responsibility is to inject those while creating beans. With Dependency inject in place, we have cleaner code and clear way of decoupling. There are two prominent variants of Dependency Injection. Constructor based Dependency Injection Setter based Dependency Injection Constructor based Dependency Injection When you express your dependencies through constructor arguments and your container invoke your constructor with number of arguments, type of arguments expected by the constructor. Let's jump to one quick example. @Component public class ConstructorBasedFileParser { private Parser parser; @Autowired public ConstructorBasedFileParser(Parser parser) { this.parser = parser; } public void setParser(Parser parser) { this.parser = parser;

Spring Core - @Import annotation

In this post, we will learn about @Import annotation and its usage. You can see my previous post on how to create a simple spring core project. What is @Import annotation and usage? @Import annotation is equivalent to <import/> element in Spring XML configuration. It helps in splitting the single Java based configuration file into small, modular, maintainable and component based configuration. Let's see it with example. @Configuration @Import(value = { DBConfig.class, WelcomeGbConfig.class }) public class HelloGbAppConfig { } In above code snippet, we are importing two different configuration files viz. DBConfig , WelcomeGbConfig in application level configuration file HelloGbAppConfig . The above code is equivalent to Spring XML based configuration below. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/bean

Spring Core - Java configuration example

In this post, we will create a spring context and will register bean via Java configuration file. You can see my previous post on how to create a simple spring core project. What is @Configuration annotation? @Configuration annotation indicates that there is one or more bean methods and spring containers can process to generate bean definitions at runtime. Also, @Bean annotation is used at method level to signifies that this will be registered as bean in spring context. Let's create a quick configuration class. @Configuration public class WelcomeGbConfig { @Bean GreetingService greetingService() { return new GreetingService(); } } Now, we will create spring context as follows. // using try with resources so that this context closes automatically try (ConfigurableApplicationContext context = new AnnotationConfigApplicationContext( WelcomeGbConfig.class);) { GreetingService greetingService = context.getBean(GreetingService.class); greetingService.gre

Spring Core - A quick start

In this post, we will create a Spring context and will get a bean object from it. What is Spring context? Spring context is also termed as Spring IoC container which is responsible for instantiate, configure and assemble the beans by reading configuration meta data from XML, Java annotations and/ or Java code in configuration files. Technologies used Spring 4.3.6.RELEASE, Maven Compiler 3.6.0 and Java 1.8 We will first create a simple maven project. You can select the maven-archtype-quickstart as archtype. Adding dependencies in pom.xml We will add spring-framework-bom in the dependency management. <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-framework-bom</artifactId> <version>4.3.6.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </depe