Overview
- Spring projects
- Spring documentation (quick access to API and reference documentation for all Spring projects)
Spring Framework
- http://www.springsource.org/spring-framework
- Desc. : provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform.
- License :
- Sources : https://github.com/spring-projects/spring-framework
- Maven artifacts
References
- Spring Framework 5.0 Reference Documentation
- Spring Framework 5.0 API
- Spring Framework 4.3 Reference Documentation
- Spring Framework 4.3 API
- Spring Framework 4.2 Reference Documentation
- Spring Framework 4.2 API
- Spring Framework 4.1 Reference Documentation
- Spring Framework 4.1 API
- Spring Framework 4.0 Reference Documentation
- Spring Framework 4.0 API
- Spring Framework 3.2 Reference Documentation
- Spring Framework 3.2 API
- Spring Framework 3.1 Reference Documentation
- Spring Framework 3.1 API
- Spring Framework 3.0 Reference Documentation
- Spring Framework 3.0 API
General
- Inversion of Control Containers and the Dependency Injection pattern(Martin Fowler, 23 January 2004)
- Announcing Spring Framework 4.0 GA Release
- Setter injection versus constructor injection and the use of @Required(JULY 11, 2007)
- As of Spring 3.1, the Spring team generally advocates setter injection. because ...
- As of Spring 4.0, the Spring team generally advocates constructor injection as it enables ...
Notes
- Annotation injection is performed before XML injection, thus the latter configuration will override the former for properties wired through both approaches.
- The
@Required
annotation simply indicates that the affected bean property must be populated at configuration time, through an explicit property value in a bean definition or through autowiring. The container throws an exception if the affected bean property has not been populated; this allows for eager and explicit failure, avoiding NullPointerExceptions or the like later on. It is still recommended that you put assertions into the bean class itself, for example, into an init method. Doing so enforces those required references and values even when you use the class outside of a container.
Resource
- Resource strings
org.springframework.util.ResourceUtils
API- Wildcards in application context constructor resource paths
classpath*:
,classpath:com/mycompany/**/applicationContext.xml
, ...
Container
ConfigurableApplicationContext
- Prefer
ConfigurableApplicationContext
toApplicationContext
- Pay attention to
registerShutdownHook()
method.
- Prefer
GenericXmlApplicationContext
Benas
MethodInvokingFactoryBean
- returns a value which is the result of a static or instance method invocation.
MethodInvokingBean
- just invoking a target method, not expecting a result to expose to the container.
<bean id="registeringIdentificationListener" class="org.springframework.beans.factory.config.MethodInvokingBean"
depends-on="listenableDetectionService, defaultDetectionListener">
<property name="targetObject" ref="listenableDetectionService"/>
<property name="targetMethod" value="addListener"/>
<property name="arguments" ref="defaultDetectionListener"/>
</bean>
- Wiring collections
<list/>, <set/>,
, and<props/>
- Declaring an array of objects in a Spring bean context(Aug 31 '10)
- Using MethodInvokingFactoryBean to set up unconventional beans(Mar 23 '11)
Configuration
- Properties with Spring(October 15, 2013)
- Example: the Class name substitution PropertyPlaceholderConfigurer
- Using Spring and Commons Configuration (Jul 11th, 2008)
- Nested Property Placeholders in Spring Configuration (February 25, 2008)
- How to programmatically resolve property placeholder in Spring(Mar 2 '11)
- Bean definition profiles
- Not loading a Spring bean when a certain profile is set (Aug 1 '14)
- Encrypting application configuration files using Jasypt
- API
Annotations
- Spring Annotation Programming Model (8 Jun 2016)
@Required
@Autowired
@Resource
@Inject
and@Named
- Spring annotations vs. Standard annotations
@Autowired, @Component, @Component, @Inject, @Required, ...
- Spring Injection with @Resource, @Autowired and @Inject (2011)
- Combining lifecycle mechanisms using
@PostConstruct
and@PreDestroy
javax.annotation
package of Java SE 6 (contains@PostConstruct
,@PreDestroy
and@Resource
)
- Packages
Purpose | Package | Annotations |
---|---|---|
Denoting the roles of types or methods in the overall architecture | org.springframework.stereotype
|
@Component , @Controller , @Repository , @Service
|
Bean configuration
|
org.springframework.beans.factory.annotation
|
@Autowired , @Required , @Value , ...
|
ApplicationContext support
|
org.springframework.context.annotation
|
@Bean , @ComponentScan , @Conditional , @Configuration , @Lazy , @Profile , @Scope , ...
|
Asynchronous method execution | org.springframework.scheduling.annotation
|
@Async , @Scheduled
|
Binding requests to controllers and handler methods | org.springframework.web.bind.annotation
|
@RestController , @GetMapping , @PostMapping , @PutMapping , @PathVariable , @RequestBody , @ResponseBody , ...
|
Transaction demarcation | org.springframework.transaction.annotation
|
@Transactional , @EnableTransactionManagement
|
Annotation-driven tests | org.springframework.test.annotation
|
@IfProfileValue , @Repeat , @Rollback , @Timed
|
MBean exposure
|
org.springframework.jmx.export.annotation
|
@ManagedResource , ManagedAttribute , ManagedOperation
|
Declaratively configuring field formatting rules | org.springframework.format.annotation
|
@DateTimeFormat , @NumberFormat
|
Spring Boot's auto-configuration capabilities. | org.springframework.boot.autoconfigure
|
EnableAutoConfiguration , SpringBootApplication
|
SpEL
- Expression support for defining bean definitions
- Escaping Spring EL (2010)
- SpEL Predefined Variables (12-May-2011)
- SpEL
- How do I reference a bean in spring-el named with a prefix? (Jul 9 '13)
Transaction
- Using
@Transactional
- "Spring recommends that you only annotate concrete classes (and methods of concrete classes) with the
@Transactional
annotation, as opposed to annotating interfaces." - "When using proxies, you should apply the
@Transactional
annotation only to methods with public visibility." - "In proxy mode (which is the default), only external method calls coming in through the proxy are intercepted. This means that self-invocation, in effect, a method within the target object calling another method of the target object, will not lead to an actual transaction at runtime even if the invoked method is marked with
@Transactional
." - "
@EnableTransactionManagement
and<tx:annotation-driven/>
only looks for@Transactional
on beans in the same application context they are defined in." - Annotation driven transaction settings
- Use
aspectj
mode as possible.
- "Spring recommends that you only annotate concrete classes (and methods of concrete classes) with the
- Using
@Transactional
with AspectJ - SPRING DECLARATIVE TRANSACTION MANAGEMENT WITH JDK PROXY VS ASPECTJ(28 JUNE, 2014)
- Use compile-time weaving (not load-time weaving) with @aspectj@ mode to processing
@Transactional
annotations.
- Use compile-time weaving (not load-time weaving) with @aspectj@ mode to processing
- Distributed transactions in Spring, with and without XA
- XA transactions using Spring
- J2EE Without the Application Server
- Configuring Spring and JTA without full Java EE
- Understanding transaction pitfalls(February 03, 2009)
AOP
Concurrency
- API
TaskExecutor
- Simple task executor interface that abstracts the execution of a
Runnable
.
- Simple task executor interface that abstracts the execution of a
ConcurrentTaskExecutor
- Adapter that takes a
java.util.concurrent.Executor
and exposes a SpringTaskExecutor
for it.
- Adapter that takes a
ExecutorServiceAdapter
- Adapter that takes a Spring
TaskExecutor
and exposes a fulljava.util.concurrent.ExecutorService
for it.
- Adapter that takes a Spring
ThreadPoolTaskExecutor
- JavaBean that allows for configuring a
ThreadPoolExecutor
in bean style (through its "corePoolSize", "maxPoolSize", "keepAliveSeconds", "queueCapacity" properties) and exposing it as a SpringTaskExecutor
.
- JavaBean that allows for configuring a
ThreadPoolExecutorFactoryBean
- JavaBean that allows for configuring a
ThreadPoolExecutor
in bean style (through its "corePoolSize", "maxPoolSize", "keepAliveSeconds", "queueCapacity" properties) and exposing it as a bean reference of its nativeExecutorService
type.
- JavaBean that allows for configuring a
@Scheduled
@Async
- 4 ways to schedule tasks in Spring 3 :
@Scheduled
example (April 23, 2013) - How to parameterize @Scheduled(fixedDelay) with Spring 3.0 expression language? (Apr 8 '10)
Internationalization
- API
- Spring Resource bundle with ResourceBundleMessageSource example (August 30, 2012)
MVC
- Exception Handling
- Exception Handling in Spring MVC (NOVEMBER 01, 2013)
- @CONTROLLERADVICE IMPROVEMENTS IN SPRING 4 (2013/11)
- Error Handling for REST with Spring (January 23, 2017)
org.springframework.web.servlet.handler.HandlerExceptionResolverComposite
APIorg.springframework.web.servlet.handler.HandlerExceptionResolverComposite
source
- misc
- Learn to customize Spring MVC @Controller method arguments(November 8, 2009)
REST
- Spring Restfull Jax-RS annotation support (Aug 28 '14)
- No, you cannot use
javax.ws.*
annotations in spring.
- No, you cannot use
JMX
- Exporting your beans to JMX
- API
org.springframework.jmx.export.MBeanExporter
public void destroy()
: Unregisters all beans that this exported has exposed via JMX when the enclosingApplicationContext
is destroyed.
org.springframework.jmx.export.annotation
package@ManagedResource
@ManagedAttribute
@ManagedOperation
- MBean descriptor
- ManagedResource Annotation is not annoted as Inherited
- We finally changed @ManagedResource to inherited now. Potential object name collisions need to be dealt with; the general recommendation is to not specify an object name value in @ManagedResource at all when using it on a base class.
Jackson
misc
- Nested Property Placeholders in Spring Configuration
- Spring 3 MVC and JSON example
- Spring framework p notation schema example
- Logging Dependencies in Spring
- Using JCL, SLF4J or Log4j with Spring
- Embedded Db’s in Spring 3.0.(2011/05/02)
- Set System Property With Spring Configuration File (Jul 26 '10)
Spring Boot
- http://projects.spring.io/spring-boot/
- Desc. : makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".
- Sources : https://github.com/spring-projects/spring-boot
References
- 2.0
- Spring Boot 2.0 Reference Guide
- Spring Boot 2.0 API
- Spring Boot 2.0 Actuator Web API
org.springframework.boot » spring-boot-starter-parent » 2.0.0 » pom.xml
org.springframework.boot » spring-boot-dependencies » 2.0.0 » pom.xml
org.springframework.boot.autoconfigure.SpringBootApplication
- a convenience annotation that is equivalent to declaring
@Configuration
,@EnableAutoConfiguration
and@ComponentScan
- a convenience annotation that is equivalent to declaring
- Common application properties
- 1.5
General
- GETTING STARTED Building an Application with Spring Boot
- Spring Boot without the web server (Sep 29 '14)
- How to load Spring Boot Application Context while using Cucumber at runtime, not via tests? (Nov 15 '17)
Config
- Spring MVC auto-configuration
Converter
,GenericConverter
,Formatter
,HttpMessageConverters
,MessageCodesResolver
,ConfigurableWebBindingInitializer
- Common application properties
- Understanding Spring Boot (06 Mar 2015)
- Condition annotations
- Class conditions, Bean conditions, Property conditions, Resource conditions, Web application conditions, SpEL expression conditions
org.springframework.boot.autoconfigure.condition
packageConditionalOnProperty
API
- Sources
Actuator
- Spring Boot Actuator: Production-ready features
- Building a RESTful Web Service with Spring Boot Actuator
- Spring Boot 2.0 Migration Guide / Spring Boot Actuator
- Spring Boot Actuator (June 8, 2018)
- Spring Boot Actuator: A Complete Guide (Feb. 27, 17)
- Securing Actuator Endpoints
Error Control
- Error Handling
- API
- source
REST
- Customize the Jackson ObjectMapper
- Spring From the Trenches: Parsing Date and Time Information From a Request Parameter (September 28, 2015)
- Configuring a global date & time format
- Parsing of LocalDate query parameters in Spring Boot (08/07/17)
- Set default format for LocalDateTime (jsr310) in spring when passed to a rest end point (Aug 27 '17)
Test
Class/Package | Description | Remarks |
---|---|---|
Annotation Type SpringBootTest
|
Annotation that can be specified on a test class that runs Spring Boot based tests | |
Enum SpringBootTest.WebEnvironment
|
An enumeration web environment modes | DEFINED_PORT , MOCK , NONE , RANDOM_PORT
|
Annotation Type MockBean
|
Annotation that can be used to add mocks to a Spring ApplicationContext | |
Class SpringExtension
|
integrates the Spring TestContext Framework into JUnit 5's Jupiter programming model. |
- Test that startup an Spring-boot application (Apr 7 '17)
webEnvironment
- Disable Test with Spring and Junit5 without context creation (Oct 9 '17)
- Spring Boot and JUnit 5 (APRIL 12, 2017)
- Testing improvements in Spring Boot 1.4 (APRIL 15, 2016)
misc
- Configuring Logback With Spring Boot (Sep. 03, 17)
- USING YAML IN SPRING BOOT TO CONFIGURE LOGBACK (May 5, 2016)
Spring Security
- http://projects.spring.io/spring-security/
- Desc. : a powerful and highly customizable authentication and access-control framework.
- Sources : https://github.com/spring-projects/spring-security
- Maven artifacts
References
Concepts
- Access control in computer security
- ACL(Access Control List)
- JAAS(Java Authentication and Authorization Service)
- Spring Security Core Components :
SecurityContext
,Authentication
,GrantedAuthority
,UserDetails
,UserDetailsService
Concept | Description | API | Remarks |
---|---|---|---|
Principal | the abstract notion of a principal, which can be used to represent any entity, such as an individual, a corporation, and a login id | java.security.Principal
|
Party |
Subject | a grouping of related information for a single entity, such as a person, including the Subject's identities as well as its security-related attributes (passwords and cryptographic keys, for example) | javax.security.auth.Subject
|
A subject can contain multiple principals and a principal represents the face of a subject. |
Authentication | authenticated principal | org.springframework.security.core.Authentication
|
interface Authentication extends Principal
|
GrantedAuthority | application-wide permissions granted to a principal | org.springframework.security.core.GrantedAuthority
|
Role |
Core Component
Component | API | Source | Remarks |
---|---|---|---|
Authentication | org.springframework.security.core.Authentication
|
||
UsernamePasswordAuthenticationToken | org.springframework.security.authentication.UsernamePasswordAuthenticationToken
|
An Authentication implementation that is designed for simple presentation of a username and password | |
TestingAuthenticationToken | org.springframework.security.authentication.TestingAuthenticationToken
|
An Authentication implementation that is designed for use whilst unit testing |
General
- Spring Boot Security Application (25/12/2014)
Session Management
- Control the Session with Spring Security(March 10, 2017)
Authentication
Authorization
- Expression-Based Access Control in Spring Security 4.2
- Expression-Based Access Control in Spring Security 3.1
Spring Session
- http://projects.spring.io/spring-session/
- Desc. : provides an API and implementations for managing a user’s session information
References
Spring Cloud
- Building Microservices with Spring Cloud and Docker (July 12, 2015)
misc
XML Schemas
beans
- namespace :
http://www.springframework.org/schema/beans
- description : defines the elements to define beans.
- sources
- namespace :
context
- namespace :
http://www.springframework.org/schema/context
- description : defines the configuration elements for the Spring Framework's application.
- sources
- namespace :
aop
- namespace :
http://www.springframework.org/schema/aop
- description : defines the configuration elements for the Spring Framework's AOP support.
- sources
- namespace :
jdbc
- namespace :
http://www.springframework.org/schema/jdbc
- description : defines the configuration elements for the Spring Framework's embedded database support.
- sources
- namespace :
tx
- namespace : http://www.springframework.org/schema/tx
- description : defines the elements used in the Spring Framework's declarative transaction management infrastructure.
- schema : http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
mvc
- namespace : http://www.springframework.org/schema/mvc
- description
- schema : http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
util
- namespace : http://www.springframework.org/schema/util
- description
- sources
task
- namespace : http://www.springframework.org/schema/task/
- description
- schema : http://www.springframework.org/schema/task/spring-task-4.2.xsd
Samples
Working of @Transactional
annotation according to the processing mode(proxy
or aspectj
)
Test code outline
- Annotated method is private :
private SearchRequest createAndPersistNewSearchReq()
- In-call is invoked from
testProcess
tocreateAndPersistNewSearchReq()
public class SearchTaskProcessorTest{
//...
ConfigurableApplicationContext spring;
private SearchMasterMapper masterMapper;
private SearchDetailMapper detailMapper;
private SearchRoiMapper roiMapper;
private SearchRoiParamMapper roiParamMapper;
private SearchRoiPointMapper roiPointMapper;
@BeforeClass
public void beforeClass() throws Exception{
//...
this.spring = new ClassPathXmlApplicationContext(CONFIG_LOCATION);
this.spring.registerShutdownHook();
this.masterMapper = this.spring.getBean("searchMasterMapper", SearchMasterMapper.class);
this.detailMapper = this.spring.getBean("searchDetailMapper", SearchDetailMapper.class);
this.roiMapper = this.spring.getBean("searchRoiMapper", SearchRoiMapper.class);
this.roiParamMapper = this.spring.getBean("searchRoiParamMapper", SearchRoiParamMapper.class);
this.roiPointMapper = this.spring.getBean("searchRoiPointMapper", SearchRoiPointMapper.class);
}
@AfterClass
public void afterClass(){ }
@Transactional
private SearchRequest createAndPersistNewSearchReq(){
//...
this.masterMapper.insertSearchMaster(req);
this.detailMapper.insertSearchDetail(req.getId(), cctv);
this.roiMapper.insertSearchRoi(req.getId(), cctv.getSystemId(), cctv.getId(), roi);
for(Param param: roi.getParams()){
this.roiParamMapper.insertSearchRoiParam(req.getId(), cctv.getSystemId(),
cctv.getId(), roi.getNo(), param);
}
for(Point pt: roi.getPoints()){
this.roiPointMapper.insertSearchRoiPoint(req.getId(), cctv.getSystemId(),
cctv.getId(), roi.getNo(), pt);
}
return req;
}
@Test
public void testProcess(){
SearchRequest req = this.createAndPersistNewSearchReq();
SearchTaskProcessor processor = this.spring.getBean(...);
//...
processor.process(req.getId(), "1", crtr);
}
}
In proxy mode
- Log