Tutorial 2 : Extend Your App
Extend With In-place Code Templates
Extend/Customize with In-place Code Templates For Backend Spring-Java.
Extend Java Backend
Practice building and coding for Spring Java backend development with examples in generated code.
Reference is made to generated code for sample tables
- erp_customer: Java Classes named ErpCustomer*
- erp_product: Java Classes named ErpProduct*
You can locate your own table's respective & relevant files if not using above tables.
Q: Most Needed Topics
Here are some most needed topics:
- Singleton
- Exception Handling
- Collections
They are covered in sections below.
Q: Singleton
Singleton class in Java: Implement a singleton class
A: A class that can possess only one object at a time is called a singleton class.
In Spring Boot, @Configuration
class and @Bean
annotation will always provide you same bean instance.
Locate file in generated code to see example:
emapi\lib\base-app\src\main\java\com\example\emapi\app\EmApiRequestLoggingFilterConfig.java
@Configuration
public class EmApiRequestLoggingFilterConfig {
@Bean
public CommonsRequestLoggingFilter logFilter() {
Q: Exception Handling
Implement Exception Handling in Java | in Spring Boot | in Spring Boot for GraphQL
A:
Declare a Custom Exception and use it.
Locate file in generated code to see example:
emapi\lib\base-app\src\main\java\com\example\emapi\app\EmResourceNotFoundException.java
Used in Service Class:
emapi\lib\base-app\src\main\java\com\example\emapi\app\ErpCustomer\ErpCustomerService.java
Used in GraphQL Custom Exception Resolver:
emapi\app\dbgraphql\src\main\java\com\example\emapi\app\EmGqlCustomExceptionResolver.java
In-place code Templates File
Locate file in generated code to see examples:
emapi\lib\base-app\src\main\java\com\example\emapi\app\ErpCustomer\ErpCustomerServiceExtend.java
Q: Using Java Collections
Practice coding for various Java Collections usages.
Locate file in generated code to see examples:
emapi\lib\base-app\src\main\java\com\example\emapi\app\ErpCustomer\ErpCustomerServiceExtend.java
Sort Collection List
- Using
stream().sorted()
- Using
Sort Collection List Via Custom Sorting
- Using Comparator and
Collections.sort(...)
- Using Comparator and
See more examples included under "Java 8 Features".
Q: Implementing Validation
Implementing Java Object Validation.
Locate file in generated code to see examples:
emapi\lib\base-app\src\main\java\com\example\emapi\app\ErpCustomer\ErpCustomerServiceExtend.java
- EXAMPLE FOR implementing Validation
- See
ErpCustomerValidate()
- See
Q: Java 8+ Features
Locate file in generated code to see examples for all below:
emapi\lib\base-app\src\main\java\com\example\emapi\app\ErpCustomer\ErpCustomerServiceExtend.java
EXAMPLE FOR using: Java Lambda Expression
- See
ErpCustomerListLambda()
,ErpCustomerLambda()
- See
EXAMPLE FOR using: Stream Reduce
- Use for: calculate aggregates, sum, averages, count
- See
ErpCustomerListReduce()
EXAMPLE FOR using: Stream Filter
- Use for filter/exclude records from list
- See
ErpCustomerListFilter()
EXAMPLE FOR using: Stream Filter & Count
- Use for filter/exclude records from list
- Count matching value records and return
- See
ErpCustomerListFilterCount()
- Use for filter/exclude records from list
Implement Functional Interfaces
EXAMPLE FOR implementing:
Function<T,R>
- Use for Implementing Calculations, Transformations
- See
ErpCustomerFunction
- Need to
@Override
methodapply()
- Need to
EXAMPLE FOR implementing:
Consumer<T>
- Use for: Use it for 1-way action flow e.g. create shipping record after order create, returns no result
- See
ErpCustomerConsumer
- Need to
@Override
methodaccept()
- Need to
Implement and Use: Misc Usage Collections, Iterators, Thread-safe Collections
- See examples inside
ErpCustomerListMiscUsageCollections()
for all below:- Set Interface - no duplicate elements, no random access, iterator to traverse elements
- Set implementations - HashSet , TreeSet , and LinkedHashSet
- Map Interface - map keys to values, no duplicate keys. Operations: put, get, containsKey, containsValue, size, isEmpty
- Map implementations - HashMap, TreeMap, and LinkedHashMap.
- ListIterator Interface
- Fail-Safe Iterators
- ConcurrentHashMap, CopyOnWriteArrayList
- binarySearch: how it works? locates middle then takes half half and checks-continue, input needs to be sorted list (beforehand)
- Collections static Methods
- Collections.sort() static method using one of below:
- Comparator and using
Comparator.comparing
- reverseOrder()
- custom lambda Comparator with
compareTo
- Comparator and using
- Collections - Shuffle, addAll
- Collections.sort() static method using one of below:
- Arrays.sort()
- Get Synchronized Collections via static methods
- Synchronized vs Concurrent Collections examples
- Set Interface - no duplicate elements, no random access, iterator to traverse elements
- See examples inside
Implement and Use: EXAMPLE FOR implementing: Misc Usage Threads
- Thread examples
- Create Thread with Runnable instance lambda
- Create Thread with inner anonymous subclass
- Use threads for runnableTaskExample
- Thread run with
start()
- Thread run with Executor
execute()
- Thread run with
- Use ExecutorService : For Running tasks in asynchronous mode
- runnableTask example
- Runnable,
executorService.execute()
- Runnable,
- callableTask example - with returnig Future result
- Callable,
executorService.submit()
, Future,futureResult.get()
- alternatively call with
executorService.invokeAny()
,executorService.invokeAll()
- alternatively call with
- Callable,
- ExecutorService shut down
shutdown()
and shut down immediatelyshutdownNow()
- runnableTask example
- Thread examples
Cloud Functions code Templates File
Locate file in generated code to see examples for all below:
emapi\lib\base-app\src\main\java\com\example\emapi\app\ErpCustomer\ErpCustomerServiceCloudFunctions.java
Q: Implementing Serverless Functions
- Enabling Serverless Functions with Spring Cloud Function
Locate file in generated code to see examples for all below:
emapi\lib\base-app\src\main\java\com\example\emapi\app\ErpCustomer\ErpCustomerServiceCloudFunctions.java
Implement : Spring Cloud Functions
These can be Integrated Further with any target Cloud: AWS, GCP, or Azure
Cloud Function implementing
Function<I, O>
- See
ErpCustomerCfFunction()
- See
Cloud Function implementing
Supplier<O>
reactive- See
ErpCustomerCfSupplierFlux()
- See
Cloud Function implementing
Supplier<O>
imperative- See
ErpCustomerCfSupplier()
- See
Cloud Function implementing
Consumer<I>
- See
ErpCustomerCfConsumer()
- See