Please enable JavaScript.
Coggle requires JavaScript to display documents.
Spring (Testing (@ExtendWith(SpringExtension.class), @ContextConfiguration…
Spring
Testing
-
@ContextConfiguration(classes=YourTestConfig.class), if no class gave, go into the class to find Configuration annotation.
-
-
@Sql(scripts="/testfiles/test-data.sql", executionPhase=AFTER_TEST_METHOD, config=\@SqlConfig(errorMode=FAIL_ON_ERROR,...))
run given sql file before(default: without executionPhrase property) test executes
use @Transactional on @Test methods to rollback after test done, no need to clean up DB
use @Commit to commit tx at end of test
use @Before\AfterTransaction to run before/after entering/exiting tx
-
@DirtiesContext force context to close after a test method. can test PreDestroy behavior. New cached context built after method with this annotation.
@TestPropertyResource(properties={"[properties]"} locations="[file path(default file name is the class name)]")
-
Transaction Management
@Transactional
-
-
-
-
readOnly=true for read-only tx, faster -> same datasource, reduce the connection in one transaction.
-
How?
-
- declare a PlatformTransactionManager bean
(use @EnableTransactionManagement on class which define the manager bean)
- Declare the transactional methods
-
warp the target class in a proxy, with an @Around advice
the proxy implements following behavior
- Tx started before entering the txnal method
- Commit at the end of the mothod
- Rollback if method throws RuntimeException (can be overridden)
-
Isolation Levels
-
-
REPEATABLE_READ
-
-
may result in locking, depending on DBMS used
-
Tx propagation
REQUIRED
default, execute within current tx, create one if none exists
REQUIRES_NEW
create a new tx, suspending the current tx if exists
-
Rollback
-
noRollbackFor={ClassB.class, ClassC.class}
-
Programmatic Transaction
new TransactionTemplate(txManager).execute( (status) -> { ... } )
status.setRollbackOnly() in catch block
-
@Sql transaction control
@Sql(config=@SqlConfig(
transactionMode=TransactionMode.ISOLATED/INFERRED/DEFAULT),
transactionManager="someTxMgr",
dataSource="someDataSource")
@EnableTransactionManagement: example of BPP, to proxies transactional beans
AOP
enables modularization of cross-cutting concerns which is
generic funcionality needed across many places
-
-
Concepts
Join Point
a point of execution of a program, like method call or exception thrown
-
-
-
-
APIs
-
Advice types
@AfterReturning(value="execution...",
returning="someType")
access JoinPoint, returned value from target method
@AfterThrowing(value=..., throwing="exceptionType")
-
-
do NOT stop exception propagation, use @Around advice to stop it
-
@Around
-
ProceedingJoinPoint inherits JoinPoint and adds proceed(), which calls the target method
-
-
Pointcut Designator:
1.execution(@SomeAnnotation *(return type) com.example.*Service.find*(int, ..));
2.can use multiple expression at the same time.
-
-
-
Spring Boot
use sensible defaults to initialize app based on classpath contents
e.g. if spring-jdbbc.jar is on classpath, create a JdbcTemplate
-
-
@SpringBootTest(classes={C1.class, C2.class})
-
Dependency Injection
-
-
Dependency injection default to be true.By default, inject bean defined in other @configuration file, given that this file is @import-ed in that file, or throw out exception.
Use required = false to override default behavior
-
-
ways
Constructor dependency injection:
->precisely many parameters in constructor method;
->injection is mandatory and immutable
Setters:
-> fields or method parameters
-> injection can be inherited automatically.
-> changeable and circular dependency.
Application Context
-
Beans
-
-
duplicate beans
-
Order (control which Bean method to run and this is the class level annotation)
Scope (@Scope("[scopeName]") )
-
singleton (default)
To multi-thread issues(same bean at same time):
1.stateless or immutable beans;
- sychronized;
- different scope.
-
-
-
-
-
-
-
-
@Configuration
@Import({ConfA.class, ConfB.class})
to separate config into multiple files(Best practice is to separate "application" bean from "infrastructure" bean)
-
Bean (bean definition)
used inside configuration classes
-
Environment
-
@Value
-
BUT need a static bean for PropertySourcesPlaceholderConfigurer()
- automactically defined in spring boot projects
-
JPA
EntityManager
-
Lifecycle is bound to a Transaction, usually managed by container
APIs
-
-
find(Class entity, Object primaryKey)
JPA Quering
Query createQuery(String jpqlString, [returnclass])
.setParameter(paramName/index, value)
.getResultList()/getSingleResult()
select required, cannot user *
-
-
-
-
-
Lifecycle
-
-
Destruction
context close ->bean clean up (@PreDestroy method called, beans released for garbage collection )
Data Management
-
Caching
@Cacheable(value="cachestore name", key="cached item key(pattern: #method param name)", condition="#title.length < 32") marks a method for caching its return value from the same input arguments
:question: per thread or global?
cache applies to spring beans, scope should be same with cached bean
@Cacheable
key
can use object field as key, key="#author.name"
can use custom key generator, key="T(example.KeyGen).hash(#author)"
condition
only cache if condition is true, condition="#person.age < 20"
@CacheEvict
(value="cachestore name", beforeInvocation=true) clear cache before method invoked
-
-
JdbcTemplate
queryForObject("SELECT * FROM ...", JavaBuiltinType.class,[paramValues in sequence with comma])
query for domain objects
lamba expression is from java 8 and it can be encapsulated in a class implementing 3 interfaces for resultSet
query(someSQL, [(RowCallbackHandler/ResultSetExtractor<T>)](ResultSet rs, [int row]) -> expression to deal with one row in rs)
reuse just ONE template, it's thread-safe after constructed
-
-
RowMapper<T>
T mapRow(ResultSet rs, int rowNum) throws SQLException
-
ResultSetExtractor<T>
T extractData(ResultSet rs) throws SQLException, DataAccessException
-
-
Lifecycle Control
-
->any visibility, BUT must take NO args and return void
->not spring annotation. javax.annotation
-
Bean level
class which cannot be annotated
initMethod="aMethodName", destroyMethod="someMethodName"
-
Spring Data
Instant Repository
- annotate domain class, define keys and enable persistence
- Define repository as an interface and extend Repository<T,K>
- at run-time, Spring scans for interfaces extending Repository<T, K> and
generate CRUD methods automatically
also support paging, sorting (via PagingAndSortingRespository<T, K>), custom queries etc.
Auto-generated finder methods named like
find[First]By{FieldName}[GreaterThan|NotEquals|Between|Like|...]
-
Add Custom Behavior
- Create normal interface and implementation
- mix-in with an automatic repository
-
Stereotype Annotations
predefined in spring, themselves annotated with Component
-
DAO class, Data access logic
-
-
-
-
-
-