AnnotationConfigApplicationContext

【AnnotationConfigApplicationContext】别裁伪体亲风雅,转益多师是汝师。这篇文章主要讲述AnnotationConfigApplicationContext相关的知识,希望能为你提供帮助。

package com.test; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.Properties; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.core.env.PropertiesPropertySource; import org.springframework.core.env.PropertySource; public class ReportRunner {public static void main(String... args) { try { new ReportRunner().parseAndExecute(); } catch (Exception e) { e.printStackTrace(); } }private void parseAndExecute() throws ClassNotFoundException, IOException { prepareAndExecute("uat.properties","com.test.TestReportContext"); }private void prepareAndExecute(String propName, String contextClassName) throws IOException, ClassNotFoundException { //log.info("Preparing execute report runner, properties name: {}, contextClassName: {}", propName, //contextClassName); URL url = getClass().getClassLoader().getResource(propName); if (url == null) { //log.error("Properties name is wrong, cannot find resource with name: {}", propName); return; } // log the properties which used in the jar Properties properties = getProperties(url); Class contextClass = Class.forName(contextClassName); execute(properties, contextClass); }private void execute(Properties props, Class contextClass) { //log.info("Starting execute report runner"); PropertySource< ?> ps = new PropertiesPropertySource("main", props); buildContextAndRun(ps, contextClass); //log.info("Stopping report runner"); }private Properties getProperties(URL url) throws IOException { try (InputStream is = url.openStream()) { Properties properties = new Properties(); properties.load(is); //log.info("All defined properties: {}", properties); return properties; } }/** * First build {@link AnnotationConfigApplicationContext} with contextClass, then build and send * report. */ private void buildContextAndRun(PropertySource ps, Class contextClass) { try (AnnotationConfigApplicationContext reportContext = new AnnotationConfigApplicationContext()) { reportContext.getEnvironment().getPropertySources().addLast(ps); reportContext.register(contextClass); reportContext.refresh(); TestTopology testTopology = reportContext.getBean(TestTopology.class); System.out.println(testTopology); } } }

package com.test; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @Configuration @Import({ Person.class, Student.class, TestTopology.class }) public class TestReportContext { /* @Bean public Person testPerson() { return new Person(); }@Bean public Student testStudent() { return new Student(); }@Bean public TestTopology testTopology() { return new TestTopology(); }*/ }

package com.test; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; public class TestTopology {//@Resource @Autowired private Person testPerson; //@Resource @Autowired private Student testStudent; }

package com.test; public class Student { public void say() { System.out.println("hello"); } }

package com.test; import javax.annotation.Resource; public class Person { @Resource private Student student; public void say() { this.student.say(); } }

 

    推荐阅读