请看例子:
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);
}
}