1、@controller 控制器(注入服務)=====控制層
用於標註控制層,相當於struts中的action層
@Controller
@RequestMapping("/trade")//請求服務的名字
public class aaaController{
@RequestMapping("/getAll")//請求服務的名字
@ResponseBody//返回響應數據
public ExecuteResult getAll() {
}
}
2、@service 服務(注入dao)====業務邏輯層
用於標註服務層,主要用來進行業務的邏輯處理
例:
@Service
public class UserServiceImpl implements UserService {
}
如果想自定義實現接口名(@repository類似):
//實現接口定義的名字---(適用於一個接口有多個實現類的情況)
@Service(“student”)
@Scope(“prototype”)
public class StudentServiceImpl implements Person {
}
@Service(“user”)
@Scope(“prototype”)
public class UserServiceImpl implements Person {
}
//調用創建對象時取到student實現類
@Autowired
@Qualifier("student")//獲取具體是哪個實現類
private Person person;
3、@repository(實現dao訪問)====持久層
用於標註數據訪問層,也可以說用於標註數據訪問組件,即DAO組件.
例:
@Repository
public class UserDaoImpl implements UserDao {
}
4、@component (把普通pojo實例化到spring容器中,相當於配置文件中的 <bean id="" class=""/>)
泛指各種組件,就是說當我們的類不屬於各種歸類的時候(不屬於@Controller、@Services等的時候),我們就可以使用@Component來標註這個類。
5.@Autowired (它可以對類成員變量、方法及構造函數進行標註,完成自動裝配的工作。)
//調用創建對象時取到student實現類
@Autowired
@Qualifier("student")
private Person person;
//代替
//private Person person = new Person;