Spring-boot: load configuration file with #ImportResource error - java

I'm trying to create a Rest service using Spring-boot but, deployed under Tomcat, it does not start because of some error in a configuration file loaded through #ImportResource.
The error is
Caused by: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element "beans".
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203) ~[na:1.8.0_45]
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134) ~[na:1.8.0_45]
My main class starts like this
#Configuration
#ImportResource({"classpath:spring.xml"})
#ComponentScan(basePackages="mypackage")
#EnableWebMvc
public class AppConfig extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(AppConfig.class, args);
}
...
The file spring.xml starts like this
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
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://mybatis.org/schema/mybatis-spring
http://mybatis.org/schema/mybatis-spring.xsd">
Any idea how to deal with it?

Related

Java : How to load content of .properties file into Properties using spring annotation?

I am using Java 8 on spring framework. I have a properties file which has "=" separated values in it. I am trying to load the values of the properties file into a Property directly using spring annotation.
For example :
my Application.properites has:
cat=250
dog=560
lama=1000
donkey=650
And I have declared this properties file in my context.xml as :
<util:properties id="app_props"
location="classpath*:/application.properties" />
Now in my main class I am trying to load all of the values in Application.properites into
private Properties properties;
So that properties.getProperty("cat") will return "250"
Not sure how to do it. May I get any help on that?
Register your properties file as follows
XML Config
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<context:property-placeholder location="classpath:foo.properties" />
</beans>
Java Config
#Configuration
#PropertySource("classpath:foo.properties")
public class PropertiesWithJavaConfig {
//...
}
Using your properties as follows
#Value( "${jdbc.url}" )
private String jdbcUrl;
You can find a complete working solution in my GitHub repository
https://github.com/mirmdasif/springmvc/tree/master/springproperties

Spring doesn't instantiate bean at start

I'm stuck with the following problem. My application doesn't create my Services at startup and I end up with a NullPointerException when I try to use them in my Maven Tests after Inject them with #Autowired.
I don't understand where it comes from. I have done some research but I still don't understand why it doesn't work.
Here is the class where my Autowired administrationActionService is null:
public class AdministrationActionTests extends EntityTests {
#Autowired
AdministrationActionService administrationActionService;
#Test
public void equalsTests() {
administrationActionService.getExample();
[...]
The class :
package com.bloombooking.services;
#org.springframework.stereotype.Service
public class AdministrationActionService extends ServiceEntity{
#Autowired private AdministrationActionDao administrationActionDao;
[...]
And my ApplicationContext.xml. I've placed it in src/ApplicationContext.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"
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">
<context:component-scan base-package="com.bloombooking.services, com.bloombooking.dao"/>
</beans>
I really don't know what I could have done wrong. Can someone help me? Thanks!
To make it work you have to make the following changes:
add #RunWith(SpringJUnit4ClassRunner.class) to your class.
add #ContextConfiguration("path_to_you_spring_beans.xml")
So your class becomes:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration("path_to_you_spring_beans.xml")
AdministrationActionTests {
}

Java Spring Scheduled job not working

I have a web application which is supposed to run a scheduled code:
package com.myproject.daemon.jobs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
#Component
public class MyDaemonJob {
private static final Logger log = LoggerFactory.getLogger(MyDaemonJob.class);
#PostConstruct
public void init() {
log.info("MyDaemonJob is intialized " );
}
#Scheduled(fixedDelay = 1000)
public void startDaemon() {
try {
log.info("MyDaemonJob is running ...");
} catch (Exception e) {
log.error("Encountered error running scheduled job: " + e.getMessage());
}
}
}
It surely is recognized as a Spring bean and initialized, as I can see from the PostConstruct log. However the method with the #Scheduled annotation never runs although it is supposed to run every 1 second.
Here is app 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"
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-4.0.xsd">
<context:component-scan base-package="
com.myproject.daemon.jobs,
com.myproject.product" />
</beans>
Thank you ALL for quick help. This is really helpful.
The code started working, once I added config class with annotations as shown below --
package com.myproject;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
#Configuration
#EnableScheduling
public class AppConfig {
// various #Bean definitions
}
To use the #scheduled annotation it is necessary to include the spring task name space in spring bean configuration xml file. You can update the following namespac e in your xml configuration file.
<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:task="http://www.springframework.org/schema/task"
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-4.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.1.xsd">
Otherwise if you use the spring-boot application you can include the #EnableScheduling in your configuration file

Spring Integration gateway proxy bean for <int:gateway> not being created

I have a context as follows
<?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:int="http://www.springframework.org/schema/integration"
xmlns:int-amqp="http://www.springframework.org/schema/integration/amqp"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/amqp
http://www.springframework.org/schema/integration/amqp/spring-integration-amqp.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<int:gateway id="fooGateway" service-interface="com.foo.FooGateway" />
</beans>
My gateway interface is as follows:
public interface FooGateway
{
public void foo(String foo);
}
This context is imported into another context but the fooGateway bean is never created by GatewayProxyFactoryBean.
I've set a breakpoint in GatewayProxyFactoryBean and I can see that it is not creating a proxy for this interface.
Turn on DEBUG logging for org.springframework; the bean creation process emits lots of information including when it reads particular configuration files.

appengine spring aop java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "accessClassInPackage.sun.reflect")

I am creating application with spring aspect but in app engine when I hit first request then exception occurs while initialize spring framework.
Exception stack trace is as below:
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'adminController' defined in file [PATH\PACKAGE\AdminController.class]:
Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException:
Unexpected AOP exception; nested exception is java.security.AccessControlException:
access denied ("java.lang.RuntimePermission" "accessClassInPackage.sun.reflect")
My Aspect
#Aspect
#Component
public class AuthorizationAspect {
#Around(value="#annotation(org.springframework.web.bind.annotation.RequestMapping)")
public Object interceptApplicationRequest(
ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("test");
return proceedingJoinPoint.proceed();
}
}
My Controller
#Controller
#RequestMapping(value="/admin")
public class AdminController {
#RequestMapping(value = "/add", method = RequestMethod.GET)
public ModelAndView add(){
ModelAndView mv = new ModelAndView("add");
return mv;
}
}
Configuration in 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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<context:component-scan base-package="com.package.name" />
<aop:aspectj-autoproxy/>
<mvc:annotation-driven />
</beans>
Is AOP not allowed in app engine framework?
App Engine version: 1.9.7
Spring Framework: 4.0.3
Based on the exception message, it is likely that the library you are using relies on some classes that are not in the App Engine's White list: https://cloud.google.com/appengine/docs/java/jrewhitelist
In particular, I don't see any of sun.reflect* classes in this white list. Hence, you are getting AccessControllException.

Categories