# Spring: Java Configuration

Spring is most popular framework for building web applications in Java ecosystem. Spring Framework can be configured either in XML, or with Java code. This page shows how to configure various Spring modules with Java. We will start with a simple application and in every section we will be adding one more Spring module to it.
If you are interested in how to do all the same things with XML configuration, this page can help you: Spring_MVC:_Tutorial

# Spring MVC

* Create .\pom.xml:
* Create .\src\main\java\myapp\config\WebInit.java:
* Create .\src\main\java\myapp\config\WebConfig.java:
* Create .\src\main\java\myapp\controller\MainController.java:
* mvn clean jetty:run and curl http://localhost:8080/raw:
Sources:
* zetcode.com: Three basic approaches to configure a Spring web application
* baeldung.com: web.xml vs Initializer

# JSP

* Add this bean to WebConfig.java:
* Add this method to MainController.java:
* Create .\src\main\webapp\WEB-INF\jsp\greeting.jsp:
* Visit http://localhost:8080/jsp.
Sources:
* Spring Reference: Web on Servlet Stack - JSP and JSTL

# Thymeleaf

* Add this dependency to pom.xml:
* Add this beans to WebConfig.java:
* Add this method to MainController.java:
* Create .\src\main\webapp\WEB-INF\thy\hello.html:
* Visit http://localhost:8080/thy.
Sources:
* thymeleaf.org: Tutorial: Thymeleaf + Spring

# CSS

* Add this method to WebConfig.java:
* Create src\main\webapp\resources\css\styles.css:
* To your greeting.jsp/<html>/<head> page add this line:
* To your hello.html/<html>/<head> add this line:
Now background of these pages will turn to grey.
Sources:
* Spring Reference: Static resources
* baeldung: Serve Static Resources with Spring

# Spring Security

* Add these dependencies to your pom.xml:
* Create .\src\main\java\myapp\security\SecurityInitialization.java:
* Create .\src\main\java\myapp\security\SecurityConfiguration.java:
Now you will have to log in to view any page of your web application.
Sources:
* Spring Security Reference: Java Configuration

### Spring Security + Thymeleaf

* To your pom.xml add this dependency:
* In WebConfig#templateEngine register new dialect:
* To your hello.html/<html>/<body> add this line:
* Visit http://localhost:8080/thy, if you're logged in, your username will be displayed.
Sources:
* thymeleaf.org: Thymeleaf + Spring Security

### Spring Security + JSP

* Add this dependency to your pom.xml:
* Add this line above <html> tag in greeting.jsp:
* Add this line in greeting.jsp/<html>/<body>:
* Visit http://localhost:8080/jsp, if you're logged in your username will be displayed.
Sources:
* Spring Security Reference: JSP Tag Libraries

# Logging

In any of your classes you can use this built-in logger right-away:
But if you'll want to use log4j2, you'll need to add it's library to dependencies and log4j2.xml to classpath:
* Add this dependency to your pom.xml:
* Create .\src\main\resources\log4j2.xml:
* In any of your classes, use org.apache.commons.* package to write logs as with the built-in logger, Spring will now use log4j2 under the hood.
But if you'll want to use log4j2 through slf4j interface, here's what you'll need to do:
* Remove log4j-core dependency if you have it;
* Add this dependency higher than Thymeleaf dependency:
* In any of your classes, use slf4j interface:
Now you also can manage Thymeleaf logs as well, since it uses slf4j internally.
Sources:
* Spring Reference: Logging

# Internationalization

* To WebConfig.java add these beans and an interceptor:
* Create src\main\resources\messages_en.properties:
* Create src\main\resources\messages_ru.properties:
* In your Thymeleaf template *.html/<html>/<body> add this line:
* In your greeting.jsp add these lines:
* Visit these pages and change language of message by appending ?lang=ru or ?lang=en parameters to URL.
Sources:
* Spring Reference: Session Resolver and Locale Interceptor
* Spring Reference: I18n using MessageSource
* baeldung: MVC Interceptors Java Config
* mkyong: Spring MVC i18n example

# Aspects

* Add this dependency to your pom.xml:
* To WebConfig.java add this annotation:
* Create .\src\main\java\myapp\aop\LoggingAspect.java:
Now every invocation of every method from *.controller package will be surrounded by corresponding log messages.

# DataSource: H2 Embedded Database

Regardless of what technology you will choose for your application to interact with database, you will need a datasource bean. For convenience, this project uses embedded H2 database, and this section will show you how to enable it and get it's datasource bean.
* Add these dependencies to your pom.xml:
* Add this bean to WebConfig.java:
* Create .\src\main\resources\schema.sql:
* Create .\src\main\resources\data.sql:
Now if you will start your application you will see a log:
Sources:
* Spring Reference: Creating an Embedded Database Programmatically

# JdbcTemplate

JdbcTemplate doesn't need any explicit configuration. You'll need only spring-jdbc dependency and datasource bean, which you've already got in previous section. This is an example of how to use JdbcTemplate:
* Create .\src\main\java\myapp\model\Message.java:
* Create .\src\main\java\myapp\service\MessageService.java:
* Create src\main\java\myapp\repository\MessageJdbcDaoImpl.java:
* In your MainController.java add this imports, class variable and a method:
* Visit https://localhost:8080/messages and you will see contents of your Message table. Create new message by sending it's content as request parameter: https://localhost:8080/messages/new?content=ThisShouldWork

# REST

* Add this dependency to pom.xml:
* Add this method to WebConfig.java:
* Create .\src\main\java\myapp\rest\MainRest.java:
* In SecurityConfiguration#securityFilterChain add csrf.disable statement like that:
Now http://localhost:8080/rest/messages will show all your messages in json format, and you can create new messages by sending them to the same address through curl or PowerShell:

# Hibernate Configuration

At this point you should have a working dataSource bean.
* Add these dependencies to pom.xml:
* Add these beans and class-level annotation to WebConfig.java:
Now you can create Entity classes with annotations from jakarta.persistence package and use SessionFactory or EntityManager in your dao layer:
Sources:
* baeldung: Bootstrapping Hibernate 5 with Spring