Spring Annotation-Based Development and MyBatis Integration Tutorial

This article explains how Spring 3.0's pure annotation mode simplifies bean configuration, demonstrates using @Component, @Scope, @Autowired, @Qualifier, @Value, and @PropertySource for dependency injection and property loading, and shows step‑by‑step integration of MyBatis with Spring, including required dependencies and configuration classes.

Architecture Digest
Architecture Digest
Architecture Digest
Spring Annotation-Based Development and MyBatis Integration Tutorial

Spring 3.0 introduced pure annotation development, simplifying configuration and integrating tightly with MyBatis.

Annotations are special interfaces extending Annotation; at runtime they are implemented by dynamic proxy classes. Using @Component replaces XML <bean> definitions, and <context:component-scan> can be replaced by @ComponentScan in a @Configuration class.

Bean scopes are controlled with @Scope (singleton or prototype). Dependency injection is achieved with @Autowired, optionally qualified by @Qualifier to resolve multiple candidates. Simple value injection uses @Value, and external properties are loaded with @PropertySource and injected via @Value placeholders.

For MyBatis integration, add spring-jdbc and mybatis-spring dependencies, configure a DataSource, create a configuration class annotated with @Configuration, @ComponentScan and @Import, obtain SqlSessionFactory, and retrieve mapper beans from the ApplicationContext.

All code examples are shown below:

<bean id="a" class="yu7daily.Dao.Daoimpl.A"/>
@Component("a")
public class A implements AA {
    public void save() {
        System.out.println("book dao save ...");
    }
}
<context:component-scan base-package="yu7daily.Dao"/>
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
A aa = (A) ctx.getBean("a");
bookService.save();
@Qualifier("a")
private AA aa;
@Value("${name}")
private String str;
@Configuration
@ComponentScan("yu7daily")
@PropertySource("test.properties")
public class SpringConfig { ... }
ApplicationContext ACC = new AnnotationConfigApplicationContext(SpringConfig.class);
AccountService acc = ACC.getBean(AccountService.class);
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.

BackendJavaConfigurationspringMyBatisannotationsdependency-injection
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

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.