spring零配置(Annotation)學習筆記
有關spring的註解,今天瞭解了下,現在一方面做下學習總結,另一方面給學習的筒子做個借鑑。
spring提供相關的幾個Annotation來標註bean先列出來
@Component:標註一個普通的spring bean
@Controller:標註一個控制器元件類如action
@Service:標註一個邏輯控制類如Service層
@Repository:標註一個持久層Dao元件類
再列幾個
@Scope:相信大家對這個不陌生吧,表示bean的作用域,使用方式:Scope("prototype")
@Resource:配合依賴,使用方式:Resource(name="XXXX")等同於xml中的配置<property …… ref="XXXX" />
@Autowired:自動裝配,預設按照type裝配,如果需要按照name裝配就需要和下面的相結合了
@Qualifier
針對自動裝配下面展示兩種寫法分別表示屬性修飾和set方式修飾:
@Autowried
@Qualifier("XXXX")
private XXXX xxxx;
-----------------------------------------------------
@Autowried
public void setXXX(@Qualifier("xxxx") XXX xxx){}
基本常用的註解也就上面的了,現在貼上程式碼:
要想讓註解生效,首先要在配置檔案中指明掃描那些包下的Bean類,
包結構:
cn.life.routine
-action
-dao
-service
引入ContextSchema,spring配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
>
<!-- action暫未用註解 -->
<bean id="NoticeAction" class="cn.life.routine.action.NoticeAction" scope="prototype" ></bean>
<!-- 註解測試,routine -->
<context:component-scan base-package="cn.life.routine"></context:component-scan>
</beans>
現在依次貼出service,dao
service介面
/**
* 註解
* @author Francis.Hu
* @createDate Oct 21, 2012
*/
public interface TestService {
/**
* 註解測試
* @return
*/
public String getTestAnnotation();
}
service實現類
package cn.life.routine.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import cn.life.routine.dao.TestDao;
/**
* 註解測試
* @author Francis.Hu
* @createDate Oct 21, 2012
*/
@Service("testService")
public class TestServiceImp implements TestService{
/**
* 自動裝配
*/
@Autowired
@Qualifier("testDao")
//@Resource(name="testDao"), 等價於<property ………… ref="testDao" />
private TestDao testDao;
public String getTestAnnotation() {
return testDao.getTestDaoAnnotation();
}
public TestDao getTestDao() {
return testDao;
}
public void setTestDao(TestDao testDao) {
this.testDao = testDao;
}
}
dao層介面
/**
* 測試註解
* @author Francis.Hu
* @createDate Oct 21, 2012
*/
public interface TestDao {
/**
* 得到dao層註解
* @return
*/
public String getTestDaoAnnotation();
}
dao層實現類
/**
* 測試註解
* @author Francis.Hu
* @createDate Oct 21, 2012
*/
@Repository("testDao")
public class TestDaoImpl implements TestDao {
public String getTestDaoAnnotation() {
return "This is testDao Annotation";
}
}
下面是action中的呼叫
/**
* 測試註解
* @return
*/
@Resource(name="testService")
private TestService testService;
public String testAnnotation(){
String result = testService.getTestAnnotation();
System.out.println(result);
return SUCCESS;
}
public TestService getTestService() {
return testService;
}
public void setTestService(TestService testService) {
this.testService = testService;
}
/*********************測試結束******************************/
開發中沒有絕對的xml配置或者絕對的零配置 ,只有合理的相互結合才能保證專案的可讀性,高效性,個人語言組織能力有限,如果有什麼問題或者建議請發郵件至[email protected]