Git Product home page Git Product logo

add-bean-to-spring-context-using-stereotype-annotation's Introduction

Add bean to Spring context using stereotype annotation

This project is based on chapter 2.2.2. Using stereotype annotations to add beans to the Spring context from book Spring Starts here (2021) by Laurentiu Spilca.

Create Maven project with Intellij Idea

File > New project > Java

Add Spring Context dependency

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>6.1.10</version>
</dependency>

Create entity

public class Book {
    private final String title;

    public Book() {
        this.title = "One Hundred Years of Solitude";
    }

    public String getTitle() {
        return title;
    }
}

Mark entity with @Component annotation

+ @Component
public class Book {
    private final String title;

    public Book() {
        this.title = "One Hundred Years of Solitude";
    }

    public String getTitle() {
        return title;
    }
}

Create configuration class

@Configuration
public class ApplicationConfiguration {
}

Configure component scan in configuration class

@Configuration
+ @ComponentScan(basePackages = "org.example")
public class ApplicationConfiguration {
}

Create Spring context

ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfiguration.class);

Get bean from Spring context

Book book = context.getBean(Book.class);

Add tests

Add dependency for Spring TestContext Framework

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>6.1.10</version>
    <scope>test</scope>
</dependency>

Add dependency for JUnit

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.11.0-M2</version>
    <scope>test</scope>
</dependency>

Create test to check that application context is created

public class ApplicationTests {

    @Test
    @DisplayName("Checks that Application Context is created")
    public void checkApplicationContextCreated() {
        ApplicationContext context = new AnnotationConfigApplicationContext();

        assertNotNull(context);
    }
}

Create test to check that book is added to Spring context

  • use @ExtendWith(SpringExtension.class) to integrate Spring TestContext Framework to the test
  • use @ContextConfiguration to configure Spring context in Spring integration tests
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = { ApplicationConfiguration.class })
public class BookTests {

    @Autowired
    private Book book;

    @Test
    @DisplayName("Fetch the book bean from the context")
    public void fetchBookBean() {
        String actualTitle = book.getTitle();
        String expectedTitle = "One Hundred Years of Solitude";

        assertEquals(actualTitle, expectedTitle);
    }
}

add-bean-to-spring-context-using-stereotype-annotation's People

Contributors

romach avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.