Spring Annotation Development and MyBatis Integration Guide

This article provides a comprehensive tutorial on using Spring annotations for pure annotation‑based development, defining beans, managing bean scopes, performing various injection techniques, reading configuration properties, and integrating Spring with MyBatis, complete with code examples and best‑practice tips.

Top Architect
Top Architect
Top Architect
Spring Annotation Development and MyBatis Integration Guide

Preface

Spring 3.0 introduced a pure annotation development mode that further simplifies configuration, especially when integrating with MyBatis.

1. Annotation Development

Annotations are special interfaces extending Annotation, implemented at runtime by dynamic proxy classes. When retrieving annotations via reflection, a proxy object ( $Proxy1) is returned, and the AnnotationInvocationHandler fetches values from the memberValues map sourced from the constant pool.

2. Defining Beans with Annotations

Before annotations, beans were declared in XML, e.g., <bean id="a" class="yu7daily.Dao.Daoimpl.A"/>. After adopting annotations, the XML <bean> entry is removed and the class is marked with @Component("a").

@Component("a")
public class A implements AA {
    public void save() {
        System.out.println("book dao save ...");
    }
}

Scanning the package with <context:component-scan base-package="yu7daily.Dao"/> registers the component in the IoC container.

3. Derived Annotations

Beyond @Component, Spring provides @Controller, @Service, and @Repository to distinguish presentation, business, and data layers in MVC architecture.

4. Pure Annotation Development Mode

All configuration is moved to annotations. Use @Configuration to declare a configuration class and @ComponentScan to specify the scan path.

@Configuration
@ComponentScan("yu7daily")
public class Config { ... }

Instantiate the context with

ApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);

to obtain beans.

5. Bean Scope Configuration

Beans are singleton by default; use @Scope("prototype") to create non‑singleton beans.

@Component("a")
@Scope("prototype")
public class A implements AA { ... }

6. Annotation‑Based Injection

Use @Autowired for setter injection, @Qualifier to resolve NoUniqueBeanDefinitionException when multiple candidates exist, and @Value for simple data injection from properties files.

@Service
public class A implements AA {
    @Autowired
    private B b;
}

@Repository("b")
public class B implements BB {
    @Autowired
    @Qualifier("a")
    private AA aa;
}

7. Reading Properties Files

Mark a configuration class with @PropertySource("test.properties") and inject values using @Value("${name}").

@Configuration
@PropertySource("classpath:jdbc.properties")
public class SpringConfig { ... }

8. Spring Integration with MyBatis

Add spring-jdbc and mybatis-spring dependencies, configure the data source, create a MyBatis configuration class to obtain SqlSessionFactory, and retrieve beans from the context to perform data operations.

ApplicationContext ACC = new AnnotationConfigApplicationContext(SpringConfig.class);
AccountService acc = ACC.getBean(AccountService.class);

The article concludes with a reminder that annotation‑based configuration dramatically reduces boilerplate compared to XML.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend DevelopmentspringMyBatisannotationsdependency-injection
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.