Spring @Value资源注入测试和主要资源

时间:2018-05-25 12:42:26

标签: java spring

我有一个Spring服务,它从类路径中读取XML文件。 XML资源由@Value注入,如

@Value("classpath:xyz/*.xyz.xml")
private Resource[] xyzResources;

现在,在单元测试中,这将找到放置在 src / test / resource 下但不在 src / main / resources 下的XML文件。 为什么?如何从测试环境中的两个资源文件夹中获取文件?

My Spring版本为5.0+,spring boot 2.0 +。

1 个答案:

答案 0 :(得分:0)

在评论中解决了:

public partial class Form1 : Form
{
    private Timer timer;

    private TrafficLight[] lights;
    private Panel[] light_panels;

    public Form1()
    {
        InitializeComponent();
        timer = new Timer();

        timer.Tick += timer_Tick;

        timer.Interval = 2000;
        timer.Enabled = true;

        timer.Start();

        lights = new TrafficLight[]
        {
            new TrafficLight(10, 10, 75, 150, 0),
            new TrafficLight(50, 50, 75, 150, 2)
        };

        light_panels = new Panel[]
        {
            new Panel(),
            new Panel()
        };

        for (int i = 0; i < light_panels.Length; ++i)
        {
            light_panels[i].Width = lights[i].Width;
            light_panels[i].Height = lights[i].Height;
            light_panels[i].Location = new Point(lights[i].X, lights[i].Y);
            Controls.Add(light_panels[i]);
            light_panels[i].Paint += PLight_Paint;
        }

        CenterToScreen();
    }

    private void PLight_Paint(object sender, PaintEventArgs e)
    {
        Panel p = sender as Panel;

        int index = 0;
        for (int i = 0; i < light_panels.Length; ++i)
        {
            if (Object.ReferenceEquals(light_panels[i], p))
            {
                index = i;
                break;
            }
        }

        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

        e.Graphics.FillRectangle(new SolidBrush(Color.Gray), lights[index].X, lights[index].Y, lights[index].Width, lights[index].Height);

        switch (lights[index].State)
        {
            case 0:
                e.Graphics.FillEllipse(new SolidBrush(Color.Red), lights[index].X + 10, lights[index].Y + 5, lights[index].Width - 30, lights[index].Height / 3 - 10);
                e.Graphics.FillEllipse(new SolidBrush(Color.Black), lights[index].X + 10, lights[index].Y + lights[index].Height / 3, lights[index].Width - 30, lights[index].Height / 3 - 10);
                e.Graphics.FillEllipse(new SolidBrush(Color.Black), lights[index].X + 10, lights[index].Y + lights[index].Height / 3 * 2 - 5, lights[index].Width - 30, lights[index].Height / 3 - 10);
                break;
            case 1:
                e.Graphics.FillEllipse(new SolidBrush(Color.Red), lights[index].X + 10, lights[index].Y + 5, lights[index].Width - 30, lights[index].Height / 3 - 10);
                e.Graphics.FillEllipse(new SolidBrush(Color.Orange), lights[index].X + 10, lights[index].Y + lights[index].Height / 3, lights[index].Width - 30, lights[index].Height / 3 - 10);
                e.Graphics.FillEllipse(new SolidBrush(Color.Black), lights[index].X + 10, lights[index].Y + lights[index].Height / 3 * 2 - 5, lights[index].Width - 30, lights[index].Height / 3 - 10);
                break;
            case 2:
                e.Graphics.FillEllipse(new SolidBrush(Color.Black), lights[index].X + 10, lights[index].Y + 5, lights[index].Width - 30, lights[index].Height / 3 - 10);
                e.Graphics.FillEllipse(new SolidBrush(Color.Black), lights[index].X + 10, lights[index].Y + lights[index].Height / 3, lights[index].Width - 30, lights[index].Height / 3 - 10);
                e.Graphics.FillEllipse(new SolidBrush(Color.Green), lights[index].X + 10, lights[index].Y + lights[index].Height / 3 * 2 - 5, lights[index].Width - 30, lights[index].Height / 3 - 10);
                break;
            case 3:
                e.Graphics.FillEllipse(new SolidBrush(Color.Black), lights[index].X + 10, lights[index].Y + 5, lights[index].Width - 30, lights[index].Height / 3 - 10);
                e.Graphics.FillEllipse(new SolidBrush(Color.Orange), lights[index].X + 10, lights[index].Y + lights[index].Height / 3, lights[index].Width - 30, lights[index].Height / 3 - 10);
                e.Graphics.FillEllipse(new SolidBrush(Color.Black), lights[index].X + 10, lights[index].Y + lights[index].Height / 3 * 2 - 5, lights[index].Width - 30, lights[index].Height / 3 - 10);
                break;
        }
    }

    private void timer_Tick(object sender, EventArgs e)
    {
        for (int i = 0; i < lights.Length; ++i)
        {
            lights[i].Switch();
            light_panels[i].Invalidate();
            light_panels[i].Update();
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }
}

@Value("classpath*:xyz/*.xyz.xml") private Resource[] xyzResources; 后面的*成功了。请参阅herehere