Spring学习笔记

1.Spring注解@Component、@Repository、@Service、@Controller区别

spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository、@Service 和 @Controller。

@Repository:持久层

@Service :业务层

@Controller:控制层(Web层)

它们和@Component是等效的。

作用:在一个稍大的项目中,如果组件采用xml的bean定义来配置,显然会增加配置文件的体积,查找以及维护起来也不太方便。 
Spring2.5为我们引入了组件自动扫描机制,他在类路径下寻找标注了上述注解的类,并把这些类纳入进spring容器中管理。
它的作用和在xml文件中使用bean节点配置组件时一样的

例:

@Service("VentorServiceImpl")  
public class VentorServiceImpl implements iVentorService {     
}  
@Repository("VentorDaoImpl")  
public class VentorDaoImpl implements iVentorDao {   
}  

如果去掉@Service、@Repository 就需要在XML文件中配置:






你可能感兴趣的:(JAVA知识)