Spring <context:component-scan base-package="groupid.aop"/> cannot be defined - spring

<?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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 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="triangle1" class="groupid.aop.triangle">
<property name="name" value="triangle bean"></property>
</bean>
<bean id="circle1" class="groupid.aop.circle">
<property name="name" value="circle bean"></property>
</bean>
<aop:aspectj-autoproxy proxy-target-class="true"/>
<context:component-scan base-package="groupid.aop"/>
</beans>
im getting error on <context:component-scan base-package="groupid.aop"/>
this line. im using maven to add dependencies for both spring and aop dependencies. I also added namespace for context. please let me know if im missing anything.

You were wrong in namespaces declaration.
This xml should solve the issue:
<?xml version="1.0" encoding="UTF-8"?>
<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:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="triangle1" class="groupid.aop.triangle">
<property name="name" value="triangle bean" />
</bean>
<bean id="circle1" class="groupid.aop.circle">
<property name="name" value="circle bean" />
</bean>
<aop:aspectj-autoproxy proxy-target-class="true" />
<context:component-scan
base-package="groupid.aop" />
</beans>
In your original XML you have:
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.xsd
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"
As you can see you declared twice spring-beans (http://www.springframework.org/schema/beans/spring-beans.xsd and http://www.springframework.org/schema/beans/spring-beans-3.0.xsd) and you used the 3.0 context version (http://www.springframework.org/schema/context/spring-context-3.0.xsd)
I hope it's usefull
Angelo

Related

Apache Camel invalid namespace handler

I have an application that has been developed (reworked) on Eclipse. It works fine in the development environment. However when it is loaded into Tomcat it fails with a namespace exception:
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from relative location [camel-config.xml]
Offending resource: class path resource [spring-config.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [camel-config.xml]; nested exception is
org.springframework.beans.FatalBeanException: Invalid NamespaceHandler class [org.apache.camel.spring.handler.CamelNamespaceHandler] for namespace [http://camel.apache.org/schema/spring]: problem with handler class file or dependent class; nested excep
tion is java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
my spring-config and camel-config are below
<?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:p="http://www.springframework.org/schema/p"
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:task="http://www.springframework.org/schema/task"
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
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd">
<context:component-scan base-package="foo.bar.agent" />
<task:annotation-driven />
<import resource="camel-config.xml" />
</beans>
camel-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:camel="http://camel.apache.org/schema/spring" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- enable Spring #Component scan -->
<context:component-scan base-package="com.altegix.agent.hl7" />
<bean id="properties" class="org.apache.camel.component.properties.PropertiesComponent">
<property name="ignoreMissingLocation" value="true" />
<property name="locations">
<list>
<value>classpath:default.properties</value>
<value>file:/opt/altegix/agent/application.properties</value>
</list>
</property>
</bean>
<bean id="myhl7codec" class="org.apache.camel.component.hl7.HL7MLLPCodec">
<property name="charset" value="iso-8859-1" />
<property name="validate" value="false" />
</bean>
<camelContext xmlns="http://camel.apache.org/schema/spring"
id="resultsCamelContext">
<contextScan />
<camel:endpoint id="hl7listener"
uri="mina2:tcp://{{results.endpoint.server}}:{{results.endpoint.port}}?sync=true&codec=#myhl7codec" />
</camelContext>
<context:annotation-config />
<bean class="com.altegix.agent.hl7.HL7ListenerConfiguration" />
I don't understand why I would be getting a InvalidNamespace Exception? There is also the noClassDefFoundError which I have not seen before - maybe am I missing some classes that are in Eclipse?

Java Spring batch access command line arguments in XML and Java Annotation config

How can I get the command line arguments in a xml configuration file with spring?
With property file I can write this:
<?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:batch="http://www.springframework.org/schema/batch"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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">
<context:property-placeholder location="file:///my.property" />
<bean id="mybean">
<property name="prop1" ref="#{jobParameters['value.from.property']}" />
</bean>
<bean id="mybean2">
<property name="prop1" ref="#{jobParameters['value2.from.property']}" />
</bean>
<bean id="mybean3">
<property name="prop1" ref="#{jobParameters['value3.from.property']}" />
</bean>
<import resource="classpath:/META-INF/spring/module-context.xml" />
</beans>
But how can I explain Spring to get the values from command line arguments instead of property file specified in property-placeholder?
Thanks
To read from property file use
<property name="prop1" value="${value.from.property}" />
To read from Job Parameter use
<property name="prop1" value="#{jobParameters['value.from.jobParameter']}" />

LOG/print mongo options of mongoclient created using spring

For a spring based project I am using mongoTemplate to query mongo DB.
For this I have used systemProperties to configure mongo options .
Now I want to print the value of these mongo options in my server's LOG, but unable to find a suitable way to do this. Kindly suggest. Following is my spring mongo xml.
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task" xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.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/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd">
<!-- Mongo Template for Test -->
<mongo:mongo replica-set="#{systemProperties['mongoTest.replicaset']}" id="mongoCocofs">
<mongo:options auto-connect-retry="true" connections-per-host="#{systemProperties['mongoTest.connPerHost']?:'80'}" threads-allowed-to-block-for-connection-multiplier="#{systemProperties['mongoTest.threadAllowedForConnMultiplier']?:'5'}" slave-ok="true"/>
</mongo:mongo>
<mongo:db-factory dbname="test" mongo-ref="mongoTest" id="mongoDbFactoryTest" />
<bean id="mongoTemplateTest" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactoryCocofs" />
</bean>
<context:annotation-config />
Solved it.
We can print it by calling following functions on a mongoTemplate bean:
mongoTemplate.getDb().getMongo().getMongoOptions()

Cannot find the declaration of element 'beans' on the bean tag recursively

I am getting below error:
Caused by: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.
my context.xml is as follows
<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="no"
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="fileParser" class="com.xxx.xx.xx.oxm.FileInputParser" >
<property name="unmarshaller" ref ="jaxb2Marshaller" />
</bean>
<bean id = "jaxb2Marshaller" class = "org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name = "classesToBeBound">
<array>
<value>com.xxx.xx.xx.oxm.XutoxenPathInfo</value>
</array>
</property>
</bean>
</beans>
Your schemaLocation is not written correctly. The location needs to follow the namespace name.
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-3.1.xsd">
Yours wasn't doing that. It was
beansNS
contextNS
contextLocation
beansLocation

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