spring注解和组件扫描配置


spring的部分bean在xml中配置,但我觉得action,service,dao这些完全没必要在xml中配置,一来如果IDE支持不好容易配置错误,二来这些类多起来后简直就没办法看。故采用注解配置这三层,基本配置配好后可以不动xml配置文件了。

一、首先添加注解和组件扫描配置。在spring配置文件中加入:

<!-- 注解支持 -->
<context:annotation-config/>
<!-- 组件扫描com.test目录下的所有文件 -->
<context:component-scan base-package="com.test"/>


二、在action,service,dao这三层的实现类中分别对应注解Controller,Service,Repository如果你不想逻辑上有所区分就直接用Component。

代码类似于:
@Controller
public class myAction extends BaseAction{
    ......
}

@Service
public class myService extends BaseService{
    ......
}

@Repository
public class myDao extends BaseDao{
    ......
}

@Component
public class myXXX extends BaseXXX{
    ......
}

猜你喜欢

转载自clojure.iteye.com/blog/1102701