Mastering Spring Annotation Development: From XML to Pure Annotations and MyBatis Integration

This tutorial explains how Spring's pure annotation mode replaces XML bean definitions, covers component scanning, bean scopes, dependency injection techniques, property loading, and demonstrates seamless integration with MyBatis for streamlined backend development.

macrozheng
macrozheng
macrozheng
Mastering Spring Annotation Development: From XML to Pure Annotations and MyBatis Integration

Preface

Spring 3.0 introduced a pure annotation development mode, aiming to simplify configuration; annotations work especially well when integrating MyBatis.

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. The proxy’s invoke method looks up values in the memberValues map, which originates from the Java constant pool.

Defining Beans with Annotations

Previously beans were declared in XML:

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

With annotations, the XML entry is removed and the class is annotated with @Component:

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

Component scanning is configured in XML:

<context:component-scan base-package="yu7daily.Dao"/>

Scanning smaller packages speeds up startup; larger packages increase the scan range.

Beans are retrieved from the container:

public static void main(String[] args) {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    A aa = (A) ctx.getBean("a");
    aa.save();
}

Note: Interfaces cannot be instantiated directly; annotations must be placed on concrete classes.

Derived Annotations

@Component

has three specialized stereotypes: @Controller, @Service, @Repository, which correspond to presentation, business, and data layers in MVC.

Pure Annotation Development Mode

All configuration is done with annotations, eliminating XML. @Configuration marks a class as a Spring configuration class; @ComponentScan defines the packages to scan.

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

The container can be created with:

ApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);

Bean Scope

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

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

Annotation‑Based Injection

Field injection is achieved with @Autowired:

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

Automatic Wiring

Earlier XML could enable autowire="byType". The annotation approach achieves the same effect via reflection.

Injection by Name

When multiple beans of the same type exist, @Qualifier together with @Autowired specifies the target bean.

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

Simple Value Injection

@Value

injects literal values, typically from configuration files.

@Repository("a")
public class A implements AA {
    @Value("hello java")
    private String str;
    public void save() { System.out.println(str); }
}

Reading Properties Files

@PropertySource

on a configuration class specifies the properties file; @Value on fields injects the values.

@Configuration
@ComponentScan("yu7daily")
@PropertySource("test.properties")
public class SpringConfig { }

Example property: name=lanyangyang; injected with @Value("${name}") yields the value.

Spring Integration with MyBatis

After adding spring-jdbc and mybatis-spring dependencies, configure a DataSource, then create a configuration class that imports JDBC and MyBatis configs.

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

Obtain beans from the context to operate on the data layer:

ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
AccountService acc = ctx.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.

BackendjavaspringMyBatisannotationsdependency-injection
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

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.