【问题标题】:SpringMvc Annotations for DAO interface and DAO implementationDAO 接口和 DAO 实现的 SpringMvc 注解
【发布时间】:2012-09-21 09:48:21
【问题描述】:

我想知道我是否正确地注释了这些类,因为我是新的注释:

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 肯定看起来不正确
  • country 如果是 Entity 或 VO,则不应注解为 Component。您可以将其保留为简单的未注释 bean。
  • 您应该对实现类进行注释,而不必担心接口。除非没有更好、更语义化的选项,否则也要避免使用组件。乍一看,CountryManager 更像是一个@Service。
  • 在这些类中,我只有 CountryManager 作为\@Service,JdbcCountryDAO 作为@Repository。此外还有用于...控制器的 \@Controller。坚持这些将划分您的层并强调类的意图。
  • 不,你不应该。 Bean 注解是如果您有类似工厂的存储库,并且需要在创建它们后在上下文中注册它们。

标签: java spring-mvc annotations


【解决方案1】:

这个答案应该可以澄清一点: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(表示层)、@Service(服务层)和@Repository(dao 层)涵盖了我主要的 Spring bean 需求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-04
    • 2016-01-26
    • 1970-01-01
    • 2020-06-25
    • 2018-11-18
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多