public static void main(String[] args) { logger.info("TestClient started"); // [ Aufgabe 1.6 GenericXmlApplicationContext context = new GenericXmlApplicationContext(); context.getEnvironment().setActiveProfiles("mysql"); context.load("/app/server/bean-definitions.xml"); context.refresh(); // ] Aufgabe 1.6 Define application context // [ Aufgabe 1.7 IBookService bean = context.getBean("bookService", IBookService.class); // ] Aufgabe 1.7 Receive the bookService from the application context BookSearchCriteria criteria = new BookSearchCriteria(); criteria.setPublicationYear(2007); // [ Aufgabe 1.8 List<Book> books = bean.findBySearchCriteria(criteria); // ] Aufgabe 1.8 Read books - List<Book> books = null; if (books != null) { logger.info("The following books have been found:"); for (Book book : books) { logger.info(book.getAuthor() + ": \"" + book.getTitle() + "\""); } } }
@Test public void should_read_properties_file_from_profile_a() { context.getEnvironment().setDefaultProfiles("default"); context.getEnvironment().setActiveProfiles("a"); context.load( "classpath:/com/github/jcgay/example/spring/ConfigureBeansWithPropertiesInProfileTest.xml"); context.refresh(); BeanWithProperties bean = context.getBean(BeanWithProperties.class); BeanWithPropertiesAnnotated annotated = context.getBean(BeanWithPropertiesAnnotated.class); assertThat(bean.getTest()).isEqualTo("a"); assertThat(annotated.getTest()).isEqualTo("a"); }