作用为给bean在容器中命名,多用在dao层
定义一个接口
public interface UserRepository {
void save();
}
两个实现类
package com.proc.bean.repository;
import org.springframework.stereotype.Repository;
//将此UserRepositoryImps类在容器中命名改为userRepository
@Repository("userRepository")
public class UserRepositoryImps implements UserRepository{
@Override
public void save() {
System.out.println("UserRepositoryImps save");
}
}
package com.proc.bean.repository;
import org.springframework.stereotype.Repository;
//这个实现类则不进行命名
@Repository
public class UserJdbcImps implements UserRepository {
@Override
public void save() {
System.out.println("UserJdbcImps save");
}
}
调用接口测试:
@Autowired
private UserRepository userRepository;
//会找到我们命名为userRepository的bean,并装配到userRepository中