Master Spring Annotation Development: From XML Beans to Pure Annotations
This article explains how Spring 3.0's pure annotation mode simplifies bean configuration, demonstrates replacing XML with @Component and @Configuration, covers bean scopes, dependency injection techniques like @Autowired, @Qualifier, @Value, and shows seamless integration of Spring with MyBatis for data access.
Preface
Spring 3.0 introduced a pure annotation development mode that further simplifies development, especially when integrating MyBatis.
Annotation Development
Annotations are special interfaces extending Annotation, implemented at runtime by dynamic proxy classes. When retrieving annotations via reflection, a proxy object ($Proxy1) is returned, and its invoke method reads values from the memberValues map sourced from the constant pool.
Defining Beans with Annotations
Before annotations, beans were defined in XML:
<bean id="a" class="yu7daily.Dao.Daoimpl.A"/>After switching to annotations, remove the XML <bean> tag and annotate the class with @Component("a"):
@Component("a")
public class A implements AA {
public void save() {
System.out.println("book dao save ...");
}
}Enable component scanning in XML:
<context:component-scan base-package="yu7daily.Dao"/>Scanning a broader package increases the scan range but may reduce speed.
Retrieve beans directly from the container:
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
A aa = (A) ctx.getBean("a");
bookService.save();Note: Do not place annotations on interfaces because they cannot be instantiated.
Derived Annotations
Beyond @Component, Spring provides three derived annotations: @Controller, @Service, and @Repository, which help distinguish presentation, business, and data layers.
Pure Annotation Development Mode
All configuration is done with annotations, eliminating XML.
Use @Configuration to mark a class as a Spring configuration class and @ComponentScan to specify the package to scan:
@Configuration
@ComponentScan("yu7daily")
public class Config { ... }Instantiate the container with the configuration class:
ApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);Bean scope defaults to singleton; use @Scope("prototype") for non‑singleton beans:
@Component("a")
@Scope("prototype")
public class A implements AA { ... }Annotation‑Based Dependency Injection
Field injection with @Autowired:
@Service
public class A implements AA {
@Autowired
private B b;
}When multiple beans of the same type exist, use @Qualifier together with @Autowired to specify the bean name:
@Repository("b")
public class B implements BB {
@Autowired
@Qualifier("a")
private AA aa;
public void save() { aa.save(); }
}@Qualifiermust be used with @Autowired.
Simple Value Injection
Inject simple data with @Value:
@Repository("a")
public class A implements AA {
@Value("hello java")
private String str;
public void save() { System.out.println(str); }
}
// Output: helloTypically @Value reads values from configuration files.
Reading Properties Files
Mark a configuration class with @PropertySource to specify the properties file, then use @Value on fields:
@Configuration
@ComponentScan("yu7daily")
@PropertySource("test.properties")
public class SpringConfig { }
@Repository("a")
public class A implements AA {
@Value("${name}")
private String str;
public void save() { System.out.println(str); }
}
// Assuming test.properties contains name=lanyangyang, output: lanyangyangThe same approach applies to reading database configuration.
Spring Integration with MyBatis
After adding spring-jdbc and mybatis-spring dependencies, configure the data source, create a main configuration class that imports JDBC and MyBatis configs, and obtain beans from the container:
@Configuration
@ComponentScan("yu7daily")
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class, MybatisConfig.class})
public class SpringConfig { ... } ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
AccountService acc = ctx.getBean(AccountService.class);This reduces the amount of configuration compared to traditional XML setup.
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.
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.
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.
