读书人

引文注入类

发布时间: 2012-06-26 10:04:13 作者: rapoo

注解注入类
请看例子:
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")@Transactionalpublic class ArithmeticService {public Integer add(Integer operand1, Integer operand2) {// A simple arithmetic additionreturn operand1 + operand2;}}


@Controllerpublic class TestVo {@Resource(name="springService")private ArithmeticService springService;public void test(){            //将返回3            springService.add(1,2);        }}

读书人网 >开源软件

热点推荐