spring - No matching bean of type [com.sachin.dao.StockDao] found for dependency -
i getting error when trying use dependency injection in spring mvc.
no matching bean of type [com.sachin.dao.stockdao] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {}; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no matching bean of type [com.sachin.dao.stockdao] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {}
i think making mistake in annotations. trying inject stockdaoimpl in homcontroller.
this controller file homecontroller.java
@controller public class homecontroller { private final stockdao dao; @autowired public homecontroller(stockdao dao){ this.dao = dao; } @requestmapping(value = "/", method = requestmethod.get) public string home(locale locale, model model) { return "home"; } @requestmapping(value = "/stockgoogle/", method = requestmethod.get) public @responsebody stock stockgoogle(locale locale, model model) { //stockdaoimpl si = new stockdaoimpl(); stock s=dao.listgoogle(); system.out.println("reached here"); model.addattribute("s", s ); return s; }
i have created configuration file beanconfiguration , using create bean
@configuration public class beanconfiguration { @bean public stockdao stockdao(){ return new stockdaoimpl(); } }
i have stockdaoimpl implement data retreival.
public class stockdaoimpl implements stockdao { @override public stock listgoogle() { connection con = null; stock s = null; try { class.forname("org.postgresql.driver"); con = drivermanager .getconnection("jdbc:postgresql://localhost:5432/webapp", "postgres", "sachin"); statement stmt = con.createstatement(); resultset rs = stmt.executequery( "select * \"public\".\"historical\" " ); rs.next(); string name = rs.getstring("name"); s = new stock(name); rs.close(); stmt.close(); con.close(); } catch (exception e) { e.printstacktrace(); } return s; } }
the above implementing following interface:
public interface stockdao { public stock listgoogle(); }
am missing something. there more annotations have put. or @ other place. not sure if creating configuration class correctly. can please help?
you need point configuration class(es). java configuration class (for example, in beanconfiguration class), add:
@componentscan(basepackages="com.sachin")
Comments
Post a Comment