Java Code Examples for org.springframework.context.support.ClassPathXmlApplicationContext#getBean()

The following examples show how to use org.springframework.context.support.ClassPathXmlApplicationContext#getBean() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConfigTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testInitReference() throws Exception {
    ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/demo-provider.xml");
    providerContext.start();
    try {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/init-reference.xml");
        ctx.start();
        try {
            DemoService demoService = (DemoService)ctx.getBean("demoService");
            assertEquals("say:world", demoService.sayName("world"));
        } finally {
            ctx.stop();
            ctx.close();
        }
    } finally {
        providerContext.stop();
        providerContext.close();
    }
}
 
Example 2
Source File: AroundAdviceBindingTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Before
public void onSetUp() throws Exception {
	ctx = new ClassPathXmlApplicationContext(getClass().getSimpleName() + ".xml", getClass());

	AroundAdviceBindingTestAspect  aroundAdviceAspect = ((AroundAdviceBindingTestAspect) ctx.getBean("testAspect"));

	ITestBean injectedTestBean = (ITestBean) ctx.getBean("testBean");
	assertTrue(AopUtils.isAopProxy(injectedTestBean));

	this.testBeanProxy = injectedTestBean;
	// we need the real target too, not just the proxy...

	this.testBeanTarget = (TestBean) ((Advised) testBeanProxy).getTargetSource().getTarget();

	mockCollaborator = mock(AroundAdviceBindingCollaborator.class);
	aroundAdviceAspect.setCollaborator(mockCollaborator);
}
 
Example 3
Source File: ConfigTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Test
public void testInitReference() throws Exception {
    ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/demo-provider.xml");
    providerContext.start();
    try {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/init-reference.xml");
        ctx.start();
        try {
            DemoService demoService = (DemoService) ctx.getBean("demoService");
            assertEquals("say:world", demoService.sayName("world"));
        } finally {
            ctx.stop();
            ctx.close();
        }
    } finally {
        providerContext.stop();
        providerContext.close();
    }
}
 
Example 4
Source File: DubboNamespaceHandlerTest.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomParameter() {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/customize-parameter.xml");
    ctx.start();

    ProtocolConfig protocolConfig = ctx.getBean(ProtocolConfig.class);
    assertThat(protocolConfig.getParameters().size(), is(1));
    assertThat(protocolConfig.getParameters().get("protocol-paramA"), is("protocol-paramA"));

    ServiceBean serviceBean = ctx.getBean(ServiceBean.class);
    assertThat(serviceBean.getParameters().size(), is(1));
    assertThat(serviceBean.getParameters().get("service-paramA"), is("service-paramA"));
}
 
Example 5
Source File: SerializationSwitchThreadConsumer.java    From dubbo-samples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/serialization-switch-thread-consumer.xml");
    context.start();

    DemoService demoService = context.getBean("demoService", DemoService.class);
    System.out.println(demoService.sayHello("Dubbo"));
}
 
Example 6
Source File: BeanNamePointcutAtAspectTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@org.junit.Before
public void setup() {
	ClassPathXmlApplicationContext ctx =
			new ClassPathXmlApplicationContext(getClass().getSimpleName() + ".xml", getClass());

	counterAspect = (CounterAspect) ctx.getBean("counterAspect");
	testBean1 = (ITestBean) ctx.getBean("testBean1");
	testBean3 = (ITestBean) ctx.getBean("testBean3");
}
 
Example 7
Source File: Application.java    From Spring with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
	final ClassPathXmlApplicationContext context =
			new ClassPathXmlApplicationContext("spring/application-context.xml");
	bookRepository = context.getBean(BookRepository.class);

	/*CRUD*/
	create();
	read();
	update();
	delete();
}
 
Example 8
Source File: JdbcNamespaceIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void createAndDestroyNestedWithHsql() throws Exception {
	ClassPathXmlApplicationContext context = context("jdbc-destroy-nested-config.xml");
	try {
		DataSource dataSource = context.getBean(DataSource.class);
		JdbcTemplate template = new JdbcTemplate(dataSource);
		assertNumRowsInTestTable(template, 1);
		context.getBean(EmbeddedDatabaseFactoryBean.class).destroy();
		expected.expect(BadSqlGrammarException.class); // Table has been dropped
		assertNumRowsInTestTable(template, 1);
	}
	finally {
		context.close();
	}
}
 
Example 9
Source File: ZkPropertyPlaceholderConfigurerTest.java    From cloud-config with MIT License 5 votes vote down vote up
@Test
public void testMultipleRemotePropertyPathWithLocalOverride() {
    applicationContext = new ClassPathXmlApplicationContext("classpath:zkprops3-context.xml");
    SampleBean mailBean = applicationContext.getBean("mailBean", SampleBean.class);
    assertThat(mailBean.getHost(), equalTo("a"));
    assertThat(mailBean.getPort(), equalTo(25));

    SampleBean queryBean = applicationContext.getBean("queryBean", SampleBean.class);
    assertThat(queryBean.getHost(), equalTo("a"));
    assertThat(queryBean.getPort(), equalTo(4321));
}
 
Example 10
Source File: DirectConsumer.java    From dubbo-samples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-direct-consumer.xml");
    context.start();
    DirectService directService = (DirectService) context.getBean("directService");
    String hello = directService.sayHello("world");
    System.out.println(hello);
}
 
Example 11
Source File: DubboOrderServiceStarter.java    From seata-samples with Apache License 2.0 5 votes vote down vote up
/**
 * The entry point of application.
 *
 * @param args the input arguments
 */
public static void main(String[] args) {
    /**
     *  3. Order service is ready . Waiting for buyers to order
     */
    ClassPathXmlApplicationContext orderContext = new ClassPathXmlApplicationContext(
        new String[] {"spring/dubbo-order-service.xml"});
    orderContext.getBean("service");
    new ApplicationKeeper(orderContext).keep();
}
 
Example 12
Source File: Main.java    From sharding-jdbc-1.5.1 with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) {
    // CHECKSTYLE:ON
    ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("META-INF/jpa/mysql/jpaContext.xml");
    OrderRepository orderRepository = applicationContext.getBean(OrderRepository.class);
    List<Long> orderIds = new ArrayList<>(10);
    for (int i = 0; i < 10; i++) {
        Order order = new Order();
        order.setUserId(51);
        order.setStatus("INSERT_TEST");
        orderRepository.create(order);
        orderIds.add(order.getOrderId());
        System.out.println(orderRepository.selectById(order.getOrderId()));
        System.out.println("--------------");
        order.setStatus("UPDATE_TEST");
        orderRepository.update(order);
        System.out.println(orderRepository.selectById(order.getOrderId()));
        System.out.println("--------------");
    }

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

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

    for (Long each : orderIds) {
        orderRepository.delete(each);
    }
    applicationContext.close();
}
 
Example 13
Source File: AspectJAutoProxyCreatorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testTwoAdviceAspectPrototype() {
	ClassPathXmlApplicationContext bf = newContext("twoAdviceAspectPrototype.xml");

	ITestBean adrian1 = (ITestBean) bf.getBean("adrian");
	testAgeAspect(adrian1, 0, 1);
	ITestBean adrian2 = (ITestBean) bf.getBean("adrian");
	assertNotSame(adrian1, adrian2);
	testAgeAspect(adrian2, 0, 1);
}
 
Example 14
Source File: EchoConsumer.java    From dubbo-samples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/echo-consumer.xml");
    context.start();
    DemoService demoService = context.getBean("demoService", DemoService.class);

    EchoService echoService = (EchoService) demoService;
    String status = (String) echoService.$echo("OK");
    System.out.println("echo result: " + status);
}
 
Example 15
Source File: Application.java    From dubbo-samples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-consumer.xml");
    context.start();
    DemoService demoService = context.getBean("demoService", DemoService.class);
    String hello = demoService.sayHello("world");
    System.out.println("result: " + hello);
    GreetingService greetingService = context.getBean("greetingService", GreetingService.class);
    System.out.print("greetings: " + greetingService.hello());
}
 
Example 16
Source File: AspectJAutoProxyCreatorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testAdviceUsingJoinPoint() {
	ClassPathXmlApplicationContext bf = newContext("usesJoinPointAspect.xml");

	ITestBean adrian1 = (ITestBean) bf.getBean("adrian");
	adrian1.getAge();
	AdviceUsingThisJoinPoint aspectInstance = (AdviceUsingThisJoinPoint) bf.getBean("aspect");
	//(AdviceUsingThisJoinPoint) Aspects.aspectOf(AdviceUsingThisJoinPoint.class);
	//assertEquals("method-execution(int TestBean.getAge())",aspectInstance.getLastMethodEntered());
	assertTrue(aspectInstance.getLastMethodEntered().indexOf("TestBean.getAge())") != 0);
}
 
Example 17
Source File: AspectJExpressionPointcutAdvisorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setup() {
	ClassPathXmlApplicationContext ctx =
			new ClassPathXmlApplicationContext(getClass().getSimpleName() + ".xml", getClass());
	testBean = (ITestBean) ctx.getBean("testBean");
	interceptor = (CallCountingInterceptor) ctx.getBean("interceptor");
}
 
Example 18
Source File: DefaultSequcenceTest.java    From tddl5 with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() throws Exception {
    context = new ClassPathXmlApplicationContext(new String[] { "classpath:spring-context-old.xml" });
    sequence = (Sequence) context.getBean("sequence");
}
 
Example 19
Source File: InitializeDatabaseIntegrationTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void testScriptNameWithExpressions() throws Exception {
	context = new ClassPathXmlApplicationContext("org/springframework/jdbc/config/jdbc-initialize-expression-config.xml");
	DataSource dataSource = context.getBean("dataSource", DataSource.class);
	assertCorrectSetup(dataSource);
}
 
Example 20
Source File: XMLConfigAnnotationTest.java    From apollo with Apache License 2.0 4 votes vote down vote up
private <T> T getBean(String xmlLocation, Class<T> beanClass) {
  ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(xmlLocation);

  return context.getBean(beanClass);
}