3-SpringBoot BankApp Project July 24
3-SpringBoot BankApp Project July 24
================================================================
Day -1
Day -2
Day -3
Bankapplication applicatation
---------------------------
* Spring vs Spring boot, Spring boot introduction
* Spring DI Qucick recap
* AOP how to handle cross cutting concerns
* Spring boot rest
* spring boot data jpa
* Excpetion handling
* Validation
* Actuator
* logging
* Security
=> Auto-Configuration
=> Microservice
repository layer
Dao layer:
--------------
public AccountDaoCollectionImpl() {
accounts.put(1, new Account(1, "raj", 560000.00));
accounts.put(2, new Account(2, "ekta", 760000.00));
}
@Override
public List<Account> getAll() {
System.out.println("getAll using hard coded collection...");
return new ArrayList<Account>(accounts.values());
}
@Override
public Account getById(int id) {
return accounts.get(id);
}
@Override
public void updateAccount(Account account) {
accounts.put(account.getId(), account);
}
Service layer:
-----------------
public interface AccountService {
public List<Account> getAll();
public Account getById(int id);
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Account {
@NotNull(message = "{account.name.absent}")
@Pattern(regexp = "[A-Za-z]+( [A-Za-z]+)*", message =
"{account.name.invalid}")
private String name;
@NotNull(message = "{account.balance.absent}")
@Range(min = 100, max = 100000, message = "{account.balance.invalid}")
private BigDecimal balance;
@Email(message = "{account.email.invalid}")
@NotNull(message = "{account.email.absent}")
private String email;
@NotNull(message = "{account.phone.absent}")
@Pattern(regexp = "[789][0-9]{9}", message = "{account.phone.invalid}")
private String phone;
@RestControllerAdvice
public class AccountExceptionRestController {
@Autowired
private Environment environment;
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentNotValidException.class)
public Map<String, String>
handleInvalidArgument(MethodArgumentNotValidException ex) {
ValidationMessages.properties
-------------------------------
account.email.absent=Please provide email address
account.email.invalid=Please provide valid email address
@EnableConfigurationProperties(Config.class)
@ConfigurationProperties(prefix = "msg")
<aop:aspectj-autoproxy />
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Loggable {
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class MethodLogger {
private static final Logger
logger=LoggerFactory.getLogger(MethodLogger.class);
@Around("@annotation(Loggable)")
public Object around(ProceedingJoinPoint point) throws Throwable {
long start = System.currentTimeMillis();
Object result = point.proceed();
logger.info("Method call takes" +(System.currentTimeMillis() - start));
return result;
}
}
// logger.info("start
"+MethodSignature.class.cast(point.getSignature()).getMethod().getName()+" is
called"+" takes+(System.currentTimeMillis() - start));
spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto= update
spring.datasource.url=jdbc:mysql://localhost:3306/busycoder?useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.hibernate.ddl-auto=update
logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR
logging.level.com.productapp: INFO
logging.level.com.productapp.service: INFO
spring.jpa.show-sql=true
spring.banner.location=
spring.jmx.enabled=true
management.endpoints.web.exposure.include=*
management.endpoints.jmx.exposure.include=*
management.info.env.enabled=true
info.app.encoding=UTF-8
info.app.java.source=21
info.app.java.target=21
info.app.name=productapp
info.app.dev=amit ku
management.endpoint.health.show-details=always
management.endpoint.health.probes.enabled=true
# livenessstate readinessstate
#management.health.livenessstate.enabled=true
#management.health.readinessstate.enabled=true
info.key=default
spring.profiles.active=test
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=foo
spring.datasource.password=foo
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.jpa.hibernate.ddl-auto=update