Grave: Context initialization failed - spring

I am creating a HelloWorld web/spring application from scratch. I have followed this tutorial in order to learn how to use the mvc pattern. So, after finished all steps and starts to run the application I received this error in console:
Grave: Context initialization failed
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:
Line 11 in XML document from ServletContext resource
[/WEB-INF/dispatcher-servlet.xml] is invalid; nested exception is
org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 100;
cvc-complex-type.2.4.c
Searching around SO question I found some threat like below but have not resolved my error.
Cannot find the declaration of element 'beans' in internet offline mode
Spring Security beginner's question. Build failed
So, I deduced that could be the dispatcher-servlet.xml at WEB-INF folder. This xml looks like:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors">
<list>
<ref local="localeChangeInterceptor" />
</list>
</property>
<property name="urlMap">
<map>
<entry key="/hello.html">
<ref bean="helloController" />
</entry>
</map>
</property>
</bean>
<bean id="helloController" class="controllers.HelloController">
</bean>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="hl" />
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
</bean>
</beans>
Finally these are my included jars:
commons-logging-1.1.1.jar
org.springframework.aop-3.1.2.RELEASE.jar
org.springframework.asm-3.1.2.RELEASE.jar
org.springframework.aspects-3.1.2.RELEASE.jar
org.springframework.beans-3.1.2.RELEASE.jar
org.springframework.context-3.1.2.RELEASE.jar
org.springframework.context.support-3.1.2.RELEASE.jar
org.springframework.core-3.1.2.RELEASE.jar
org.springframework.expression-3.1.2.RELEASE.jar
org.springframework.instrument-3.1.2.RELEASE.jar
org.springframework.instrument.tomcat-3.1.2.RELEASE.jar
org.springframework.jdbc-3.1.2.RELEASE.jar
org.springframework.jms-3.1.2.RELEASE.jar
org.springframework.orm-3.1.2.RELEASE.jar
org.springframework.oxm-3.1.2.RELEASE.jar
org.springframework.spring-library-3.1.2.RELEASE.libd
org.springframework.test-3.1.2.RELEASE.jar
org.springframework.transaction-3.1.2.RELEASE.jar
org.springframework.web-3.1.2.RELEASE.jar
org.springframework.web.portlet-3.1.2.RELEASE.jar
org.springframework.web.servlet-3.1.2.RELEASE.jar
org.springframework.web.struts-3.1.2.RELEASE.jar
spring-webmvc-3.0.5.RELEASE.jar
Thanks in advance
EDIT 1:
After make the changes of #Biju Kunjummen, looks like now the problem is that there is a comflic in bean declaration:
Grave: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'urlMapping' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Cannot resolve reference to bean 'helloController' while setting bean property 'urlMap' with key [TypedStringValue: value [/hello.html], target type [null]]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloController' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [controllers.HelloController]: Constructor threw exception; nested exception is java.lang.Error: Unresolved compilation problems:
The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files
Implicit super constructor Object() is undefined for default constructor. Must define an explicit constructor
String cannot be resolved to a type
I have checked the controller and looks fine into src/controllers dir:
package controllers;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class HelloController implements Controller {
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String Mess = "Hello World!";
ModelAndView modelAndView = new ModelAndView("hello");
modelAndView.addObject("message", Mess);
return modelAndView;
}
}

You have the http://www.springframework.org/schema/mvc as the default, but your bean definitions belong to the http://www.springframework.org/schema/beans namespace, that should be the issue.
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
...">
<beans:bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix">
<beans:value>/WEB-INF/jsp/</beans:value>
</beans:property>
.....
Or make beans namespace the default this way:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

Related

Spring MVC 3.0.5.RELEASE get bean from the controller without scan

I have defined this bean:
#org.springframework.stereotype.Service(value = "deviceService")
public class DeviceServiceImpl implements DeviceService {
...
}
and this config file
<?xml version='1.0' encoding='utf-8'?>
<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 name="/hello" class="com.tdk.DeviceController"></bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
Is there a way to get the deviceService bean from the controller without scanning ?
Without Component Scan deviceService will never be loaded in application context , and you can't inject a bean which dosen't even exist.
You already have a dispatcher-servlet.xml so you can use xml based approach to define deviceService bean.
<bean name="deviceService" class="com.*.DeviceServiceImpl"></bean>
and then use property injection for your controller
<bean name="/hello" class="com.tdk.DeviceController">
<property name="deviceService" ref="deviceService" />
</bean>
Hope , this will help.

Could not convert constructor argument value of type [java.util.ArrayList] to required type [java.util.List]

I have a spring xml file like this named "request-details-upload.xml"
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
">
<import resource="classpath:META-INF/jobs/environment.xml" />
<import resource="classpath:META-INF/jobs/clients.xml" />
<import resource="classpath:META-INF/jobs/edx/request-details.xml" />
<import resource="classpath:META-INF/jobs/dynamoDbClients.xml" />
<bean id="requestDetailsFetchAndDecryptDataDao" class="com.amazon.edx.dao.FetchAndDecryptDataDaoDynamoDbImpl"
c:dataUploadDao-ref="requestDetailsDataUploadDao"
c:dataTransformer-ref="requestDetailsDataTransformer"
/>
<util:list id="requestDetailsKeyItemAttributesMetadata" value-type="com.amazon.edx.manager.ItemAttributesMetaData">
<ref bean="RequestId"/>
<ref bean="RequestDate"/>
</util:list>
<util:list id="requestDetailsNonKeyItemAttributesMetadata" value-type="com.amazon.edx.manager.ItemAttributesMetaData">
<ref bean="CreatedBy"/>
<ref bean="UpdatedTime"/>
</util:list>
<bean id="RequestId" class="com.amazon.edx.manager.ItemAttributesMetaData"
c:itemAttributeName="RequestId"
/>
<bean id="RequestDate" class="com.amazon.edx.manager.ItemAttributesMetaData"
c:itemAttributeName="RequestDate"
/>
<bean id="CreatedBy" class="com.amazon.edx.manager.ItemAttributesMetaData"
c:itemAttributeName="CreatedBy"
/>
<bean id="UpdatedTime" class="com.amazon.edx.manager.ItemAttributesMetaData"
c:itemAttributeName="UpdatedTime"
/>
<!-- upload manager -->
<bean id = "requestDetailsDataUploadManager" class ="com.amazon.edx.manager.DataUploadManagerImpl"
c:fetchAndDecryptDataDao-ref="requestDetailsFetchAndDecryptDataDao"
c:keyAttributes-ref="requestDetailsKeyItemAttributesMetadata"
c:nonKeyAttributes-ref="requestDetailsNonKeyItemAttributesMetadata"
/>
<bean id = "requestDetailsUploadToEdx" class = "com.amazon.edx.UploadDataToEdx"
c:dataUploadManager-ref ="requestDetailsDataUploadManager"
/>
</beans>
and another xml named request-details.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
">
<bean id="requestDetailsDataUploadDao" class="com.amazon.edx.dao.DataUploadClientImpl"
c:someName="xxxxxxxxxx"
c:otherName="yyyyyyyyy"
/>
<util:list id="requestDetailsColumnMetaData" value-type="com.amazon.edx.transformer.ColumnMetaData">
<ref bean="RequestId"/>
<ref bean="RequestDate"/>
<ref bean="CreatedBy"/>
<ref bean="UpdatedTime"/>
</util:list>
<bean id="RequestId" class="com.amazon.edx.transformer.ColumnMetaData"
c:attributeName="RequestId"
c:dataType="VARCHAR2"
c:columnDisplayName="REQUEST_ID"
/>
<bean id="RequestDate" class="com.amazon.edx.transformer.ColumnMetaData"
c:attributeName="RequestDate"
c:dataType="VARCHAR2"
c:columnDisplayName="REQUEST_DATE"
/>
<bean id="CreatedBy" class="com.amazon.edx.transformer.ColumnMetaData"
c:attributeName="CreatedBy"
c:dataType="VARCHAR2"
c:columnDisplayName="CREATED_BY"
/>
<bean id="UpdatedTime" class="com.amazon.edx.transformer.ColumnMetaData"
c:attributeName="UpdatedTime"
c:dataType="NUMBER"
c:columnDisplayName="UPDATED_TIME"
/>
<bean id="requestDetailsDataTransformer" class="com.amazon.edx.transformer.DataTransformerImpl"
c:dataFlattener-ref="requestDetailsDataFlattener"
c:columnMetadata-ref="requestDetailsColumnMetaData"
c:delimiter="{tabDelimiter}"
/>
<util:constant id="tabDelimiter"
static-field="com.amazon.edx.transformer.Delimiters.TAB_DELIMITER" />
<bean id="requestDetailsDataFlattener" class="com.amazon.edx.flattener.JsonDataFlattenerImpl"
c:multipleRowColumnName=""
/>
</beans>
I am getting the following error:
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'requestDetailsDataTransformer' defined in class path resource [META-INF/jobs/edx/request-details.xml]: Unsatisfied dependency expressed through constructor argument with index 1 of type [java.util.List]: Could not convert constructor argument value of type [java.util.ArrayList] to required type [java.util.List]: Failed to convert value of type 'java.util.ArrayList' to required type 'java.util.List'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.amazon.edx.manager.ItemAttributesMetaData] to required type [com.amazon.edx.transformer.ColumnMetaData]: no matching editors or conversion strategy found
It says cannot convert from java.util.ArrayList to java.util.List. I am not even using java.util.ArrayList. I am new to Spring and not able to understand the exact cause of this error.
Any help is appreciated.
Thanks
Ahh...figured out the issue. There is a conflict in the bean ids present in the lists in both the files. Bean id's should be unique even if they are in different .xml files. There would be a conflict when we import one file into other.
If you use List type dependency, since it is a interface, spring container create an object of its implementation class ArrayList.
For Set type dependency, spring container creates an object of its implementation class LinkedHashSet.
For Map type dependency, spring container creates an object of its implementation class LinkedHashMap.
For Properties type dependency, since it is a class, spring container creates an object of class Properties only.

Why does adding #Transactional causes exception org.springframework.beans.factory.BeanCreationException: Error creating bean with name?

I am learning Spring with Hibernate and I am playing a bit with my configuration to fully understand how it works. An answer to this will be quite helpful to understand what is going on. I have quite a perplexity about adding #Transactional to my #Repository object. When I add it I get the error:
WARNING: Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'soccerController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.edu.spring.soccer.service.AccountService com.edu.spring.soccer.controller.SoccerController.accountService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.edu.spring.soccer.dao.AccountDaoImpl com.edu.spring.soccer.service.AccountService.accountDaoImpl; nested exception is java.lang.IllegalArgumentException: Can not set com.edu.spring.soccer.dao.AccountDaoImpl field com.edu.spring.soccer.service.AccountService.accountDaoImpl to com.sun.proxy.$Proxy63
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:298)
However when I delete the #Transactional everything works properly. I have read in several places that #Repository should be followed by #Transactional.
Following you see my configuration. This is the #Repository object with #Transactional. Note that extends an AbstractDao class that contains SessionFactory object.
#Repository
#Transactional //It causes the exception, without it, it works.
public class AccountDaoImpl extends AbstractDao implements AccountDao {
public void saveAccount(Account account, String password) {
//Both methods come from AbstractDao
persist(account);
insertPassword(account, password);
}
}
However when I delete the #Transactional everything works properly. I have read in several places that #Repository should be followed by #Transactional.
Following you see my configuration:
beans-data.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<context:property-placeholder location="/WEB-INF/spring/environment.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" p:driverClassName="${dataSource.driverClassName}"
p:url="${dataSource.url}" p:username="${dataSource.username}"
p:password="${dataSource.password}" />
<!-- Taken from http://websystique.com/spring/spring4-hibernate4-mysql-maven-integration-example-using-annotations/ -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.edu.spring.soccer.domain</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql:false}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql:false}</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven />
<context:component-scan base-package="com.edu.spring.soccer.dao" />
</beans>
dispatcher-servlet.xml
I am doing the component-scan to all my packages and adding annotation-driven.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<tx:annotation-driven />
<context:component-scan base-package="com.edu.spring.soccer.controller" />
<context:component-scan base-package="com.edu.spring.soccer.dao" />
<context:component-scan base-package="com.edu.spring.soccer.domain" />
<context:component-scan base-package="com.edu.spring.soccer.service" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
p:basename="classpath:/messages" />
</beans>
Is it maybe related to having the component-scan and <tx:annotation-driven /> in the dispatcher-servlet.xml and then adding again #Transactional?.Maybe this causes some kind of redundancy.
The exception stack says it all :
nested exception is java.lang.IllegalArgumentException: Can not set com.edu.spring.soccer.dao.AccountDaoImpl field com.edu.spring.soccer.service.AccountService.accountDaoImpl to com.sun.proxy.$Proxy63
you are trying here to autowire AccountDaoImpl in your AccountService
But
AccountDaoImpl has that #Transactionnal annotation, so Spring has "proxied" it... You should autowire not the implementation but the interface, that is to say, in your AccountService change:
private AccountDaoImpl accountDaoImpl;
to
private AccountDao accountDao;
I believe it is not because of tx:annotation-driven & #Transactional. Whether in Hibernate or not, I m quite sure #Transactional have to be used at Service layer not #Repository Dao layer. What if you need to handle a transaction that spans across multiple Daos? Dao should be just cohesive components and transaction logic should reside in Service layer.
Pls see
http://www.byteslounge.com/tutorials/spring-with-hibernate-persistence-and-transactions-example
But I still see posts where #Transactional is used in Dao layer, and I disagree with it. But if u insist to use #Transactional and #Repository, maybe try put #Transactional on Dao method(s) saveAccount?

Spring: No default constructor found

My english isn't perfect or even good but I will try to explain my problem.
I need to write in my application REST client which is using Wiki API to get some necessary data.I have no idea what is wrong with this code.
Here is my client:
#Component("wikiService")
public class WikiServiceImpl implements WikiService {
private Log log = LogFactory.getLog(WikiServiceImpl.class);
private String wikiBaseURL = "http://pl.wikipedia.org/w/api.php?action=query&prop=revisions&titles={name}&rvprop=content&";
private final RestTemplate restTemplate;
public WikiServiceImpl() {
restTemplate = new RestTemplate();
}
#Autowired
public WikiServiceImpl(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
// other metods ommited
Controller:
#Controller
#RequestMapping(value="/ask")
public class WikiController {
final Logger logger= LoggerFactory.getLogger(WikiController.class);
#Autowired
private WikiService wikiService;
#RequestMapping(value="/{query}", method=RequestMethod.GET)
#ResponseBody
public String getAnswer(#PathVariable String query) {
logger.info("Odebralem zapytanie: " + query);
String bDate = wikiService.getBirthDate(query);
logger.info("Znaleziona data: " + bDate);
return bDate;
}
public final WikiService getWikiService() {
return wikiService;
}
public final void setWikiService(WikiService wikiService) {
this.wikiService = wikiService;
}
}
Here is rest servlet context:
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/oxm
http://www.springframework.org/schema/oxm/spring-oxm.xsd">
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
<bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="castorMarshaller"/>
<property name="unmarshaller" ref="castorMarshaller"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<context:annotation-config/>
<context:component-scan base-package="com.web.restful.controller"/>
<bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller">
<property name="mappingLocation" value="classpath:oxm-mapping.xml"/>
</bean>
</beans>
Root context:
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:META-INF/schema.sql"/>
<jdbc:script location="classpath:META-INF/test-data.sql"/>
</jdbc:embedded-database>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<context:annotation-config/>
<context:component-scan base-package="com.service.jpa, com.wiki.service" />
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="packagesToScan" value="com.kr.soft.domain"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.H2Dialect
</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
</beans>
Error stack:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'wikiService' defined in file [/home/user/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/MyApp/WEB-INF/classes/com/wiki/service/WikiServiceImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.wiki.service.WikiServiceImpl]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.wiki.service.WikiServiceImpl.<init>()
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:997)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:943)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4791)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.wiki.service.WikiServiceImpl]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.wiki.service.WikiServiceImpl.<init>()
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:72)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:990)
... 23 more
Stack says container can't find default constructor which actually is there.
Application is running on TomCat 7.0 Server.
Thanks for help.
As the error is impossible with the source code you shared with us, my guess would be that your server contains old class (i.e. the source code we see is not being used).
I suggest to clean the project and also Tomcat installation in your IDE.
On project in Eclipse (if that is your IDE):
Menu Project => Clean ...
On Tomcat in Eclipse (if that is your IDE):
Right click on the server in Servers view => Clean ...

Spring 3 newbie - NoSuchBeanDefinitionException - what have I missed?

Just started looking at spring 3 and MVC. I get the following exception...
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'registerController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private browniePoints.dao.UserDao browniePoints.web.RegisterController.userDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [browniePoints.dao.UserDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
From all the examples I have google this is all that is needed. What have I missed?
The interface...
package browniePoints.dao;
public interface UserDao
{
The implementation...
package browniePoints.dao.impl;
#Component
public class UserDaoImpl implements UserDao
{
#Autowired
private DataSource dataSource;
The controller...
package browniePoints.web;
#Controller
public class RegisterController
{
#Autowired
private UserDao userDao;
web.xml has one servlet that points to following xml config...
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
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">
<!-- Scans the classpath of this application for #Components to deploy as beans -->
<context:component-scan base-package="browniePoints.web" />
<!-- Configures the #Controller programming model -->
<mvc:annotation-driven />
<!-- Forwards requests to the "/" resource to the "index" view -->
<mvc:view-controller path="/" view-name="index"/>
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<!-- point URLs ending .jsp to views in /WEB-INF/views -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"> </property>
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- load properties from config.properites -->
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="config.properties" />
</bean>
<!-- Define a SQL Server datasource -->
<bean id="dataSource" destroy-method="close" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
</beans>
Thanks.
Shot in the dark; you are missing the package of UserDaoImpl in your component-scan directive:
<context:component-scan base-package="x.z"/>

Resources