ApplicationContext
doesBeanFactory
interface is defined in org.springframework.beans.factory.BeanFactory
while the ApplicationContext
interface is defined in org.springframework.context.ApplicationContext
ApplicationContext
supports internationalization, BeanFactory
doesn’tBeanFactory
uses syntax for providing a resource object. Contrarily, ApplicationContext
creates as well as manages resource objects on its ownApplicationContext
makes use of eager or aggressive initialization. On the other hand, BeanFactory
uses lazy initializationorg.springframework.beans.factory.xml.XmlBeanFactory
, which loads its beans based on the definitions contained in an XML file. This container reads the configuration metadata from an XML file and uses it to create a fully configured system or application. META-INF/spring.components
” rather than classpath scanning.<? xml version ="1.0" encoding ="UTF-8"?>
<beansxmlns="HTTP://www.Springframework.org/schema/beans"xmlns:xsi="HTTP://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="HTTP://www.Springframework.org/schema/beans
HTTP://www.Springframework.org/schema/beans/Spring-beans.xsd">
<beanid="helloWorld"class="com.demoprogram.HelloWorld">
<propertyname="info"value="Hello World!"/>
</bean>
</beans>
<bean>
<bean id="FreeTimeLearning" class="org.freetimelearn.firstSpring.FreeTimeLearning">
<property name="name" value="freetimelearn"></property>
</bean>​
<beans>
<context:annotation-config/>
<!-- bean definitions go here -->
</beans>​
@Configuration
annotated classes and @Bean
annotated methods.@Bean
annotation has the same role as the <bean/>
element.@Bean
methods in the same class. setBeanName()
which take the bean ID and the corresponding bean has to implement BeanNameAware
interface.setBeanFactory()
by passing an instance of itself (if BeanFactoryAware interface is implemented in the bean).BeanPostProcessors
is associated with a bean, then the preProcessBeforeInitialization()
methods are invoked.postProcessAfterInitialization()
methods will be called if there are any BeanPostProcessors associated with the bean that needs to be run post creation.<constructor-arg>
and <property>
settings that override autowiring.@Scope
annotation or “scope” attribute in XML configuration files. Note that there are five supported scopes :@EnableWebSecurity
.@Autowired
annotation on those beans :@Autowired
ServletContext servletContext;
@Autowired
ServletConfig servletConfig;
RequestMapping
annotation is used to map web requests to Spring Controller methods. In addition to simple use cases, we can use it for mapping of HTTP headers, binding parts of the URI with @PathVariable
, and working with URI parameters and the @RequestParam
annotation. setBeanName()
method.setBeanFactory()
method.postProcesserBeforeInitialization()
method.afterPropertySet()
method is called. If the bean has init method declaration, the specified initialization method is called.postProcessAfterInitialization()
methods will be called.destroy()
methodbean/>
element inside the <property/>
or <constructor-arg/>
elements defines a so-called inner bean. An inner bean definition does not require a defined id or name; the container ignores these values. It also ignores the scope flag. Inner beans are always anonymous and they are always scoped as prototypes.<bean id = "outerBean" class = "...">
<property name = "target">
<bean id = "innerBean" class = "..."/>
</property>
</bean>​
BeanInitializationException
if the affected bean property has not been populated. @Autowired
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. @Qualifier
annotation along with @Autowired
to remove the confusion by specifying which exact bean will be wired. JSR-250
based annotations which include @PostConstruct
, @PreDestroy
and @Resource
annotations.@PostConstruct :
This annotation can be used as an alternate of initialization callback.@PreDestroy :
This annotation can be used as an alternate of destruction callback.@Resource :
This annotation can be used on fields or setter methods. The @Resource
annotation takes a 'name' attribute which will be interpreted as the bean name to be injected. You can say, it follows by-name autowiring semantics.<bean id="emailService"
class="com.freetimelearn.app.EmailService">
</bean>
<bean id="customer"
class="com.freetimelearn.app.Customer">
<constructor-arg ref="emailService">
</bean>
EmailService theEmailService = new EmailService();
Customer theCustomer = new Customer(theEmailService);
com.freetimelearn.app
, Spring will recursively scan for components starting from the base package through all directories until the required component is found.com.freetimelearn.app.demo
is a sub package because of the naming structure, just like folders on a file system..xml file
. The spring container uses this file to control the lifecycle of spring beans. A bean is configured as:<bean id = "..." class = "..." init-method = "..." lazy-init="true" destroy-method="....">
<!-- bean dependencies and configurations -->
</bean>
JoinPoint
is a point during the execution of a program and represents the method execution. It includes the execution of a method or even handling of an exception. @Autowired
and @Qualifier
annotationsDispatcherServlet
is the front controller in the Spring MVC application and it loads the spring bean configuration file and initializes all the beans that are configured. If annotations are enabled, it also scans the packages and configures any bean annotated with @Component
, @Controller
, @Repository
, or @Service annotation
s.ContextLoaderListener
is the listener to start up and shut down Spring’s root WebApplicationContext
. Its important functions are to tie up the lifecycle of ApplicationContext to the lifecycle of the ServletContext and to automate the creation of ApplicationContext. We can use it to define shared beans that can be used across different spring contexts.<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
home
”, view resolver will use view page located at /WEB-INF/views/home.jsp
.MultipartResolver
interface is used for uploading files : CommonsMultipartResolver
and StandardServletMultipartResolver
are two implementations provided by spring framework for file uploading. By default there are no multipart resolvers configured but to use them for uploading files, all we need to define a bean named “multipartResolver
” with type as MultipartResolver
in spring bean configurations.MultipartResolver
and pass on a wrapped HttpServletRequest.
Then it’s used in the controller class to get the file and process it.Spring DAO
support allows for easier access to data technologies like JDO, Hibernate and JDBC. This support module also provides more consistent access to these technologies without having to worry about catching technology-specific exceptions.” AspectJ's
pointcut expression language. It works in such a way that if certain conditions are met Spring AOP system will apply the given advice code. execution(modifiers-pattern? return-type-pattern declaring-type-pattern?
method-name-pattern(param-pattern) throws-pattern?)
@Before("execution(public void com.freetimelearn.app.CustomerDAO.addCustomer())")
addCustomer
method in the CustomerDAO
class, and if those items match, then we will apply our advice for that Pointcut, and that is all for matching on execution calls for a given method name.param ()
is a parameter pattern which matches on a method with no arguments.@Before("execution(* addCustomer())")​
param (*)
is another pattern that matches a method with one argument of any type.@Before("execution( * addCustomer(com.FreeTimeLearn.app.model.Customer))")
param (..)
which matches on a method with zero to many arguments of any type.@Before("execution(* addCustomer(..))")
Spring 5
is not compatible with the older versions of JAVA. It requires at least JAVA
8 for attaining compatibility. Spring 5
could integrate with the Jdk9
Modularity. This can be stated as follows :package com.hello;
public class HelloWorld {
public String sayHello(){
return "HelloWorld";
}
}
module com.hello {
export com.hello;
}
module com.hello.client {
requires com.hello;
}
public class HelloWorldClient {
public static void main(String[] args){
HelloWorld helloWorld = new HelloWorld();
log.info(helloWorld.sayHello());
}
}
<props>
tag as below.<bean id="adminUser" class="com.freetimelearn.common.Customer">
<!-- java.util.Properties -->
<property name="emails">
<props>
<prop key="admin">admin@nospam.com</prop>
<prop key="support">support@nospam.com</prop>
</props>
</property>
</bean>
<util:properties id="emails" location="classpath:com/foo/emails.properties" />
WebClient
is a component in the new Web Reactive framework that can act as a reactive client for performing non-blocking HTTP requests. Since it's reactive client, it can handle reactive streams with back pressure, and it can take full advantage of Java 8 lambdas. It can also handle both sync and async scenarios.WebTestClient
is a similar class that we can use in tests. Basically, it's a thin shell around the WebClient
. It can connect to any server over an HTTP connection. It can also bind directly to WebFlux applications using mock request and response objects, without the need for an HTTP server.