View Javadoc
1   /**
2    * Copyright 2010-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util.spring.context;
17  
18  import java.util.Arrays;
19  import java.util.Collections;
20  import java.util.List;
21  import java.util.Properties;
22  
23  import org.junit.Test;
24  import org.kuali.common.util.PropertyUtils;
25  import org.kuali.common.util.log.LoggerUtils;
26  import org.kuali.common.util.spring.PropertySourceUtils;
27  import org.slf4j.Logger;
28  import org.springframework.context.annotation.AnnotationConfigApplicationContext;
29  import org.springframework.core.env.ConfigurableEnvironment;
30  
31  import com.google.common.collect.Lists;
32  
33  public class ContextTest {
34  
35  	private static final Logger logger = LoggerUtils.make();
36  
37  	@Test
38  	public void test() {
39  		try {
40  			Properties source = PropertyUtils.load("classpath:org/kuali/common/util/spring/context/breakfast.properties");
41  			AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
42  			ConfigurableEnvironment env = ctx.getEnvironment();
43  			PropertySourceUtils.reconfigurePropertySources(env, "properties", source);
44  			ctx.register(CerealConfig.class, MilkConfig.class);
45  			ctx.refresh();
46  			String[] beanNames = ctx.getBeanDefinitionNames();
47  			logger.info("{} beans in this application context", beanNames.length);
48  			List<String> names = Lists.newArrayList(Arrays.asList(beanNames));
49  			Collections.sort(names);
50  			for (int i = 0; i < names.size(); i++) {
51  				String name = names.get(i);
52  				Object bean = ctx.getBean(name);
53  				logger.info(" " + (i + 1) + "  [{}]=[{}]", name, bean.getClass().getCanonicalName());
54  			}
55  			ctx.close();
56  		} catch (Exception e) {
57  			e.printStackTrace();
58  		}
59  	}
60  
61  }