开发者

Java, Spring, manually instantiating Spring bean, getting "FileNotFoundException: class path resource [WEB-INF/spring.properties] cannot be opened..."

开发者 https://www.devze.com 2023-02-19 15:23 出处:网络
So far I have just used Spring beans that are singletons but now I want to make it where I have a scope=\"prototype\" bean and instantiate it each time I need a new instance.

So far I have just used Spring beans that are singletons but now I want to make it where I have a scope="prototype" bean and instantiate it each time I need a new instance.

In the application context I have:

<bean id="processDoNewDocSearch_Spr" class="org.jadefalcon.demo.server.processes.ProcessDoNewDocSearch_Spr" scope="prototype"/>

The actual test bean class is:

public class ProcessDoNewDocSearch_Spr {

    @Resource(name = "savedsearchesService")
    private SavedSearchesService savedsearchesService;

    public ProcessDoNewDocSearch_Spr () {

    }

    public void Main () {
        SavedSearches ss1 = savedsearchesService.get(3);
        String test= ss1.getSearchname();
        System.out.print( "Search name: " + test);
    }


}

And I am trying to instantiate it like this and call the Main() method:

    ApplicationContext applicationContext = new ClassPathXmlApplicationContext ( "applicationContext.xml" );
    ProcessDoNewDocSearch_Spr processDoNewDocSearch_Spr = ( ProcessDoNewDocSearch_Spr ) applicationContext.getBean ( "processDoNewDocSearch_Spr" );
    processDoNewDocSearch_Spr.Main();

Its not working and I am getting some error about the file WEB-INF/spring.properties not being there, when it is, there is no problem with my application when I remove everything related to the ProcessDoNewDocSearch_Spr class, I am wondering if I am doing this correctly. Thanks

EDIT: Here is the error message I am getting:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [WEB-INF/spring.properties] cannot be opened because it does not exist
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause

org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [WEB-INF/spring.properties] cannot开发者_StackOverflow社区 be opened because it does not exist
    org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:78)
    org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:663)
    org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:638)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:407)

EDIT: In the root of my application I have a file called .classpath and in it I have, among other entries,

    <classpathentry kind="output" path="target/spring-hibernate-mysql/WEB-INF/applicationContext.xml"/>
<classpathentry kind="output" path="target/spring-hibernate-mysql/WEB-INF/spring.properties"/>
    <classpathentry kind="src" path="src/main/webapp/WEB-INF/spring.properties"/>
    <classpathentry kind="src" path="src/main/webapp/WEB-INF/applicationContext.xml"/>


When you are loading the applicationContext.xml file from the classpath, you must make sure that the applicationContext.xml file is in the runtime classpath for your application. Runtime classpath (as in, after deployment and/or outside of an IDE) is often different than the build time classpath.

Classpath info

On windows, your classpath is an environment variable that you set by accessing system advanced stuff. To keep your classpath from getting giant, I suggest that you create C:\lib and c:\classes directories. Add the c:\classes directory to the classpath and drop any classes in that directory. For jars, each jar must be listed on the classpath. I put them in c:\lib so I know where to look for them. then list each jar in the classpath.

Here is what applies to you. With the setup I described, you can drop your applicationContext.xml file in your c:\classes directory and it will also be in the class path.

0

精彩评论

暂无评论...
验证码 换一张
取 消