Spring Annotation Development: From Bean Definition to MyBatis Integration

This article explains how Spring’s pure annotation development replaces XML bean definitions, covers component scanning, bean scopes, derived annotations, various injection methods, property file loading, and demonstrates seamless Spring‑MyBatis integration, showing step‑by‑step code examples that simplify backend configuration.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Spring Annotation Development: From Bean Definition to MyBatis Integration

Spring 3.0 introduced pure annotation development to simplify configuration, especially when integrating with MyBatis.

1. Annotation Development

Annotations are special interfaces extending Annotation; at runtime they are implemented by dynamic proxy classes. When retrieving an annotation via reflection, a proxy object ($Proxy1) is returned, whose invoke method in AnnotationInvocationHandler reads values from the memberValues map sourced from the constant pool.

2. Defining Beans with Annotations

Previously beans were defined in XML:

<bean id="a" class="yu7daily.Dao.Daoimpl.A"/>

With annotations, the @Component("a") annotation on the class replaces the XML definition.

Scanning packages is done with <context:component-scan base-package="yu7daily.Dao"/> or the equivalent @ComponentScan("yu7daily.Dao") in a configuration class.

3. Bean Scope

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

4. Derived Annotations

Spring provides @Controller, @Service, and @Repository as specializations of @Component to distinguish presentation, business, and data layers.

5. Pure Annotation Mode

Configuration classes are marked with @Configuration and package scanning with @ComponentScan. The XML <context:component-scan/> can be fully replaced.

6. Dependency Injection

Field injection is achieved with @Autowired. When multiple beans of the same type exist, @Qualifier("beanName") together with @Autowired specifies the target bean, avoiding NoUniqueBeanDefinitionException.

7. Simple Value Injection

Use @Value("hello java") or @Value("${property}") to inject literal strings or properties from a file.

8. Reading Properties Files

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

9. Spring‑MyBatis Integration

Add spring-jdbc and mybatis-spring dependencies, configure a data source, create a main configuration class that imports JdbcConfig and MybatisConfig, and obtain beans via AnnotationConfigApplicationContext. This eliminates most XML configuration.

The article demonstrates that pure annotation development dramatically reduces boilerplate and simplifies Spring‑MyBatis projects.

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.

JavaspringMyBatisannotationdependency-injection
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

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.