获取spring所有propertysources和key-value

Life is short , play more!
本文来自lihao's Blog,转载请注明。

	@Test
	public void getallProperties() {

		System.out.println(a.getEnvironment());
		AbstractEnvironment env = (AbstractEnvironment) a.getEnvironment();

		if (env.getPropertySources().size() <= 0)
			System.out.println("===zero size ====");
		// AbstractEnvironment env;

		for (Iterator it = env.getPropertySources().iterator(); it.hasNext();) {

			Object propertysource = it.next();

			if (propertysource instanceof CompositePropertySource) {

				System.out.println("================");
				CompositePropertySource propertySource = (CompositePropertySource) propertysource;

				System.out.println(propertySource.getName());

				propertySource.getPropertySources().stream().forEach(property -> {

					System.out.println("------------------------");
					System.out.print("compositeProperty:  " + property);
					System.out.println("------------------------");

				});

				System.out.println("================");

			}

			else if (propertysource instanceof MapPropertySource) {
				MapPropertySource propertySource = (MapPropertySource) propertysource;
				System.out.println("================");
				System.out.println(propertySource.getName());

				String[] properties = propertySource.getPropertyNames();

				int size = properties.length;

				for (int a = 0; a < size; a++) {

					System.out.println("------------------------");
					System.out.println(
							"mapProperty :  " + properties[a] + " = " + propertySource.getProperty(properties[a]));

					System.out.println("------------------------");

				}

				System.out.println("================");

			}

			else if (propertysource instanceof RandomValuePropertySource) {
				RandomValuePropertySource propertySource = (RandomValuePropertySource) propertysource;

				System.out.println("randomvalue " + propertySource.toString());

			}

			else if (propertysource instanceof EndpointWebMvcAutoConfiguration) {
				EndpointWebMvcAutoConfiguration propertySource = (EndpointWebMvcAutoConfiguration) propertysource;

				System.out.println("EndpointWebMvcAutoConfiguration " + propertySource.toString());

			}

			else {
				System.out.println("other properties");
				MapPropertySource propertySource = (MapPropertySource) propertysource;

			}

		}

	}

 


发表评论

电子邮件地址不会被公开。 必填项已用*标注