Spring: Java Configuration

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 :

Create :

Create :

Create :

 and  :

hello there! UTF-8 ?? ???????? ?



Sources:
 * zetcode.com: Three basic approaches to configure a Spring web application
 * baeldung.com: web.xml vs Initializer

= JSP =  Add this bean to :

Add this method to :</li>

<li>Create :</li>

<li>Visit http://localhost:8080/jsp.</li> </ol>

Sources:
 * Spring Reference: Web on Servlet Stack - JSP and JSTL

= Thymeleaf = <ol> <li>Add this dependency to :</li>

<li>Add this beans to :</li>

<li>Add this method to :</li>

<li>Create :</li>

<li>Visit .</li> </ol>

Sources:
 * thymeleaf.org: Tutorial: Thymeleaf + Spring

= CSS = <ol> <li>Add this method to :</li>

<li>Create :</li>

<li>To your /  page add this line:</li>

<li>To your /  add this line:</li> </ol> Now background of these pages will turn to grey.

Sources:
 * Spring Reference: Static resources
 * baeldung: Serve Static Resources with Spring

= Spring Security =

<ol> <li>Add these dependencies to your :</li>

<li>Create :</li>

<li>Create :</li>

</ol> Now you will have to log in to view any page of your web application.
 * To register multiple users, just create new UserDetails and pass it to the constructor as well, it accepts varargs.

Sources:
 * Spring Security Reference: Java Configuration

Spring Security + Thymeleaf
<ol> <li>To your  add this dependency:</li>

<li>In  register new dialect:</li>

<li>To your /  add this line:</li>

<li>Visit http://localhost:8080/thy, if you're logged in, your username will be displayed.</li> </ol>

Sources:

<ul> <li> thymeleaf.org: Thymeleaf + Spring Security </li> </ul>

Spring Security + JSP
<ol> <li>Add this dependency to your :</li>

<li>Add this line above  tag in  :</li>

<li>Add this line in / :</li>

<li> Visit http://localhost:8080/jsp, if you're logged in your username will be displayed. </li> </ol>

Sources: <ul> <li> Spring Security Reference: JSP Tag Libraries </li></ul>

= 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  to classpath: <ol> <li>Add this dependency to your :</li>

<li>Create :</li>

<li>In any of your classes, use  package to write logs as with the built-in logger, Spring will now use log4j2 under the hood.</li> </ol> But if you'll want to use log4j2 through slf4j interface, here's what you'll need to do: <ol> <li>Remove  dependency if you have it;</li> <li>Add this dependency higher than Thymeleaf dependency:</li>

<li>In any of your classes, use slf4j interface:</li>

</ol> Now you also can manage Thymeleaf logs as well, since it uses slf4j internally.

Sources:
 * Spring Reference: Logging

= Internationalization =

<ol> <li>To  add these beans and an interceptor:</li>

<li>Create :</li>

welcome=You are welcome! <li>Create :</li>

welcome=Добро пожаловать! <li>In your Thymeleaf template /  add this line:</li>

<li>In your  add these lines:</li>

<li>Visit these pages and change language of message by appending  or   parameters to URL.</li> </ol>

Sources:

<ul> <li> Spring Reference: Session Resolver and Locale Interceptor </li> <li> Spring Reference: I18n using MessageSource </li> <li> baeldung: MVC Interceptors Java Config </li> <li> mkyong: Spring MVC i18n example </li> </ul>

= Aspects =

<ol> <li>Add this dependency to your :</li>

<li>To  add this annotation:</li>

<li>Create :</li>

</ol> Now every invocation of every method from  package will be surrounded by corresponding log messages.
 * Your logging library may differ, consult Logging section on this page;

= 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.

<ol> <li>Add these dependencies to your :</li>

<li>Add this bean to :</li> <li>Create :</li>

<li>Create :</li> </ol> Now if you will start your application you will see a log:

Starting embedded database: url='jdbc:h2:mem:dataSource;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'

Sources: <ul> <li> Spring Reference: Creating an Embedded Database Programmatically </li> </ul>

= JdbcTemplate =

JdbcTemplate doesn't need any explicit configuration. You'll need only  dependency and datasource bean, which you've already got in previous section. This is an example of how to use JdbcTemplate: <ol> <li>Create :</li>

<li>Create :</li>

<li>Create :</li>

<li>In your  add this imports, class variable and a method:</li>

<li>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</li> </ol>

= REST =

<ol> <li>Add this dependency to :</li>

<li>Add this method to :</li>

<li>Create :</li>

<li>In  add   statement like that:</li>

</ol>

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: curl -X POST --data '{&quot;content&quot;:&quot;in curl send latin text only&quot;}' http://localhost:8080/rest/messages --header 'Content-Type: Application/Json' Invoke-WebRequest -Uri http://localhost:8080/rest/messages -Method POST -ContentType 'application/json;charset=utf-8' -Body '{&quot;content&quot;:&quot;In pwsh you can use all UTF8 characters 😀&quot;}'

= Hibernate Configuration =

At this point you should have a working dataSource bean. <ol> <li>Add these dependencies to :</li>

<li>Add these beans and class-level annotation to :</li>

</ol> Now you can create Entity classes with annotations from  package and use SessionFactory or EntityManager in your dao layer:

Sources:
 * baeldung: Bootstrapping Hibernate 5 with Spring