🏠Spring BootSpring Boot Starters

Spring Boot Starters

Spring Boot Starters add or change the behavior of an application. Starters do this through opinionated defaults and auto-configurations.

Background

In a traditional web application, The developers will have to do the following stuff.

  • Write a spring-context.xml to define the beans required.
  • Write definitions for your database configuration.
  • Define MVC configuration and their mappings/filters/etc.
  • Create a web.xml to define the web application context.
  • Manage dependencies and track their compatibility.

With Spring Boot starters, you can avoid all of the above steps. For example, Take a look at the Hello-World example and see how much code it has.

Spring Boot Starters

With starters, we can add functionalities to a Spring Boot application. Importantly, You don’t have to write code or maintain the JAR files. Because Spring Boot takes care of them.

The developers of spring boot provide a number of core starters. This curated list of starters has well-tested and hand-picked dependencies. They also provide auto-configuration classes that help create beans and configurations needed to apply a specific feature.

For example, A web application may require filter configurations, web context, and controller mapping, error handling, etc. The spring-boot-starter-web provides all of these out of the box. To enable web functionalities to your application, All you need is to add the following dependency.

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>Code language: HTML, XML (xml)

You can also add these starters when you are creating projects from Spring Initializer.

Important Starters

Here are some of the important starters that deal with real-world problems.

Web Starter

The Spring Boot Starter web provides an embeddable tomcat server for the application. Along with that, This starter deals with the following configurations

  1. Setting up the servlet context.
  2. Filter configuration.
  3. Request parsing and content negotiation.
  4. Controller mapping.

You can take a look at an example for this starter at the Hello World Example.

Spring Boot JPA Starter

The following dependencies will set up JPA repositories for you. The starter takes care of JPA configurations and database settings. With opinionated defaults, it also provides

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
Code language: HTML, XML (xml)

Most importantly, We haven’t specified any versions for these dependencies. Because Spring Boot takes care of it all.

With the above, You can write JPA repositories as shown below.

@Repository
public interface UserAccountRepository extends JpaRepository<UserAccount, Integer> {
    UserAccount findByUsername(String username);
}Code language: PHP (php)

JavaMail Starter

You can add mail functionality with spring-boot-starter-mail dependency. Add the following Spring Boot Starter and configure the mail properties and you are good to go.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>Code language: HTML, XML (xml)

For example, Here is how you can connect to GMAIL for sending messages.

[email protected]
spring.mail.password=*******
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.protocol=smtp
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=trueCode language: JavaScript (javascript)

Once the connection is established, You can send mails as shown below.

@Service
public class EmailService {

    private final JavaMailSender javaMailSender;

    public EmailService(JavaMailSender javaMailSender) {
        this.javaMailSender = javaMailSender;
    }

    public void sendMail() throws MessagingException {
        javax.mail.internet.MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
        helper.setSubject("Welcome " + user.getName());

        String html = "<H1>Hello World!!!</H1>";
        helper.setText(html, true);
        helper.setTo(user.getEmail());
        javaMailSender.send(mimeMessage);
    }
}Code language: PHP (php)

Conclusion

In conclusion, These are just a few examples of the Spring Boot starters. There are also third-party starters available for us to use.

Related

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *