Comprehensive Guide to Spring Annotation Development and MyBatis Integration

This article provides a step‑by‑step tutorial on using Spring annotations for bean definition, component scanning, scope management, autowiring, qualifier usage, simple value injection, property file loading, and finally demonstrates how to integrate Spring with MyBatis for streamlined data access in Java backend projects.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Comprehensive Guide to Spring Annotation Development and MyBatis Integration

Spring 3.0 introduced a pure annotation development model that simplifies configuration by replacing XML bean definitions with annotations such as @Component, @Configuration, @Bean, and others.

1. Annotation Development – Annotations are special interfaces extending Annotation, implemented at runtime by dynamic proxy classes. Accessing them via reflection returns a proxy whose invoke method reads values from the memberValues map sourced from the constant pool.

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

3. Derived Annotations – Spring provides @Controller, @Service, and @Repository to differentiate presentation, business, and data layers, complementing the MVC pattern.

4. Pure Annotation Development Mode – All configuration is expressed via annotations. Example configuration class:

@Configuration<br/>@ComponentScan("yu7daily")<br/>public class Config {...}

Bean scope can be controlled with @Scope("prototype") to create non‑singleton instances.

5. Injection Techniques

Automatic wiring with @Autowired (by type).

Qualified injection using @Qualifier("a") together with @Autowired to resolve NoUniqueBeanDefinitionException when multiple beans of the same type exist.

Simple value injection with @Value("hello java") for primitive data, typically used to read configuration properties.

6. Loading Properties Files – Annotate a configuration class with @PropertySource("test.properties") and inject values using @Value("${name}").

7. Spring‑MyBatis Integration – After adding spring-jdbc and mybatis-spring dependencies, configure a data source, create a MyBatis configuration class to obtain a SqlSessionFactory, and import both configurations:

@Configuration<br/>@ComponentScan("yu7daily")<br/>@PropertySource("classpath:jdbc.properties")<br/>@Import({JdbcConfig.class, MybatisConfig.class})<br/>public class SpringConfig {...}

Finally, obtain beans from the application context:

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

The article concludes with a reminder to share the content and join the community for further learning.

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.

ConfigurationspringMyBatisannotationsdependency-injection
Java Architect Essentials
Written by

Java Architect Essentials

Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow 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.