请看例子:
applicationContext.xml
ArithmeticService.java
applicationContext.xml
<!-- 对Web包的所有类进行扫描,完成Bean创建和自动依赖注入 -->
<context:component-scan base-package="com" />
ArithmeticService.java
package com;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service("springService")
@Transactional
public class ArithmeticService {
public Integer add(Integer operand1, Integer operand2) {
// A simple arithmetic addition
return operand1 + operand2;
}
}
@Controller
public class TestVo {
@Resource(name="springService")
private ArithmeticService springService;
public void test(){
//将返回3
springService.add(1,2);
}
}
本文介绍了一个使用Spring框架的示例,展示了如何通过XML配置文件实现类的自动装配及依赖注入。具体包括<context:component-scan>标签的使用、@Service注解的应用以及@Transactional事务管理的集成。
18万+

被折叠的 条评论
为什么被折叠?



