I am not able to find the file/classpath for my xml file. I have tried the absolute path and springs classpath*:
ApplicationContext ctx =
new ClassPathXmlApplicationContext();
private Resource resource = this.ctx.getResource("classpath*:validation.xml");
In the constructor I have:
public Validator() {
if(this.resource.exists()){
System.out.println("FOUND!!!!!!!!!!!!!!!!!!!!"+resource);
}else{
System.out.println("NOT FOUND");
}
this.runInputStream();
}
The above prints out out FALSE
in the console.
I have the validation.xml
file in two places just under different names. I am trying to figure out where and why it is not loading or being found.
I posted the project on GIT.... GitHub Link
---------------------Update 1---------------------------
I have added the following to my pom
file:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
</plugin>
</plugins>
</build>
It appears to have found/been initialized but the class is not and it gives me a null pointer exception just like it was doing before.
Updated Code:
@Configuration
public class Validator {
private InputStream forms;
private ValidatorResources resources;
private Resource validationResource;
ApplicationContext ctx =
new ClassPathXmlApplicationContext();
@PostConstruct
public void init(){
validationResource = ctx.getResource("classpath*:validation.xml");
}
public Validator() {
this.runInputStream();
}
private void runInputStream(){
try{
this.forms = this.validationResource.getInputStream();
this.resources = new ValidatorResources(this.forms);
}catch (IOException ex) {
System.out.println("Error in Validator.Class-----runInputStream IOException: "+ex);
}catch (SAXException e) {
System.out.println("Error in Validator.Class-----runInputStream SAXException: "+e);
}catch (Exception e){
System.out.println("Error in Validator.Class-----------"+e);
}
}
}
The problem is that src/main/resources/validation.xml is not packaged into the war file.
Replace the < build> section in the pom with this:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
</plugin>
</plugins>
</build>
This way the default resource directory for all Maven projects which is src/main/resources will end up in target/classes and in WEB-INF/classes in the WAR.
First remove the trailing / in resources part. The resource part should be relative to the build directory.
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/webapp/WEB-INF/config</directory>
</resource>
</resources>
</build>
Next modify your Validator.java
@Configuration
public class Validator implements InitializingBean {
@Autowired
private ApplicationContext applicationContext;
@Override
public void afterPropertiesSet() throws Exception {
Resource resource = applicationContext.getResource("validation.xml");
if(this.resource.exists()){
System.out.println("FOUND!!!!!!!!!!!!!!!!!!!!"+resource);
}else{
System.out.println("NOT FOUND");
}
}
See InitializingBean for more details. The other approach is to use ApplicationContextAware like this:
@Configuration
public class Validator implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
Resource resource = applicationContext.getResource("classpath:validation.xml");
if(this.resource.exists()){
System.out.println("FOUND!!!!!!!!!!!!!!!!!!!!"+resource);
}else{
System.out.println("NOT FOUND");
}
}
Build the project then deploy the war in tomcat. You should see something like
22-May-2017 10:57:38.658 INFO [localhost-startStop-1] org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions Loading XML bean definitions from ServletContext resource [/WEB-INF/config/application-context.xml] FOUND!!!!!!!!!!!!!!!!!!!!class path resource [validation.xml]
The first thing I noticed is you're trying to acquire resources at the field level. In other words, this:
public class SomeClass{
ApplicationContext ctx = new ClassPathXmlApplicationContext();
private Resource resource = this.ctx.getResource("classpath:validation.xml");
}
Would make me think that application context isn't fully initialized.
After pulling down your project and checking, my assumption was correct. ApplicationContext
isn't fully initialized until after the classes are initialized. To get around this fact, you can use the @PostConstruct
annotation. Your class will now look like this:
@Configuration
public class Validator {
@Autowired
private ApplicationContext applicationContext;
private Resource validationResource;
@PostConstruct
public void init(){
validationResource = applicationContext.getResource("classpath*:validation.xml");
}
On a side note, I'd recommend you take some time to properly configure your pom. Ideally, someone should be able to pull down your code, build, and run. It took me a while to get your project running locally due to manual configuration.
You could try:
URL resource = ClassLoader.getSystemClassLoader().getResource("validation.xml");
If having a URL
would be usable to then get a Resource
from. It will return null if no resource is found.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.