关于@Repository、@Service、@Controller的作用?

@Repository和@Controller、@Service、@Component的作用差不多,都是把对象交给spring管理。@Repository用在持久层的接口上,这个注解是将接口的一个实现类交给spring管理。

Spring 自 2.0 版本开始,陆续引入了一些注解用于简化 Spring 的开发。@Repository注解便属于最先引入的一批,它用于将数据访问层 (DAO 层 ) 的类标识为 Spring Bean。具体只需将该注解标注在 DAO类上即可。同时,为了让 Spring 能够扫描类路径中的类并识别出 @Repository 注解,需要在 XML 配置文件中启用Bean 的自动扫描功能,这可以通过<context:component-scan/>实现。

1 @Service(value = "userService")2 public class UserServiceImpl implements IUserService {
3     @Autowired 
4     private IUserDao userDao;

@Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 通过 @Autowired的使用来消除 set ,get方法。

<!--扫描包-->
<context:component-scan base-package="com.hibernate.* "></context:component-scan>

猜你喜欢

转载自www.cnblogs.com/xieshilin/p/12014275.html