applicationcontext

How to access Spring context in jUnit tests annotated with @RunWith and @ContextConfiguration?

五迷三道 提交于 2019-11-27 00:33:18
问题 I have following test class @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"/services-test-config.xml"}) public class MySericeTest { @Autowired MyService service; ... } Is it possible to access services-test-config.xml programmatically in one of such methods? Like: ApplicationContext ctx = somehowGetContext(); 回答1: Since the tests will be instantiated like a Spring bean too, you just need to implement the ApplicationContextAware interface: @RunWith

Get application context returns null

大城市里の小女人 提交于 2019-11-26 20:42:37
The following schema has been touted as the way to get application context from anywhere within my android app. But sometimes doing MyApp.getContext() returns null. I tried changing the schema by removing static from getContext() so that I would do MyApp.getInstance().getContext() . It still returns null. How do I fix this? How do I get my application's context from anywhere within my app? public class MyApp extends Application { private static MyApp instance; public static MyApp getInstance() { return instance; } public static Context getContext() { return instance.getApplicationContext(); }

Spring cannot find bean xml configuration file when it does exist

房东的猫 提交于 2019-11-26 12:36:53
问题 I am trying to make my first bean in Spring but got a problem with loading a context. I have a configuration XML file of the bean in src/main/resources. I receive the following IOException: Exception in thread \"main\" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [src/main/resources/beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [src/main/resources/beans.xml] cannot be opened

spring junit load application context for tests

梦想与她 提交于 2019-11-26 12:04:22
问题 I\'ve got some XML files under my WEB-INF directory: lyricsBaseApp-servlet.xml hibernate.xml dataSource.xml beans.xml the servlet xml imports other xml files: <import resource=\"dataSource.xml\"/> <import resource=\"hibernate.xml\"/> <import resource=\"beans.xml\"/> I would like my junit4 JukeboxTest class to include entire spring configuration. Using default filename I have created a JukeboxTest-content.xml file. And finally, I do not know what to put there... I\'ve tried: <import resource=\

How to call a method after bean initialization is complete?

旧巷老猫 提交于 2019-11-26 04:34:53
问题 I have a use case where I need to call a (non-static) method in the bean only-once at the ApplicationContext load up. Is it ok, if I use MethodInvokingFactoryBean for this? Or we have a some better solution? As a side note, I use ConfigContextLoaderListener to load the Application Context in web application. And want, that if bean \'A\' is instantiated just call methodA() once. How can this be done nicely? 回答1: You can use something like: <beans> <bean id="myBean" class="..." init-method=

How to add a hook to the application context initialization event?

别等时光非礼了梦想. 提交于 2019-11-26 03:53:32
问题 For a regular Servlet, I guess you could declare a context listener, but for Spring MVC would Spring make this any easier? Furthermore, if I define a context listener and then would need to access the beans defined in my servlet.xml or applicationContext.xml , how would I get access to them? 回答1: Spring has some standard events which you can handle. To do that, you must create and register a bean that implements the ApplicationListener interface, something like this: package test.pack.age;

BeanFactory vs ApplicationContext

泄露秘密 提交于 2019-11-26 01:04:02
问题 I\'m pretty new to the Spring Framework, I\'ve been playing around with it and putting a few samples apps together for the purposes of evaluating Spring MVC for use in an upcoming company project. So far I really like what I see in Spring MVC, seems very easy to use and encourages you to write classes that are very unit test-friendly. Just as an exercise, I\'m writing a main method for one of my sample/test projects. One thing I\'m unclear about is the exact differences between BeanFactory

What is a NoSuchBeanDefinitionException and how do I fix it?

╄→гoц情女王★ 提交于 2019-11-25 23:58:20
问题 Please explain the following about NoSuchBeanDefinitionException exception in Spring: What does it mean? Under what conditions will it be thrown? How can I prevent it? This post is designed to be a comprehensive Q&A about occurrences of NoSuchBeanDefinitionException in applications using Spring. 回答1: The javadoc of NoSuchBeanDefinitionException explains Exception thrown when a BeanFactory is asked for a bean instance for which it cannot find a definition. This may point to a non-existing bean

How to change context root of a dynamic web project in Eclipse?

无人久伴 提交于 2019-11-25 23:32:49
问题 I have developed a dynamic web project in Eclipse. Now I can access it through my browser using the following URL: http://localhost:8080/MyDynamicWebApp Now I want to change the access URL to http://localhost:8080/app I changed the context root from the project \"Properties | Web Project Settings | Context Root\". But it is not working. The web app still has the access URL as earlier. I have re-deployed the application on Tomcat, re-started the Tomcat and have done everything that should be

What is the difference between ApplicationContext and WebApplicationContext in Spring MVC?

懵懂的女人 提交于 2019-11-25 23:12:45
问题 What is the difference between Application Context and Web Application Context? I am aware that WebApplicationContext is used for Spring MVC architecture oriented applications? I want to know what is the use of ApplicationContext in MVC applications? And what kind of beans are defined in ApplicationContext ? 回答1: Web Application context extended Application Context which is designed to work with the standard javax.servlet.ServletContext so it's able to communicate with the container. public