spring-junit的标注总结

如果在测试类的类名上面添加了注解

@ContextConfiguration("meta/springConfigured.xml")

如何在标注了@Test的方法里面获取上面xml文件中的配置?

 

package aoptest;



import org.junit.Assert;

import org.junit.Before;

import org.junit.Test;

import org.junit.runner.RunWith;



import aoptest.ShouldBeConfiguredBySpring;



import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.ContextLoader;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;



/**

 * @author Administrator

 *

 */

@RunWith(SpringJUnit4ClassRunner.class)//必须有,如果去掉,下面的ctx就不能注入

@ContextConfiguration("/meta/springConfigured.xml")

public class TestAop {



    @Autowired

    protected ApplicationContext ctx;

    

    @Test

    public void aopTest(){

        //第一种方法

        //ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("meta/springConfigured.xml");

        //ShouldBeConfiguredBySpring myObject = (ShouldBeConfiguredBySpring) context.getBean("configuredBean");

        //System.out.println(myObject.getName()+",wj");

        //AopOperation aopOperation = (AopOperation) context.getBean("aopOperationBean");

        //aopOperation.queryOpLog();

        //第二种方法

        ShouldBeConfiguredBySpring myObject = (ShouldBeConfiguredBySpring) ctx.getBean("configuredBean");//必须是spring的bean才能拦截,自己定义的类springaop无法拦截

        myObject.TestAop();

        

    }

}

 

参考http://www.coderli.com/junit-spring-test-applicationcontext

你可能感兴趣的:(spring)