Spring Bean tag error - spring

Could some please let me know what things I am missing here. I m getting the below exception
Line 76 in XML document from file [C:\S2\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\proj_name\WEB-INF\classes\spring\test-context.xml] is invalid; nested exception is
org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content
was found starting with element 'bean'. One of '{"http://www.springframework.org/schema/beans":description,
"http://www.springframework.org/schema/beans":meta,
"http://www.springframework.org/schema/beans":constructor-arg, "http://www.springframework.org/schema/beans":property, "http://www.springframework.org/schema/beans":qualifier, "http://www.springframework.org/schema/beans":lookup-method, "http://www.springframework.org/schema/beans":replaced-method, WC[##other:"http://www.springframework.org/schema/beans"]}' is expected.
And I have my test-context.xml as below.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:batch="http://www.springframework.org/schema/batch"
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd">
<bean id="restAuthenticationEntryPoint" class="com.test.security.RestAuthenticationEntryPoint">
Could anybody please let me know why I am missing here . i have spring 3.0.5 Spring jar

You are missing surrounding <beans> </beans> tags:
<beans>
<bean id="restAuthenticationEntryPoint" class="com.test.security.RestAuthenticationEntryPoint">
<beans>
This is what the error message wants to tell you...
Invalid content was found starting with element 'bean'.
"The bean tag is not allowed here"
One of [..] beans, [..] is expected
The only valid tags here are "beans", etc.

Add <beans:beans/> to your config xml.
<beans:beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:batch="http://www.springframework.org/schema/batch"
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd">
<beans:beans id="restAuthenticationEntryPoint" class="com.test.security.RestAuthenticationEntryPoint"/>
</beans:beans>
In your xml you have opened <bean> tag but not closed.
<beans xmlns....>
where is </beans> and same for restAuthenticationEntryPoint
so you are getting that exception.

Related

cvc-elt.1: Cannot find the declaration of element 'beans' - applicationcontext.xml

I end up getting only this error:
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 12 in XML document from class path resource [applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException;
cvc-elt.1: Cannot find the declaration of element 'beans'.
I see that there is a declaration for beans already present
Can someone help me with where I am exactly going wrong?
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jms="http://www.springframework.org/schema/jms" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms-3.0.xsd">
<mvc:annotation-driven>
</mvc:annotation-driven>
 
 
 
 
 
 
Spring jar present is 4.1.6.
Here is your corrected file :
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jms="http://www.springframework.org/schema/jms" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms-3.0.xsd">
<mvc:annotation-driven>
</mvc:annotation-driven>
</beans>
You wrote xsi:schemalocation instead of xsi:schemaLocation (note the uppercase L)
More information about the xsi:schemaLocation:
The XML parser will read the schema location from internet and use it to validate the xml. If you do not have internet access (as do most Productive systems with no out going internet access) Spring comes to the rescue. It interprets the read requests and loads the schema from within the Spring jars.

Spring configuration set up : No qualifying bean of type [] found for dependency

I am desperate about this problem. I have been looking around for almost a week looking for solutions to my exception : I have been looking on this forum and also on a site giving the inventory of the many possible causes : http://www.baeldung.com/spring-nosuchbeandefinitionexception
Nothing seems to match with my configuration.
I must be missing something.
The source exception is :
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [fr.bnp.paiement.persistence.api.idaos.IDaoCompte] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#javax.inject.Inject()}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1326)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1072)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:967)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:543)
... 15 more
If, instead of the component-scan I explicitly give the bean location in the Spring configuration i get the same Exception.
I am wondering if this is not because of the depency chain of my modules in my Maven project :
Services --> ServicesAPI
Services --> PersistenceAPI
Persistence --> PersistenceAPI
But I am trying to inject a Persistence bean into a Services one. Is it a problem if Services doesn't have a dependency on Persistence but on PersistenceAPI ?
The spring configuration xml files follow the same chain for the import.
Services imports ServicesAPI
Services imports PersistenceAPI
Persistence imports PersistenceAPI
How can the Service module see the beans in the Persistence module ? Maybe through the PersistenceAPI (which is the type of the Persistence in my injection) ?
With EJB this dependency chain works fine (probably because there are no xml imports to do).
I tried importing Persistence from Services directly but it does not work if I don't change the dependency chain : it just doesn't find the xml file.
Everything would nicely work if I didn't bother splitting API and implementation, but that's what I am trying to achieve.
I am trying to set up a clean Maven multi-module project.
The code is here : https://github.com/ASolidGrasp/springTest.git
Prerequisites to make it work as is : having MySQL installed.
You have the error when running fr.bnp.paiement.service.test.Main.main() in the WSBanqueServices module.
For those who prefer to read the code here :
In WSBanqueServices module \ fr.bnp.paiement.service.test package
public class Main
{
public static void main(String[] args)
{
BeanFactory bF = new ClassPathXmlApplicationContext("classpath:springServices.xml");
IServiceClient iServiceClient = (IServiceClient) bF.getBean("serviceClient");
}
}
In WSBanqueServices module \ fr.bnp.paiement.service.ServiceClient
#Service
#Transactional
public class ServiceClient implements IServiceClient {
#Inject
private IDaoCompte iDaoCompte;
#Inject
private IDaoCarte iDaoCarte;
...
In WSBanqueServices module \ src/main/resources/springServices.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:aop="http://www.springframework.org/schema/aop"
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/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
">
<import resource="classpath:springServicesAPI.xml" />
<import resource="classpath:springPersistenceAPI.xml" />
<aop:aspectj-autoproxy />
<context:component-scan base-package="fr.bnp.paiement.service" />
</beans>
In WSBanqueServicesAPI module \ src/main/resources/springServicesAPI.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:aop="http://www.springframework.org/schema/aop"
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/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
">
<import resource="classpath:springEntities.xml" />
<import resource="classpath:springServicesExceptions.xml" />
<tx:annotation-driven transaction-manager="txManager" />
<aop:aspectj-autoproxy />
<context:component-scan base-package="fr.bnp.paiement.service.api.iservice" />
</beans>
In WSBanquePersistenceAPI module \ src/main/resources/springPersistenceAPI.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:aop="http://www.springframework.org/schema/aop"
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/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
">
<import resource="classpath:springEntities.xml" />
<import resource="classpath:springPersistenceExceptions.xml" />
<tx:annotation-driven transaction-manager="txManager" />
<aop:aspectj-autoproxy />
<context:component-scan base-package="fr.bnp.paiement.persistence.api.idaos" />
</beans>
In WSBanquePersistence module \ src/main/resources/springPersistence.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:aop="http://www.springframework.org/schema/aop"
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/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
">
<import resource="classpath:springPersistenceAPI.xml" />
<aop:aspectj-autoproxy />
<...datasource & co + transactional + persistenceContext...>
<bean
name="daoCarte"
class="fr.bnp.paiement.persistence.daos.DaoCarte" />
<bean
name="daoCompte"
class="fr.bnp.paiement.persistence.daos.DaoCompte" />
<context:component-scan base-package="fr.bnp.paiement.persistence.daos" />
</beans>
In WSBanquePersistence module \ fr.bnp.paiement.persistence.daos.DaoCarte
#Transactional
#Repository("daoCarte")
public class DaoCarte implements IDaoCarte
{...}
In WSBanquePersistence module \ fr.bnp.paiement.persistence.daos.DaoCompte
#Repository("daoCompte")
#Transactional
public class DaoCompte implements IDaoCompte
{...}
Thank you in advance for your help.
Your bean is defined, and scanned (not sure why you do both) in springPersistence.xml. And this file is not imported, directly or indirectly, by the springServices.xml file, that you load in your main class.

context:component-scan cannot be defined

I am having a problem with the Spring-contex definitions. When I look at other people who had this problem, it was normally becasue of usign the wrong schema declerations, but I don't think this is the case with my issue.
Line 21 in XML document from ServletContext resource [/WEB-INF/spring/web-application-context.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 21; columnNumber: 67; cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'context:component-scan'.
here is the xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context/spring-context-4.1.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<!-- Imports the configurations of the different infrastructure systems of the application -->
<context:component-scan base-package="com.robert.login" />
<mvc:annotation-driven />
<import resource="webmvc-context.xml" />
<bean name="LoginInfo" class="com.robert.login.LoginInfo" />
</beans>
I'm using Spring-4.1.6
I think you need to fix this:
xmlns:context="http://www.springframework.org/schema/context/spring-context-4.1.xsd"
Should be:
xmlns:context="http://www.springframework.org/schema/context"

Spring security intercept method shows error

<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-2.0.1.xsd">
<bean id="messageBoardService"
class="com.service.MessageBoardServiceImpl">
<security:intercept-methods>
<security:protect
method="com.service.MessageBoardService.listMessages"
access="ROLE_USER,ROLE_GUEST" />
<security:protect
method="com.service.MessageBoardService.postMessage"
access="ROLE_USER" />
<security:protect
method="com.service.MessageBoardService.deleteMessage"
access="ROLE_ADMIN,IP_LOCAL_HOST" />
<security:protect
method="com.service.MessageBoardService.findMessageById"
access="ROLE_USER,ROLE_GUEST" />
</security:intercept-methods>
</bean>
by executing the above lines of code its showing error
The matching wildcard is strict, but no declaration can be found for element 'bean'
and getting red underline on security intercept method tag
here are the list of jar files
This header when added to the xml file. It works correct
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-2.0.1.xsd">

Spring AOP The matching wildcard is strict, but no declaration can be found for element 'aop:config'

Line 13 in XML document from class path resource [ApplicationContextAOP.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'aop: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:aop="http://www.springframework.org/schema/aop"
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">
<bean id="audience" class="com.idol.Audience" />
<aop:config>
<aop:aspect ref="audience">
<!-- Before performance -->
<aop:before pointcut="execution(*com.idol.performers.Performer.perform(..))"
method="takeSeats"/>
<aop:before pointcut="execution(*com.idol.performers.Performer.perform(..))"
method="turnOffCellPhones" />
<!-- After performance -->
<aop:after-returning pointcut="execution(*com.idol.performers.Performer.perform(..))"
method="applaud" />
<!-- After bad performance(exception thrown) -->
<aop:after-throwing pointcut="execution(*com.idol.performers.Performer.perform(..))"
method="demandRefund" />
</aop:aspect>
</aop:config>
<bean id="poeticDuke" class="com.idol.performers.PoeticJuggler">
<constructor-arg value = "15" />
<constructor-arg ref = "sonnet29" />
</bean>
</beans>
I've seen similar error and I am pretty sure my classpath has org.springframework.aop-3.1.0.M2.jar
Could you tell me please what am I missing?
You need to add to your schemaLocation:
xsi:schemaLocation="
...
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
...
"
Adding one more possibility to the earlier provided answers :
Wrong version of schemaLocation:
http://www.springframework.org/schema/aop/
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
Correct Version:
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
REASON: The extra "/" after "schema/aop"
We received the similar error message for the element tx:advice and got it resolved by replacing the org.springframework.*-3.0.0.M3.jars with org.springframework.*-3.1.2.RELEASE.jars where * represents the jar modules. Hence, try getting a stable release to fix the similar issues.
It happened to me with the following schema definition file. The cause was the extra > after spring-aop-3.0.xsd. Solved after removing it.
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd>
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd">
<!-- reference to tht jdbc name space and schema definition file-->
<!-- to enable AspectJ support-->
<aop:aspectj-autoproxy/>
</beans>

Resources