The compiler can not find the resource file - java

If I move the resource file to the resource folder, it will not be found.
However, when the resource file is placed in the main folder, it is recognized.
I did not understand why. Please help me.
Why do not they recognize it when they're in a resource folder?
main file
ApplicationContextExam01.java
package kr.or.connect.diexam01;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ApplicationContextExam01 {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
System.out.println("init!!");
UserBean userBean = (UserBean)ac.getBean("userBean");
userBean.setName("sakura");
System.out.println(userBean.getName());
}
}
resource file
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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userBean" class="kr.or.connect.diexam01.UserBean"></bean>
</beans>

Your resource file should be placed in the default package that is your "src" folder. and when starting your context you just call your file by
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

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 {
}

Spring-boot: load configuration file with #ImportResource error

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?

spring sample application in netbeans

This is my code. I have gotten some errors. Can anyone help me solve the problem? I get some casting errors, like cast ClassPathResource to Resource and resource object r to resource.
package firstspring;
import javax.annotation.Resource;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class test {
public static void main(String[] args){
Resource r =new ClassPathResource("applicationContext.xml");
BeanFactory bf=new XmlBeanFactory(r);
Student stud=(Student)bf.getBean("first");
stud.display();
}
}
This is my 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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="first" class="Student">
<property name="name" value="sneha"></property>
</bean>
</beans>
You are using both the resources like javax.annotation.Resource and org.springframework.core.io.Resource, which has resulted in a conflict.
You need to use them like this
org.springframework.core.io.Resource r = new ClassPathResource("applicationContext.xml");
then program will execute.
Instead of importing
javax.annotation.Resource
use,
org.springframework.core.io.Resource

HelloWorld Spring application throws Exception in thread "main": java.lang.NoClassDefFound

I am trying to develop a Spring "HelloWorld" project while I am runing that application it gives me this error:
INFO: Loading XML bean definitions from class path resource [Beans.xml] Exception in thread "main" java.lang.NoClassDefFoundError: org.springframework.expression.ExpressionParser
Below is my code:
package com.tutorialspoint;
public class HelloWorld {
private String message;
public void getMessage() {
System.out.println("your message : "+message);
}
public void setMessage(String message) {
this.message = message;
}
}
and
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld ");
obj.getMessage();
// TODO Auto-generated method stub
}
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="helloWorld" class="com.tutorialspoint/HelloWorld">
<property name="message" value="Hello World!"/>
</bean>
</beans>
Apart from adding all the dependent jars it seems like the class defined within the bean definition is invalid
com.tutorialspoint/HelloWorld
^
make it,
<bean id="helloWorld" class="com.tutorialspoint.HelloWorld">
<property name="message" value="Hello World!"/>
</bean>
also context.getBean("helloWorld ") should be changed to context.getBean("helloWorld")
include
spring-expression-3.1.1.Release.jar
under lib folder
I mentioned 3.1.1 as example you can use latest version, if any exist.
Add the spring dependencies to you class-path
If you´re not using Maven, as M Sach says, add the "spring expression-X.Release.jar" to your "lib" folder and also DON´T forget to add it to the build path in Eclipse
This Exception related to spring-expression.jar add this.
add below dependency in your pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.3.8</version>
</dependency>

Categories