春季上下文和自动配置要复制生产进行测试?

发布于 2025-01-21 21:46:31 字数 1114 浏览 0 评论 0原文

我需要编写一个将数据从生产到测试环境复制的实用程序,以准备大型重构之前测试应用程序的数据库。

我确实需要重新使用现有的应用程序配置文件不变,这样我就不会在建立测试基线之前对自己进行重构。

我的第一个尝试是尝试实例化与每个环境相对应的不同配置上下文的相同豆类。

这是我的第一次尝试:

public class PrepareDatabase {

    @Component
    static class Context {
        @Autowired UserInfoSecurity userSec;
        @Autowired UserDao userDao;         
    }

    public static void main(String[] args) {
        
        try(ClassPathXmlApplicationContext prodContext =
                new ClassPathXmlApplicationContext("classpath:/prod/web-client-env.xml");    
            ClassPathXmlApplicationContext testContext =
                new ClassPathXmlApplicationContext("classpath:/test/web-client-env.xml")) {

            Context prod = prodContext.getBean(Context.class);
            Context test = testContext.getBean(Context.class);

            Stream<UserDso> users = prod.userDao.scan().map(prod.userSec::sanitize);
            test.userDao.batchPutItems(users);
        }                   
    }    
}

bean'...

这是没有合格的类型'...准备的 被扫描。

但是我该如何解决?我不确定这甚至是正确的方法。

I need to write a utility that copies data from a Production to a Test environment to prepare a database for testing the application prior to a large refactoring.

I really need to re-use the existing application config files unchanged, so that I don't find myself refactoring prior to establishing a testing baseline.

My first attempt is to try to instantiate the same beans with different configuration contexts corresponding to each environment.

Here is my first attempt:

public class PrepareDatabase {

    @Component
    static class Context {
        @Autowired UserInfoSecurity userSec;
        @Autowired UserDao userDao;         
    }

    public static void main(String[] args) {
        
        try(ClassPathXmlApplicationContext prodContext =
                new ClassPathXmlApplicationContext("classpath:/prod/web-client-env.xml");    
            ClassPathXmlApplicationContext testContext =
                new ClassPathXmlApplicationContext("classpath:/test/web-client-env.xml")) {

            Context prod = prodContext.getBean(Context.class);
            Context test = testContext.getBean(Context.class);

            Stream<UserDso> users = prod.userDao.scan().map(prod.userSec::sanitize);
            test.userDao.batchPutItems(users);
        }                   
    }    
}

This fails with No qualifying bean of type '...PrepareDataba se$Context' available

I think I understand what's happening: the PrepareDatabase class is not being scanned.

But how can I fix this? I am not confident that this is even the right approach to begin with.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

李白 2025-01-28 21:46:31

通过切换到AnnotationConfigapplicationContext来使其起作用。

代码的相关部分如下所示:

try(AnnotationConfigApplicationContext prodContext =
        new AnnotationConfigApplicationContext();

        AnnotationConfigApplicationContext testContext =
        new AnnotationConfigApplicationContext()) {

    System.setProperty("config_path", "classpath:/prod");
    prodContext.register(MyApp.class);
    prodContext.refresh();
    
    System.setProperty("config_path", "classpath:/test");
    testContext.register(MyApp.class);
    testContext.refresh();
    
    Context prod = prodContext.getBean(Context.class);
    Context test = testContext.getBean(Context.class);

的内容导入正确的特定环境特定文件

<import resource="${config_path}/some_file_name.xml"/>

ymmv-在我的情况下,config_path变量已在已经在各个地方使用,以用类似

@PropertySources({@PropertySource("${config_path}/backend.properties"),

Got it working by switching to AnnotationConfigApplicationContext.

The relevant section of code looks like this:

try(AnnotationConfigApplicationContext prodContext =
        new AnnotationConfigApplicationContext();

        AnnotationConfigApplicationContext testContext =
        new AnnotationConfigApplicationContext()) {

    System.setProperty("config_path", "classpath:/prod");
    prodContext.register(MyApp.class);
    prodContext.refresh();
    
    System.setProperty("config_path", "classpath:/test");
    testContext.register(MyApp.class);
    testContext.refresh();
    
    Context prod = prodContext.getBean(Context.class);
    Context test = testContext.getBean(Context.class);

YMMV - in my case the config_path variable is used in various places already to import the correct environment-specific files with something like this

<import resource="${config_path}/some_file_name.xml"/>

and this

@PropertySources({@PropertySource("${config_path}/backend.properties"),
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文