Spring Framework internals:

Beans and Initialization
Sidlyarevich Vlad
1
Father of Spring - Rod Johnson
• “Expert One-on-One J2EE Design
Development” Author
• Spring Source co-founder
• Nature lover
• PhD of Musicology
2
Version history
• The first version of the Spring framework was written by Rod Johnson along with a book in 2002. 

• The framework was first released in June 2003 under the Apache license version 2.0. 

• The first milestone release of Spring framework (1.0) was released in March 2004. 

• Spring 2.0, which came in 2006, simplified the XML config files.

• Spring 2.5, which came in 2007, introduced annotation configurations.

• Spring 3.2, which came in 2012, introduced Java configuration, had support for Java 7, Hibernate 4, Servlet 3.0, and also
required a minimum of Java 1.5. 

• Spring 4.0, which came in 2014, had support for Java 8. 

• Spring Boot also was introduced in 2014.

• Spring 5.0 came out in 2017. Spring Boot 2.x has support for Spring 5.
3
XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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">
<bean class="xml.init.services.ConsoleHelloService" scope="prototype">
<property name="message" value="dear"/>
<property name="nameService" ref="hardcodedNameService"/>
</bean>
<bean class="xml.init.services.HardcodedNameService" name="hardcodedNameService"
init-method="init"/>
</beans>
4
Annotation-based
@Service
@Scope(SCOPE_PROTOTYPE)
public class HardcodedNameService implements NameService {
@Override
public String getName() {
return "Jack";
}
@PostConstruct
public void init() {
}
}
5
Java configuration
@Configuration
public class MyConfiguration {
@Bean
public HelloService helloService() {
return new ConsoleHelloService();
}
}
6
BeanDefinition
• Java class full name (with packages)
• The unique identifier (if exists)
• Configs (init method, bean scope etc)
• Constructor args
• Setter args
7
BeanFactoryPostProcessor (BFPP)
• Transformations to groups of BeanDefinitions, before
any objects will be created
• Useful implementations are provided by framework
• Contains single method:
void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
throws BeansException;
8
FactoryBean
•Old way, used before Java Config
•BeanFactory can deal with it
public interface FactoryBean<T> {
T getObject() throws Exception;
Class<?> getObjectType();
boolean isSingleton();
}
9
Proxy pattern
10
BeanPostProcessor (BPP)
• Spring-managed too
• Can configure created beans
• Two useful methods:
Object postProcessBeforeInitialization(Object bean, String beanName) throws
BeansException;
Object postProcessAfterInitialization(Object bean, String beanName) throws
BeansException;
11
Multi-staged constructor
• Java constructor
• @PostConstruct (or init)
• On ContextRefreshedEvent
12
BeanPostProcessor (BPP)
• CommonAnnotationBeanPostProcessor - JSR-250
support (@PostConstruct etc)
• RequiredAnnotationBeanPostProcessor
• AutowiredAnnotationBeanPostProcessor
• ConfigurationClassPostProcessor
• PersistenceExceptionTranslationPostProcessor
13
Q & A
14
Thanks for attention!
15

Spring internals

  • 1.
    Spring Framework internals:
 Beansand Initialization Sidlyarevich Vlad 1
  • 2.
    Father of Spring- Rod Johnson • “Expert One-on-One J2EE Design Development” Author • Spring Source co-founder • Nature lover • PhD of Musicology 2
  • 3.
    Version history • Thefirst version of the Spring framework was written by Rod Johnson along with a book in 2002. 
 • The framework was first released in June 2003 under the Apache license version 2.0. 
 • The first milestone release of Spring framework (1.0) was released in March 2004. 
 • Spring 2.0, which came in 2006, simplified the XML config files.
 • Spring 2.5, which came in 2007, introduced annotation configurations.
 • Spring 3.2, which came in 2012, introduced Java configuration, had support for Java 7, Hibernate 4, Servlet 3.0, and also required a minimum of Java 1.5. 
 • Spring 4.0, which came in 2014, had support for Java 8. 
 • Spring Boot also was introduced in 2014.
 • Spring 5.0 came out in 2017. Spring Boot 2.x has support for Spring 5. 3
  • 4.
    XML <?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"> <bean class="xml.init.services.ConsoleHelloService" scope="prototype"> <property name="message" value="dear"/> <property name="nameService" ref="hardcodedNameService"/> </bean> <bean class="xml.init.services.HardcodedNameService" name="hardcodedNameService" init-method="init"/> </beans> 4
  • 5.
    Annotation-based @Service @Scope(SCOPE_PROTOTYPE) public class HardcodedNameServiceimplements NameService { @Override public String getName() { return "Jack"; } @PostConstruct public void init() { } } 5
  • 6.
    Java configuration @Configuration public classMyConfiguration { @Bean public HelloService helloService() { return new ConsoleHelloService(); } } 6
  • 7.
    BeanDefinition • Java classfull name (with packages) • The unique identifier (if exists) • Configs (init method, bean scope etc) • Constructor args • Setter args 7
  • 8.
    BeanFactoryPostProcessor (BFPP) • Transformationsto groups of BeanDefinitions, before any objects will be created • Useful implementations are provided by framework • Contains single method: void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException; 8
  • 9.
    FactoryBean •Old way, usedbefore Java Config •BeanFactory can deal with it public interface FactoryBean<T> { T getObject() throws Exception; Class<?> getObjectType(); boolean isSingleton(); } 9
  • 10.
  • 11.
    BeanPostProcessor (BPP) • Spring-managedtoo • Can configure created beans • Two useful methods: Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException; Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException; 11
  • 12.
    Multi-staged constructor • Javaconstructor • @PostConstruct (or init) • On ContextRefreshedEvent 12
  • 13.
    BeanPostProcessor (BPP) • CommonAnnotationBeanPostProcessor- JSR-250 support (@PostConstruct etc) • RequiredAnnotationBeanPostProcessor • AutowiredAnnotationBeanPostProcessor • ConfigurationClassPostProcessor • PersistenceExceptionTranslationPostProcessor 13
  • 14.
  • 15.