2012-09-30 75 views
6

我想知道如果我正確標註這些類的,因爲我是新來的註釋:註解用Spr​​ingMVC用於DAO接口和DAO實現

Country.java

@Component 
public class Country { 

private int countryId; 
private String countryName; 
private String countryCode; 

/** 
* No args constructor 
*/ 
public Country() { 
} 

/** 
* @param countryId 
* @param countryName 
* @param countryCode 
*/ 
public Country(int countryId, String countryName, String countryCode) { 
    this.countryId = countryId; 
    this.countryName = countryName; 
    this.countryCode = countryCode; 
} 
    //getters and setters 

} 

CountryDAO的.java

@Repository 
public interface CountryDAO { 

    public List<Country> getCountryList(); 

    public void saveCountry(Country country); 

    public void updateCountry(Country country); 
} 

JdbcCountryDAO.java

@Component 
public class JdbcCountryDAO extends JdbcDaoSupport implements CountryDAO{ 

    private final Logger logger = Logger.getLogger(getClass()); 

    @Autowired 
    public List<Country> getCountryList() { 
     int countryId = 6; 
     String countryCode = "AI"; 
     logger.debug("In getCountryList()"); 
     String sql = "SELECT * FROM TBLCOUNTRY WHERE countryId = ? AND countryCode = ?"; 
     logger.debug("Executing getCountryList String "+sql); 

     Object[] parameters = new Object[] {countryId, countryCode}; 

     logger.info(sql); 

     //List<Country> countryList = getJdbcTemplate().query(sql,new CountryMapper()); 
     List<Country> countryList = getJdbcTemplate().query(sql, parameters,new CountryMapper()); 
     return countryList; 
    } 

CountryManagerIFace.java

@Repository 
public interface CountryManagerIFace extends Serializable{ 

    public void saveCountry(Country country); 

    public List<Country> getCountries(); 
} 

CountryManager.java

@Component 
public class CountryManager implements CountryManagerIFace{ 

    @Autowired 
    private CountryDAO countryDao; 

    public void saveCountry(Country country) { 
     countryDao.saveCountry(country); 

    } 

    public List<Country> getCountries() { 
     return countryDao.getCountryList(); 
    } 


    public void setCountryDao(CountryDAO countryDao){ 

     this.countryDao = countryDao; 

    } 
} 
+0

自動佈線的吸氣劑肯定看起來不對 –

+1

如果國家不是實體或VO,則不應該將其註釋爲組件。你可以把它作爲一個簡單的unannotated bean。 –

+1

你應該註釋實現類,你不必擔心接口。除非沒有更好的語義選項,否則也要避免組件。乍一看,它看起來像CountryManager更可能是@Service。 –

回答

18

這個答案應該清楚了一點東西:What's the difference between @Component, @Repository & @Service annotations in Spring?

其他的事情你應該知道:

  • 你的實體和接口不需要任何註釋。的確,@Component和其他派生的註釋只是表示你正在動態聲明一個Spring bean。例如,

    @Component 
    public class MyComponent { ... } 
    

    默認情況下會在Spring的上下文中添加一個名爲「myComponent」的bean。 Spring bean默認情況下是單例,並且代表真正的實例化對象。
    因此,將實體或接口聲明爲Spring bean是沒有意義的。

  • 經理在語義上與服務相同,因此您應該使用@Service對其進行註釋。

這裏是你的代碼應該是這樣:

// No annotation 
public class Country { 

// No annotation 
public interface CountryDAO { 

@Repository 
public class JdbcCountryDAO extends JdbcDaoSupport implements CountryDAO { 

// No annotation 
public interface CountryManagerIFace extends Serializable{ 

@Service 
public class CountryManager implements CountryManagerIFace{ 

    @Autowired 
    private CountryDAO countryDao; 

注:我在我的代碼很少使用@Component,爲@Controller(表示層),@服務(服務層)和@Repository(DAO層)覆蓋我的主要的Spring bean需求。

+1

非常感謝你這很好解釋 – devdar