java - e's Datasource Not injecting in @controller in spring-mvc 3 -
i trying develop spring-mvc 3 web application learning purpose. newbie i'am not in spring mvc.
so here problem datasource of admindao.java returns null in @controller class
@controller public class adminpanel { @autowired iadminservices admindao; @requestmapping(value={"/adminpanel.spring"},method=requestmethod.get) public string execute(@modelattribute login login){ homecoming "adminpanel"; .... }
here datasource injected null
her application context
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="datasource" class="org.springframework.jdbc.datasource.drivermanagerdatasource"> <property name="driverclassname" value="org.postgresql.driver" /> <property name="url" value="jdbc:postgresql://localhost:5432/librarysystem" /> <property name="username" value="postgres" /> <property name="password" value="postgres" /> </bean> <bean id="admindao" class="in.kailash.dao.admindao"> <property name="datasource" ref="datasource" /> </bean> </beans>
my iadminservices.java
@component public interface iadminservices { public string save(user user); public string delete(user user,string id); public string edit(user user,string id); public list<user> viewall(); public string issue(); public string returnbook(); public string getstudentcardnumber(); }
admindao class implementing iadminservices.java
@service public class admindao implements iadminservices { datasource datasource; public admindao() { super(); } public void setdatasource(datasource datasource) { this.datasource = datasource; } @override public string save(user user) { seek { connection con = datasource.getconnection(); string sql = "insert borrower(cardno,name,address,phone) values(?,?,?,?)"; ... .... }
here's mvc-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="in.kailash" /> <context:annotation-config/> <bean id="viewresolver" class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix" value="/web-inf/jsps/" /> <property name="suffix" value=".jsp"/> </bean> </beans>
here's web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns /javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="webapp_id" version="3.0"> <display-name>libsys</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>maincontroller</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>maincontroller</servlet-name> <url-pattern>*.spring</url-pattern> </servlet-mapping> <context-param> <param-name>contextconfiglocation</param-name> <param-value>/web-inf/maincontroller-servlet.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> </web-app>
my main problem datasource null in @controller class. newbie farther comments how arrange classes in spring-mvc welcomed.
thanks in advance.
@sotirios patience reply. according suggestions have done changes in
web.xml
as
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="webapp_id" version="3.0"> <display-name>libsys</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>maincontroller</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextconfiglocation</param-name> <param-value>/web-inf/maincontroller-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>maincontroller</servlet-name> <url-pattern>*.spring</url-pattern> </servlet-mapping> <context-param> <param-name>contextconfiglocation</param-name> <param-value>/web-inf/applicationcontext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener>
somehow datasource getting injected. want confirm time have done right. , if find problem in bundle arrangement please comment
i'm going assume admindao
class nested somewhere within bundle declared here
<context:component-scan base-package="in.kailash" />
if that's case, spring generate new bean type admindao
since annotated @service
. process bean, won't autowire datasource
in because haven't asked to. admindao
bean injected @controller
, not 1 application context.
instead, seems want utilize admindao
bean you've declared in application context.
you have 2 options:
remove@service
annotation admindao
class spring correctly uses admindao
<bean>
you've declared in application context. remove <bean>
declaration admindao
, add together @autowired
datasource
field or setter java spring spring-mvc
No comments:
Post a Comment