9

I would like to know if I am annotating these classes correctly, since I am new to the annotations:

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;   

    }
}
Matin Kh
  • 5,192
  • 6
  • 53
  • 77
devdar
  • 5,564
  • 28
  • 100
  • 153
  • The autowired getter certainly doesn't look right – Matt Whipple Sep 30 '12 at 02:34
  • 1
    country should not be annotated as a Component if it's an Entity or VO. You can leave it as a simple unannotated bean. – Matt Whipple Sep 30 '12 at 02:36
  • 1
    you should be annotating the implementation classes and you don't have to worry about the interfaces. Also avoid component unless there's not a better, more semantic option. At a glance it looks like CountryManager is more likely a @Service. – Matt Whipple Sep 30 '12 at 02:38
  • what should be used in place of component? – devdar Sep 30 '12 at 02:39
  • 1
    Out of those classes I'd only have CountryManager as \@Service and JdbcCountryDAO as @Repository. Additionally there is \@Controller for...controllers. Sticking to these will demarcate your layers and emphasize the intent of the classes. – Matt Whipple Sep 30 '12 at 02:41
  • do i still need to use the bean annotation as well? – devdar Sep 30 '12 at 02:41
  • where? your data beans don't need to be managed by the DI container in anyway. – Matt Whipple Sep 30 '12 at 02:42
  • i was wondering if there is a request to say \saveCountry would i need to annotate /@Bean on the CountryManager.java? – devdar Sep 30 '12 at 02:46
  • 1
    No, you should not. The Bean annotation is if you had something like a factory for your repositories and needed them registered in the context after they were created. – Matt Whipple Sep 30 '12 at 02:49
  • Orr ok i see, hey thanks a lot man, really think you should formular all you've said as an answer. You seem to very knowledgeable with the use of spring, many ppl said all sorts of thing when it came to annotations to me thats why i am asking agin this time with some of my code. – devdar Sep 30 '12 at 02:50
  • if i happen to call countryDAO at any time would'nt i have to \@Autowired it ? how will spring find it if the CountryDAO.java isnt annotated? – devdar Sep 30 '12 at 02:57
  • Because the implementation class (JdbcCountryDAO) is annotated it will be made available as a Spring Bean. With the default autowire behavior (by type), it will then be injected for anyone looking for the interface that it implements (CountryDAO). You don't inject interfaces (since they're not valid object types anyway), they're just used to qualify what classes will be injected through the reference type. – Matt Whipple Sep 30 '12 at 11:45

1 Answers1

21

This answer should clear things up a bit: What's the difference between @Component, @Repository & @Service annotations in Spring?

Other things you should know :

  • Your entities and interfaces don't need any annotation. Indeed, @Component and other derived annotations just mean that you are declaring a Spring bean on the fly. For example,

    @Component
    public class MyComponent { ... }
    

    will by default add a bean named "myComponent" in Spring's context. Spring beans are singletons by default, and represent real instantiated object.
    So it has no sense to declare entities or interfaces as Spring beans.

  • Managers are semantically the same as Services, so you should rather annotate them with @Service.

Here's how your code should be :

// No annotation
public class Country {

and

// No annotation
public interface CountryDAO {

and

@Repository
public class JdbcCountryDAO extends JdbcDaoSupport implements CountryDAO {

and

// No annotation
public interface CountryManagerIFace extends Serializable{

and

@Service
public class CountryManager implements CountryManagerIFace{

    @Autowired
    private CountryDAO countryDao;

Note : I rarely use @Component in my code, as @Controller (presentation layer), @Service (service layer) and @Repository (dao layer) cover my main Spring bean needs.

Community
  • 1
  • 1
Jerome Dalbert
  • 10,067
  • 6
  • 56
  • 64