I have seen the same question in SO but coudn't figure what's going on with my configuration.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.mmm.service.ReadPropertyFilesTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.mmm.singletons.VersionHandler com.mmm.service.ReadPropertyFilesTest.vHandler; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.mmm.singletons.VersionHandler] 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)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:384)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:331)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:213)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:290)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:292)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:233)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:176)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
This is the test class I use.
package com.mmm.service;
#Configurable
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = {
"classpath*:spring/applicationContext.xml",
"classpath*:spring/applicationContext-jpa.xml",
"classpath*:spring/applicationContext-security.xml" })
public class ReadPropertyFilesTest {
#Autowired
private VersionHandler vHandler;
#Test
public void readPropertyFilestest() {
//VersionHandler vc = new Versi
System.out.println("open mapping \t"+vHandler.isIS_OPEN_MAPPING());
}
}
VersionHandler class
package com.mmm.singletons;
#Service
#Configurable
public class VersionHandler {
#Value(value = "${IS_OPEN_MAPPING}")
private boolean IS_OPEN_MAPPING;
public boolean isIS_OPEN_MAPPING() {
return IS_OPEN_MAPPING;
}
public void setIS_OPEN_MAPPING(boolean iS_OPEN_MAPPING) {
IS_OPEN_MAPPING = iS_OPEN_MAPPING;
}
}
applicationcontext xml file
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-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/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<context:property-placeholder location="classpath*:META-INF/spring/*.properties" />
<aop:aspectj-autoproxy />
<context:spring-configured />
<context:component-scan base-package="com.mmm">
<context:include-filter expression=".*_Roo_.*"
type="regex" />
<context:include-filter expression="org.springframework.stereotype.Controller"
type="annotation" />
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Component" />
</context:component-scan>
<bean class="org.springframework.orm.jpa.JpaTransactionManager"
id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven mode="aspectj"
transaction-manager="transactionManager" />
<!-- Logging Aspect Bean Definition -->
<bean id="logAspect" class="com.mmm.aspect.LoggingAspect" />
<bean
class="org.springframework.transaction.aspectj.AnnotationTransactionAspect"
factory-method="aspectOf">
<property name="transactionManager" ref="transactionManager" />
</bean>
</beans>
Can somebody give me advise how to fix this?
Update
webmvc-context.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
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.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">
<context:property-placeholder location="classpath*:META-INF/spring/*.properties" />
<!-- The controllers are autodetected POJOs labeled with the #Controller
annotation. -->
<context:component-scan base-package="com.mmm.webapi.controllers"
use-default-filters="false">
<context:include-filter expression="org.springframework.stereotype.Controller"
type="annotation" />
</context:component-scan>
<!-- Turns on support for mapping requests to Spring MVC #Controller methods
Also registers default Formatters and Validators for use across all #Controllers -->
<mvc:annotation-driven conversion-service="applicationConversionService">
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean
class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="applicationObjectMapper" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<bean id="applicationObjectMapper" class="com.mmm.webapi.conversion.ApplicationObjectMapper" />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources -->
<mvc:resources location="/, classpath:/META-INF/web-resources/"
mapping="/resources/**" />
<!-- Allows for mapping the DispatcherServlet to "/" by forwarding static
resource requests to the container's default Servlet -->
<mvc:default-servlet-handler />
<!-- Register "global" interceptor beans to apply to all registered HandlerMappings -->
<mvc:interceptors>
<bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor" />
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
p:paramName="lang" />
</mvc:interceptors>
<!-- Selects a static view for rendering without the need for an explicit
controller -->
<mvc:view-controller path="/" view-name="index" />
<mvc:view-controller path="/uncaughtException" />
<mvc:view-controller path="/resourceNotFound" />
<mvc:view-controller path="/dataAccessFailure" />
<!-- Resolves localized messages*.properties and application.properties
files in the application to allow for internationalization. The messages*.properties
files translate Roo generated messages which are part of the admin interface,
the application.properties resource bundle localizes all application specific
messages such as entity names and menu items. -->
<bean
class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
id="messageSource" p:basenames="WEB-INF/i18n/messages,WEB-INF/i18n/application"
p:fallbackToSystemLocale="false" />
<!-- Store preferred language configuration in a cookie -->
<bean class="org.springframework.web.servlet.i18n.CookieLocaleResolver"
id="localeResolver" p:cookieName="locale" />
<!-- Resolves localized <theme_name>.properties files in the classpath to
allow for theme support -->
<bean
class="org.springframework.ui.context.support.ResourceBundleThemeSource"
id="themeSource" />
<!-- Store preferred theme configuration in a cookie -->
<bean class="org.springframework.web.servlet.theme.CookieThemeResolver"
id="themeResolver" p:cookieName="theme" p:defaultThemeName="standard" />
<!-- This bean resolves specific types of exceptions to corresponding logical
- view names for error views. The default behaviour of DispatcherServlet
- is to propagate all exceptions to the servlet container: this will happen
- here with all other types of exceptions. -->
<!-- <bean -->
<!-- class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" -->
<!-- p:defaultErrorView="uncaughtException"> -->
<!-- <property name="exceptionMappings"> -->
<!-- <props> -->
<!-- <prop key=".DataAccessException">dataAccessFailure</prop> -->
<!-- <prop key=".NoSuchRequestHandlingMethodException">resourceNotFound</prop> -->
<!-- <prop key=".TypeMismatchException">resourceNotFound</prop> -->
<!-- <prop key=".MissingServletRequestParameterException">resourceNotFound</prop> -->
<!-- </props> -->
<!-- </property> -->
<!-- </bean> -->
<!-- Enable this for integration of file upload functionality -->
<bean
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
id="multipartResolver" />
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver"
id="tilesViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>
<bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"
id="tilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/layouts/layouts.xml</value>
<!-- Scan views directory for Tiles configurations -->
<value>/WEB-INF/views/**/views.xml</value>
</list>
</property>
</bean>
<bean
class="com.mmm.webapi.conversion.ApplicationConversionServiceFactoryBean"
id="applicationConversionService" />
<!-- <bean class="com.mangofactory.swagger.configuration.SpringSwaggerConfig"
/>
-->
<bean class="com.mmm.webapi.documentation.SwaggerConfig" />
</beans>
Related
With Spring 4.3.5, we have been using an application architecture where we initialize some of the beans at a go and then use the beans as "Reference" for other beans.
Something like this
<bean id="handleUncheckedEndpointExceptionAdvice"
class="com.gehcit.cp.ws.infrastructure.aop.advices.HandleUncheckedEndpointExceptionAdvice">
<constructor-arg ref="exceptionFactory"/>
<property name="exceptionSenderService" ref="exceptionSenderService" />
</bean>
But once we upgraded our library to Spring 5.2.x, we started getting the below exception at Jboss 7.3.1 startup:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'handleUncheckedEndpointExceptionAdvice' defined in ServletContext resource [/WEB-INF/context-web.xml]: Cannot resolve reference to bean 'exceptionSenderService' while setting bean property 'exceptionSenderService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'exceptionSenderService' available
What all I did:
Enabled TRACE logging and found this - 2022-03-05 13:09:39,927 TRACE [org.springframework.beans.factory.annotation.InjectionMetadata] (ServerService Thread Pool -- 65) Processing injected element of bean 'exceptionSenderService': AutowiredFieldElement for private com.gehcit.cp.serviceability.service.ExceptionSenderService com.gehcit.cp.cem.aplix.jms.service.Impl.AplixPublisherQueueServiceImpl.exceptionSender 2022-03-05 13:09:39,927 TRACE [org.springframework.beans.factory.support.DefaultListableBeanFactory] (ServerService Thread Pool -- 65) Returning cached instance of singleton bean 'exceptionSenderServiceImpl' 2022-03-05 13:09:39,927 TRACE [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] (ServerService Thread Pool -- 65) Autowiring by type from bean name 'exceptionSenderService' to bean named 'exceptionSenderServiceImpl'
Now the bean is getting initialized but when the reference is being searched, being searched from the Spring cache, we get nothing.
But then we tried shifting the bean initialization from a different xml to the same xml where the bean is being referred and that worked. So we concluded that somehow the cross XML configurations are not working.
Bean being referred from context-web.xml
<?xml version="1.0"?>
<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:cxf="http://cxf.apache.org/core"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:oauth2="http://www.springframework.org/schema/security/oauth2"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://www.springframework.org/schema/security
https://www.springframework.org/schema/security/spring-security.xsd
http://www.springframework.org/schema/security/oauth2
https://www.springframework.org/schema/security/spring-security-oauth2.xsd">
<task:annotation-driven/>
<!-- Define placeholder for application properties -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/web.properties</value>
<value>classpath:application.properties</value>
</list>
</property>
</bean>
<!-- Load CXF modules from cxf.jar -->
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<import resource="context-cem-web.xml"/>
<!-- define an auto-proxy generator for our advisors -->
<bean
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
<property name="proxyTargetClass" value="false"/>
</bean>
<!-- id generator for exceptions -->
<bean id="exceptionIdGenerator"
class="com.gehcit.cp.ws.infrastructure.exception.HostTimeExceptionIdGenerator">
<property name="prefix" value="CEN"/>
</bean>
<!-- system fault factory -->
<bean id="systemFaultFactory"
class="com.gehcit.cp.ws.infrastructure.exception.StandardSystemFaultFactory">
<property name="exceptionIdGenerator" ref="exceptionIdGenerator"/>
<property name="appendExceptionIdToErrorMessage" value="true"/>
</bean>
<bean id="defaultParamConverterProvider"
class="org.apache.cxf.jaxrs.ext.search.DefaultParamConverterProvider"/>
<!-- factory for mapping runtime exceptions to wire faults -->
<bean id="exceptionFactory"
class="com.gehcit.cp.ws.infrastructure.exception.ExceptionMapperExceptionFactory">
<property name="exceptionMappers">
<list>
<bean
class="com.gehcit.cp.ws.infrastructure.exception.IllegalArgumentFaultFromIllegalArgumentExceptionMapper"/>
<bean
class="com.gehcit.cp.ws.infrastructure.exception.SecurityFaultFromCPSecurityExceptionMapper"/>
<bean
class="com.gehcit.cp.ws.infrastructure.exception.ApplicationFaultFromApplicationErrorMapper"/>
<bean
class="com.gehcit.cp.ws.infrastructure.exception.SystemFaultFromRuntimeExceptionMapper">
<constructor-arg ref="systemFaultFactory"/>
</bean>
</list>
</property>
</bean>
<!-- aspect for handling unchecked endpoint exceptions -->
<bean id="handleUncheckedEndpointExceptionAdvice"
class="com.gehcit.cp.ws.infrastructure.aop.advices.HandleUncheckedEndpointExceptionAdvice">
<constructor-arg ref="exceptionFactory"/>
<property name="exceptionSenderService" ref="exceptionSenderService" />
</bean>
<bean id="handleUncheckedEndpointExceptionPointcut"
class="org.springframework.aop.support.annotation.AnnotationMatchingPointcut">
<constructor-arg type="java.lang.Class">
<null/>
</constructor-arg>
<constructor-arg type="java.lang.Class"
value="com.gehcit.cp.ws.infrastructure.aop.annotations.HandleUncheckedEndpointException"/>
</bean>
<bean id="handleUncheckedEndpointExceptionAdvisor"
class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="advice" ref="handleUncheckedEndpointExceptionAdvice"/>
<property name="pointcut"
ref="handleUncheckedEndpointExceptionPointcut"/>
</bean>
The bean is being initialized in context-applix.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms.xsd">
<!-- Activemq connection factory start -->
<bean id ="connectionAmqFactory"
class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL"
value="vm://broker#{T(com.gehcit.cp.ws.ServiceProperties).instance().getProperty(T(com.gehcit.cp.Constants).CENTRICITY_DEPLOY_ID)}-applix"
/>
</bean>
<!-- A POJO that implements the JMS message listener -->
<bean id="aplixSubscriberQueueService"
class="com.gehcit.cp.cem.aplix.jms.listener.AplixSubscriberQueueListener"
/>
<bean id="exceptionSenderService"
class="com.gehcit.cp.cem.aplix.jms.service.Impl.AplixPublisherQueueServiceImpl"
/>
<!-- A destination in ActiveMQ -->
<!--amq:topic id="destination" physicalName="APLIX.ERRORINFO" /-->
<!-- A destination in ActiveMQ -->
<amq:queue id="destination" physicalName="APLIX.ERRORINFO" />
<!-- A destination in ActiveMQ end-->
<!-- A cached connection to wrap the ActiveMQ connection -->
<bean id="cachedAmqConnectionFactory"
class="org.springframework.jms.connection.CachingConnectionFactory"
p:targetConnectionFactory-ref="connectionAmqFactory"
p:sessionCacheSize="10" />
<!-- Adding JMS transaction Manager -->
<bean id="TransactionManager"
class="org.springframework.jms.connection.JmsTransactionManager">
<property name="connectionFactory" ref="cachedAmqConnectionFactory"/>
</bean>
<!-- Adding JMS transaction Manager -End -->
<!-- A JmsTemplate instance that uses the connection and destination -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"
p:connectionFactory-ref="cachedAmqConnectionFactory"
p:defaultDestination-ref="destination" />
<!-- A JmsTemplate instance that uses the connection and destination end -->
<!--Cofiguring JMS container and listener-->
<jms:listener-container container-type="default"
connection-factory="cachedAmqConnectionFactory" destination-type="queue" >
<jms:listener destination="APLIX.ERRORINFO"
ref="aplixSubscriberQueueService" method="onMessage" />
</jms:listener-container>
<!--Cofiguring JMS container and listener end-->
<!-- The Spring message listener container configuration -->
<amq:broker start="true" persistent="false"
brokerName="broker#{T(com.gehcit.cp.ws.ServiceProperties).instance().getProperty(T(com.gehcit.cp.Constants).CENTRICITY_DEPLOY_ID)}-applix"
deleteAllMessagesOnStartup="false" >
<amq:managementContext>
<amq:managementContext createConnector="false"/>
</amq:managementContext>
<amq:systemUsage>
<amq:systemUsage>
<amq:memoryUsage>
<amq:memoryUsage limit="512 mb"/>
</amq:memoryUsage>
<amq:tempUsage>
<amq:tempUsage limit="2 gb"/>
</amq:tempUsage>
</amq:systemUsage>
</amq:systemUsage>
<amq:transportConnectors>
<amq:transportConnector name="vm"
uri="vm://broker#{T(com.gehcit.cp.ws.ServiceProperties).instance().getProperty(T(com.gehcit.cp.Constants).CENTRICITY_DEPLOY_ID)}-applix"/>
</amq:transportConnectors>
</amq:broker>
</beans>
Can someone help me find a way to refer beans from other xml files in Spring 5.2.x.
Note: We have changed the classloading part with Spring 5.2.
Added this to the web.xml
<listener-class>com.gehcit.cp.ws.infrastructure.security.BeanFactoryContextLoaderListener</listener-class>
</listener>
<!-- <context-param>
<param-name>locatorFactorySelector</param-name>
<param-value>/beanRefFactory.xml</param-value>
</context-param> -->
BeanFactoryContextLoaderListener.java
package com.gehcit.cp.ws.infrastructure.security;
import javax.servlet.ServletContext;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
public class BeanFactoryContextLoaderListener extends ContextLoaderListener {
private static Logger log = Logger.getLogger(BeanFactoryContextLoaderListener.class)
#Override
protected ApplicationContext loadParentContext(ServletContext servletContext) {
log.info("Entered loadParentContext");
ApplicationContext ctx = new ClassPathXmlApplicationContext("beanRefFactory.xml");
log.info("Exit loadParentContext" + ctx.toString());
return ctx;
}
}
This has something to do with ApplicationContext which I am unable to resolve.
With Spring Security I'm trying to create the following authentication verification using Spring.
#Service("LoginUserDetailsServiceImpl")
public class LoginUserDetailsServiceImpl implements UserDetailsService {
private static final Logger logger = Logger.getLogger(UserDetailsService.class);
#Autowired
private CustomerDao customerDao;
#Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
logger.info("Customer DAO Email:" +email);
Customer customer = customerDao.getCustomer(email);
logger.info("Customer DAO Email:" +customer.getEmail());
Roles role = customer.getRole();
logger.info("Customer DAO Role:" + role.getRoles());
MyUserPrincipalimpl principal = new MyUserPrincipalimpl(customer);
logger.info("customerUserDetails DAO:" +principal);
if (customer == null) {
throw new UsernameNotFoundException(email);
}
return principal;
}
}
Which produces the following error
5:56,638 WARN XmlWebApplicationContext:487 - Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'LoginUserDetailsServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.emusicstore.dao.CustomerDao com.emusicstore.serviceImpl.LoginUserDetailsServiceImpl.customerDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.emusicstore.dao.CustomerDao] 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)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
I've found if I comment out the following
<bean id="LoginUserDetailsServiceImpl" class="com.emusicstore.serviceImpl.LoginUserDetailsServiceImpl"/>
from my application-security.xml the above error is not produced. Also my junit test will execute with no issue.
#Autowired
CustomerDao customerDao;
#Test
public void LoginService() {
String email="customeremail";
logger.info("Customer DAO Email:" +email);
Customer customer = customerDao.getCustomer(email);
logger.info("Customer DAO Email:" +customer.getEmail());
Roles role = customer.getRole();
logger.info("Customer DAO Role:" + role.getRoles());
}
Repo
#Repository
public class CustomerDaoImp implements CustomerDao {
private static final Logger logger = Logger.getLogger(CustomerDaoImp.class);
#Autowired
SessionFactory sessionFactory;
Application-security.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:security="http://www.springframework.org/schema/security"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:webflow-config="http://www.springframework.org/schema/webflow-config"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- <bean id="LoginUserDetailsServiceImpl" class="com.emusicstore.serviceImpl.LoginUserDetailsServiceImpl"/>-->
<security:http auto-config="true" use-expressions="true">
<security:intercept-url pattern="/login_test" access="permitAll" />
<security:intercept-url pattern="/logout" access="permitAll" />
<security:intercept-url pattern="/accessdenied" access="permitAll" />
<security:intercept-url pattern="/productList" access="permitAll" />
<!-- <security:intercept-url pattern="/**" access="permitAll" /> -->
<security:intercept-url pattern="/admin/**" access="hasRole('ROLE_ADMIN')" />
<security:intercept-url pattern="/shoppingCart/checkOut" access="hasRole('ROLE_USER')" />
<security:form-login login-page="/login_test" login-processing-url="/j_spring_security_check" authentication-failure-url="/accessdenied"
/>
<security:logout logout-success-url="/logout" />
</security:http>
<!-- <security:authentication-manager>-
<security:authentication-provider user-service-ref="LoginUserDetailsServiceImpl"/>
</security:authentication-manager> -->
<security:authentication-manager alias="authenticationManager">
<security:authentication-provider>
<security:user-service>
<security:user name="lokesh" password="password" authorities="ROLE_USER" />
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mydb" />
<property name="username" value="root" />
<property name="password" value="t3l3com" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.emusicstore</value>
</list>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="1024000" />
</bean>
</beans>
Spring-servlet.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:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="com.emusicstore"></context:component-scan>
<bean id="viewresolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- setting maximum upload size -->
<property name="maxUploadSize" value="10000000" />
</bean>
<mvc:resources location="/WEB-INF/resource/" mapping="/resource/**"></mvc:resources>
<tx:annotation-driven />
</beans>
I'm not sure where exactly the issue is but I'm guessing it is in how the bean is configured in the xml. Should I reference the customerDao bean when in my xml file? I would have thought the #Autowired annotation would have resolved this however?
UPDATE
If I update the application-security to add a new Customer Dao bean a new error message is thrown.
<bean id="LoginUserDetailsServiceImpl" class="com.emusicstore.serviceImpl.LoginUserDetailsServiceImpl"/>
<bean id="CustomerDao" class="com.emusicstore.daoImpl.CustomerDaoImp"/>
I get the following error 'org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.emusicstore.dao.CustomerDao] is defined: expected single matching bean but found 2: customerDaoImp,CustomerDao'
The problem is a CustomerDao instance can't be found by Spring. Either it's failed to instantiate somewhere higher up in your logs, or likely it hasn't been defined in a way that it can be instantiated and made available for injection by Spring Framework.
If you're using XML based dependency injection then defined CustomerDao instance with id customerDao in your XML similar to how LoginUserDetailsServiceImpl is defined.
Or if you're using annotation based dependency injection then add the appropriate stereotype annotation (#Component, #Repository etc) to the CustomerDao so Spring IoC knows to instantiate it and make is available for dependency injection.
I am facing a null pointer exception on my app , I am annotating Dao with #Repository , the servive by #Service , controller with #Controller and service inside it with #ManagedProperty, I am suspecting my application context is not well configured so here there is:
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xml:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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-3.2.xsd">
<!-- Enable Spring Annotation Configuration -->
<context:annotation-config/>
<!-- Scan for all of Spring components such as Spring Service -->
<context:component-scan base-package="com.domain.nameOfapp.*" />
<!-- Necessary to get the entity manager injected into the factory bean -->
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<!-- Define Hibernate JPA Vendor Adapter -->
<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" />
</bean>
<!-- Entity Manager Factory -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="jpa-persistence" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<property name="packagesToScan">
<list>
<value>com.domain.nameOfapp.*</value>
</list>
</property>
</bean>
<!-- Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- Detect #Transactional -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
Any help would be great! thanks
Your component scan needs to be as below,
<context:component-scan base-package="com.domain.nameOfapp" />
This will scan all the classes under this package including any sub-packages.
Also using #ManagedProperty, are you trying to autowire the spring service bean? I believe you should be using #Autowired.
I'm trying to set up Hibernate 4 with Spring 4, and I have run into some problems configuring it. Specifically, in my servlet-context.xml file, I followed a guide and added the following code blocks to this file:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>/WEB-INF/hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
Both of these display the following error: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'bean'
The session factory bean also displays this error: Cannot locate BeanDefintionParser for element [bean]
Here is my entire `servlet-context.xml' file (excluding those two code blocks for brevity's sake, though please notice that they are actually in the file):
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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.1.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- <beans:property name="prefix" value="/WEB-INF/views/" />-->
<!-- <beans:property name="suffix" value=".jsp" /> -->
</beans:bean>
<context:component-scan base-package="pear.pear.pear" />
</beans:beans>
How can I resolve these issues?
You set the default namespace for that XML file to mvc, so you'll need to prefix your element (beans:bean) or change the default namespace to beans (the usual approach).
When I am trying to start the application it throws an exception shown in stack trace.
I'm using:
spring version 3.2.4.RELEASE
spring Transaction version 3.2.4.RELEASE
spring.security.version 3.0.5.RELEASE
hibernate.version 4.2.1.Final
JPA 1.0
I knew that PropertyTypeDescriptor is removed from spring. But I dont know how to handle this I'm stuck in this from last 2 days. Please suggest.
Stacktrace:
2013-11-19 22:25:17 ERROR TestContextManager:324 - Caught exception
while allowing TestExecutionListener
[org.springframework.test.context.support.DependencyInjectionTestExecutionListener#109cdd5]
to prepare test instance
[test.clickbuff.controller.TestUserController#893f08]
java.lang.IllegalStateException: Failed to load ApplicationContext at
org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308)
at
org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
at
org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at
org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:220) at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:301)
at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:303)
at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at
org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at
org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at
org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at
org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at
org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at
org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name
'org.springframework.context.annotation.internalConfigurationAnnotationProcessor':
Instantiation of bean failed; nested exception is
java.lang.NoClassDefFoundError:
org/springframework/core/convert/support/PropertyTypeDescriptor at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:965)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:911)
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:291)
at
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at
org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:398)
at
org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:594)
at
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:407)
at
org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:84)
at
org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
at
org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:280)
at
org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:304)
... 24 more Caused by: java.lang.NoClassDefFoundError:
org/springframework/core/convert/support/PropertyTypeDescriptor at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:960)
... 38 more Caused by: java.lang.ClassNotFoundException:
org.springframework.core.convert.support.PropertyTypeDescriptor at
java.net.URLClassLoader$1.run(URLClassLoader.java:366) at
java.net.URLClassLoader$1.run(URLClassLoader.java:355) at
java.security.AccessController.doPrivileged(Native Method) at
java.net.URLClassLoader.findClass(URLClassLoader.java:354) at
java.lang.ClassLoader.loadClass(ClassLoader.java:423) at
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at
java.lang.
ClassLoader.loadClass(ClassLoader.java:356) ... 39 more
Application context:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:task="http://www.springframework.org/schema/task" 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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<import resource="application-config.xml" />
<import resource="spring-database.xml" />
<import resource="Spring-Mail.xml" />
<import resource="spring-security.xml" />
<!--Setting Context Annotation Driven -->
<context:annotation-config />
<!-- Controller's Base package -->
<context:component-scan base-package="training.impetus.tp" />
<!-- Scheduler Annotation Driven -->
<task:annotation-driven />
<!-- Spring MVC Annotation Driven -->
<mvc:annotation-driven />
<!-- Transaction Annotation Driven -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Resource mapping for static resources -->
<mvc:resources mapping="/resources/**" location="/static/" />
<!-- File upload Bean -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
<!-- Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="dataSource" ref="dataSource" />
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- Assigning jobs to scheduler -->
<bean id="schedulerJob" class="training.impetus.tp.scheduler.MailServiceJob"></bean>
<!-- processes PersistenceUnit and PersistenceContext annotations -->
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<!-- View Mapping to MVC -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- Exceptions Mapping to MVC -->
<bean
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="com.javabeat.ExampleException">
ExceptionPage
</prop>
</props>
</property>
<property name="defaultErrorView" value="error" />
</bean>
<!-- Message Resources -->
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>mymessages</value>
</list>
</property>
</bean>
<bean id="payCheckThread" class="training.impetus.tp.scheduler.PayCheckThread" scope="prototype"></bean>
</beans>
apploicationContext.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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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.xsd">
<context:annotation-config />
<context:component-scan base-package="training.impetus.tp" />
<context:component-scan base-package="training.impetus.tp.controller" />
<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jacksonMessageConverter" />
</list>
</property>
</bean>
</beans>
Sprung database.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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="training.impetus.tp" />
<context:property-placeholder location="classpath:datasource.properties" />
<context:component-scan base-package="training.impetus.tp" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${user}" />
<property name="password" value="${password}" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
</bean>
</property>
<property name="persistenceUnitName" value="default"></property>
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
<context:component-scan base-package="training.impetus.tp.controller" />
<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jacksonMessageConverter" />
</list>
</property>
</bean>
</beans>
test case:
#ContextConfiguration(locations = {"classpath:mvc-config.xml"})
#TransactionConfiguration(defaultRollback = true)
#RunWith(SpringJUnit4ClassRunner.class)
#Transactional
public class TestUserController {
#Before
public void setUp() throws Exception {
}
#Test
public void testGetAllUsers() {
fail("Not yet implemented");
}
The spring.security.version 3.0.5.RELEASE depends on Spring core 3.0.3 release. This is why application context cannot load. Upgrade this to a later version.
You can verify this at https://mvnrepository.com/artifact/org.springframework.security/spring-security-core/3.0.5.RELEASE (Look for Compiler dependencies)
If you really need to use that class.
Try put that class in another jar so you can include in your classpath.
Like is done with the RmiInvocationWrapper_Stub when you migrate from version 2.5.6 to 3.x.
You can find information in this docs of spring link