SpringMvc DAO接口和DAO实现的注解

9

我希望知道我是否正确地进行了这些类的注释,因为我对注释还不熟悉:

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;   

    }
}

自动装配的getter看起来肯定不对。 - Matt Whipple
1
如果国家是实体或值对象,则不应将其注释为组件。您可以将其保留为简单的未注释的 bean。 - Matt Whipple
1
你应该给实现类加上注释,无需担心接口。除非没有更好、更语义化的选项,请不要使用 component。初看起来,CountryManager 更适合被标记为 @Service。 - Matt Whipple
1
在这些类中,我只会将CountryManager声明为@Service,将JdbcCountryDAO声明为@Repository。此外,对于控制器,使用@Controller注释。坚持使用这些注释可以划分您的层并强调类的意图。 - Matt Whipple
1
不,你不应该这样做。Bean注解是用于像存储库工厂这样的东西,如果需要在创建后将它们注册到上下文中。 - Matt Whipple
显示剩余7条评论
1个回答

21

这个答案应该能够帮助你理解一些事情:Spring 中 @Component、@Repository 和 @Service 注解有什么区别?

其他需要了解的事情:

  • 你的实体和接口不需要任何注解。实际上,@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(表示层),@Service(服务层)和@Repository(数据访问层)覆盖了我主要的Spring Bean需求。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接