applicationcontext

How correctly close the ApplicationContext in Spring?

别来无恙 提交于 2019-11-28 08:37:59
I am studying for the Spring Core certification and I have some dount about on this question finded into the provided study material: What is the preferred way to close an application context? I know that if I have something like this: ConfigurableApplicationContext context = … // Destroy the application context.close(); by the use of the close() method on the context objet the ApplicationContext is closed and the application is destroyed. But I think that this is not the best way that I have to do it. Reading the official documentation I find that I can also do something like this: context

Reload or refresh a Spring application context inside a test method?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 08:30:27
I need to change the Spring profiles that are active in my applicationContext within a single method of my test class, and to do so I need to run one line of code before refreshing the contest because I am using a ProfileResolver. I have tried the following: @WebAppConfiguration @ContextConfiguration(locations = {"/web/WEB-INF/spring.xml"}) @ActiveProfiles(resolver = BaseActiveProfilesResolverTest.class) public class ControllerTest extends AbstractTestNGSpringContextTests { @Test public void test() throws Exception { codeToSetActiveProfiles(...); ((ConfigurableApplicationContext)this

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

好久不见. 提交于 2019-11-28 04:37:56
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(); Daff Since the tests will be instantiated like a Spring bean too, you just need to implement the ApplicationContextAware interface: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"/services-test-config.xml"}) public class

configure dataSource for liquibase in spring boot

与世无争的帅哥 提交于 2019-11-28 00:26:09
问题 I have a spring boot application and I want to add liquibase configuration change log for it. I have created a LiquibaseConfig class for configuring liquibase: @Configuration public class LiquibaseConfiguration { @Value("${com.foo.bar.liquibase.changelog}") private String changelog; @Autowired MysqlDataSource dataSource; @Bean public SpringLiquibase liquibase() { SpringLiquibase liquibase = new SpringLiquibase(); liquibase.setDataSource(dataSource); liquibase.setChangeLog(changelog); return

How do you reset Spring JUnit application context after a test class dirties it?

こ雲淡風輕ζ 提交于 2019-11-27 19:58:09
I'm using Spring 3.1.1.RELEASE, JUnit 4.8.1 and the HSQL 2.7.7 in-memory database. I have one test class annotated as @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({ "classpath:test-trainingSessionServiceContext.xml" }) @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) public class TrainingSessionServiceTest { The problem is, when I run "mvn clean test", it seems that all test classes run after the above class fail because the in-memory database is destroyed and not re-created. I get errors like org.hibernate.exception.SQLGrammarException: user lacks privilege or

Best Practise of injecting applicationContext in Spring3

本秂侑毒 提交于 2019-11-27 14:07:04
问题 As in the title above, I am confused about pros cons between injecting applicationContext by directly @Autowired annnotation or implementing ApplicationContextAware interface in a singleton spring bean. Which one do you prefer in which cases and why? Thanks. 回答1: Actually, both are bad. Both of them tie your application to the Spring framework, thus inverting the whole inversion-of-control concept. In an ideal world, your application should not be aware of being managed by an

Spring DI applicationContext.xml how exactly is xsi:schemaLocation used?

柔情痞子 提交于 2019-11-27 11:23:05
问题 Note: the test project I'm mentioning can be downloaded with: git clone https://github.com/mperdikeas/so-spring-di-appcontext-schemalocation.git .. and run with 'ant run'. I 'understand' that XML namespace names are just used as opaque identifiers and not meant to be used as URIs (wikipedia). I also 'understand' that the XML schema locations are meant to provide hints as to the actual location of schema documents and, being hints, not used in practice (w3.org). With that in mind I 've been

How to inject ApplicationContext itself

社会主义新天地 提交于 2019-11-27 07:10:40
I want to inject an ApplicationContext itself to a bean. Something like public void setApplicationContext(ApplicationContect context) { this.context = context; } Is that possible in spring? Previous comments are ok, but I usually prefer: @Autowired private ApplicationContext applicationContext; Johan Sjöberg Easy, using the ApplicationContextAware interface. public class A implements ApplicationContextAware { private ApplicationContext context; public void setApplicationContext(ApplicationContext context) { this.context = context; } } Then in your actual applicationContext you only need to

spring junit load application context for tests

大兔子大兔子 提交于 2019-11-27 06:34:23
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="/WEB-INF/dataSource.xml"/> <import resource="/WEB-INF/hibernate.xml"/> <import resource="/WEB-INF/beans.xml"/>

Spring cannot find bean xml configuration file when it does exist

不问归期 提交于 2019-11-27 02:57:38
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 because it does not exist but I don't get it, since I do the following code test: File f = new File("src/main