Injecting property file values into Controller via Spring not working - java

Hoping you may be able to help. My Spring knowledge is extremely limited to apologies if the solution is something really obvious.
Here are the contents of the relevant files:
application-context.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:util="http://www.springframework.org/schema/util"
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/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd">
<context:annotation-config />
<util:properties id="messageConfig" location="classpath:customMessages.properties"/>
</beans>
/src/main/resources/customMessages.properties:
sms.message=Test
Java class:
#Controller
public class ApiRestController implements ApiRest {
#Value("#{messageConfig['sms.message']}")
private String smsMessage;
public final void setSmsMessage (String smsMessage)
{
this.smsMessage = smsMessage;
}
...
}
However, when referencing the 'smsMessage' variable elsewhere in the Java class, the value is NULL. Any ideas what I might be missing? Any pointers would be really appreciated.

Add
<context:component-scan base-package="spring-controllers-package"/>
to the application-context.xml

Related

spring xml override utils:list same id

I'm trying to set up a local overrides file for some of my bean definitions. Yes, it's a fragile system, but it's just for testing. Basically, I've got one XML file which looks more or less like:
<?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:util="http://www.springframework.org/schema/util"
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/tx http://www.springframework.org/schema/tx/spring-tx-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/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- so many beaaaannzzz -->
<utils:list id="partnerList" value-type="my.partner.Class">
<ref bean="previouslyDefinedBean"/>
</utils:list>
<!-- include local bean definition overrides -->
<import resource="file://${user.home}/somedirectory/prefix-*.xml"/>
</beans>
This works in general, but it has an unexpected and undesirable result with the utils:list element.
My somedirectory/prefix-*.xml override file defines another list:
<utils:list id="partnerList" value-type="my.partner.class">
<ref bean="otherBean"/>
</utils:list>
When it is picked up however, I get an undesired result: partnerList has two beans in it, previouslyDefinedBean and otherBean, while I want it to only have the latter.
Now, I'm aware Spring offers some weird weird collection merging, so I tried setting <utils:list merge="false"... but that blew up as an unsupported attribute. Is there something I can do to continue using this override system for the util:list, or have I got to take another tack entirely?

Spring Data Populator configuration: org.xml.sax.SAXParseException

I would like to use the Spring Data Populator, but I am getting the SAXParseException:
Exception in thread "main"
org.springframework.beans.factory.parsing.BeanDefinitionParsingException:
Configuration problem: Failed to import bean definitions from URL location
[classpath*:spring-context/dbContext.xml]
Offending resource: class path resource [spring-context/applicationContext.xml];
nested exception is org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:
Line 64 in XML document from URL [file:/home/pi/Workspace/AGServer/bin/spring-context/dbContext.xml] is invalid;
nested exception is org.xml.sax.SAXParseException: cvc-complex-type.3.2.2:
Attribute 'location' is not allowed to appear in element 'repository:jackson-populator'.
My main server class is:
public final class ServerMain {
private static final String APP_CONTEXT_XML = "spring-context/applicationContext.xml";
public static void main(final String[] args) {
final ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(APP_CONTEXT_XML);
context.registerShutdownHook();
}
}
The applicationContext.xml loads other sub-files and looks like 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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<import resource="classpath*:spring-context/serverContext.xml" />
<import resource="classpath*:spring-context/dbContext.xml" />
<import resource="classpath*:spring-context/jettyContext.xml" />
</beans>
And finally, the beginning of the dbContext.xml from where I invoke the Populator
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:repository="http://www.springframework.org/schema/data/repository"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository.xsd">
<!-- AUTH DS / PU / TX / SERVICE -->
<!-- the repositories configuration, ommited for clarity -->
<repository:jackson-populator location="classpath:auth.json" />
May that be caused somehow by conflicts with spring-beans-3.0.xsd? I saw in all (though not too many, e.g. here: http://docs.spring.io/spring-data/commons/docs/1.5.1.RELEASE/reference/htmlsingle/???#d0e893) documents that spring-beans.xsd, without any release id is used. I tried to change it, but that didn't help.

Spring #Aspect & Maven. Not working in external modules, but it's working witouth annotations

I've a question about Spring AOP and AspectJ, defining aspects, pointcut etc with annotations.
The context is this:
I have 2 maven modules (A and B).
Module B uses module A (via <dependency>...</dependency> in maven) and import his spring context.
I'm trying to write and use an very stupid test aspect (catch all public operations).
This aspect is defined in module A,
I wan't that this aspect works when I call any public operation in A or B.
Well, now the "postergeist". My first aproach was define all in XML. Aspect, pointcut, etc. And it works really well. This is the code:
ApplicationContext
<?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.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 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<aop:config>
<aop:aspect id="myAspectAOP" ref="myAspect">
<aop:around method="validateCall"
pointcut="execution(public * *(..))" />
</aop:aspect>
</aop:config>
<context:component-scan base-package="com.mypackage"/>
</beans>
MyAspect.java and Test.java is the same that the next case (without Aspect annotations). I don't put it here yet to avoid spreading much text.
That works fine!
Ok, next aproach. Using annotations. This is the code:
ApplicationContext
<?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.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 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<aop:aspectj-autoproxy />
<context:component-scan base-package="com.mypackage"/>
</beans>
And MyAspect class
#Aspect
#Component //I have tried without this annotation, and it doesn't work
public class MyAspect {
#Pointcut("execution(public * *(..))")
public void allPublic() {
}
#Around("allPublic()")
public Object validateCall(final ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
// Some Code
}
}
Test.java (package com.mypackage)
#Component
public class Test{
public void foo(){
//Some code
}
}
In this case:
when I put Test.java into module B and call an public operation (remember, class is in B module), it doesn't work.
BUT when I put Test.java into A Module and I call a public operation (It's in A module), it works. It looks like only works with calls to public operations of classes defined into the same module where Aspect is defined (A module), but not in "externals" (B Module). Remember. B module includes/uses A Module and import his context. But it isn't happening when I use "xml declaration" instead of "annotations declaration"
Important: MyAspect.java is in A Module allways
Remember, in the first case (defining all in applicationContext, and not using #Aspect #Pointcut,#...) works allways, in B and A module public operations.
In both cases, the classes are into com.mypackage, so are injected by spring.
I'm really lost, It's the first time I get this kind of "postergeist", and I have been looking and looking into Spring documentation an google for days, but I can't find any solution.
I'm using Spring 3.1.2 and Aspectj 1.6.11
Please, any idea?
Thanks!

Spring frame work config issue "prefix context for for element context:annotation-config is not bound"

I am having a weird issue that I can't seem to track down. I have this working with other servers without a problem, but I can't seem to get this one to work. The closest post that I see to my problem was this post The prefix "context" for element "context:component-scan" is not bound
All others really were just because the prefix was not in the xml file. I am hoping someone might be able to point me in the right direction here.
Spring XML 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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
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
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">
<context:annotation-config/>
So I have that, but getting this error:
org.xml.sax.SAXParseException: The prefix "context" for element "context:annotation-config" is not bound.
Appreciate any help. Let me know what else I can provide.
Thanks
I was experiencing the same problem until I realized beans tag attribute xmlns:context was missing. Just added the below lines
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
...."
Then rebuilt the project.
It worked well then on.
The following works for me:
test.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:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
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
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">
<context:annotation-config/>
</beans>
When I use the following class to run it:
Test.java
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) throws Exception {
new ClassPathXmlApplicationContext("test.xml");
System.out.println("Finished!");
}
}
Can you please see if this runs for you? You will need the following libraries in the classpath: commons-logging, spring-asm, spring-beans, spring-context, spring-core, and spring-expression.
Please let me know if it worked. If it didn't, please post the full stack-trace. Finally, I used Spring 3.1.1 for the above.
This error comes when you have xmlns:context missing from your spring xml file. So add it. Your beans header should look something like the following -
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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" >
<context:annotation-config />
<context:component-scan base-package="controller" />
</beans>
I encountered the same problem but i was able to solve it by moving
from applicationContext.xml to spring-servlet.xml and adding xmlns:context in the spring-servlet.xml

spring + aspectj, define an aspect #Around

i want to define an #Around aspect for a method of my #Entity
All my entities are in package data.entity
A define an aspect like this:
#Aspect
public class TestAspect {
#Around("execution(* data.entity..*(..))")
public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("INTERCEPT: "+pjp.toLongString());
return pjp.proceed();
}
}
But never is intercepted... where is my error?
In spring xml i have 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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<context:component-scan base-package="data.dao, data.service" />
<tx:annotation-driven proxy-target-class="true"/>
<aop:aspectj-autoproxy/>
<bean id="testAspect" class="spring.TestAspect" />
... datasource and other ...
</beans>
I try also
#Around("target(data.entity.MyEntity)")
and
#Around("target(data.entity..)")
but still not work.
Thanks.
It looks like you use spring-proxy-aop. This works only if the class is a spring manged bean, and the adviced method must be invoked from an other object.
Try to use real aspectJ instead of spring-proxy-aop.
I have just started using AOP and below are the findings at my level
i am assuming you have necessary jar files, aspectjweaver-1.6.10.jar and org.springframework.aop-3.0.5.RELEASE.jar present in your apps classpath.
The method aroundAdvice, as you have defined currently is perfect.
Could you remove the below line and try.
<context:component-scan base-package="data.dao, data.service" />

Categories

Resources