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.
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);Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
