春季上下文和自动配置要复制生产进行测试?
我需要编写一个将数据从生产到测试环境复制的实用程序,以准备大型重构之前测试应用程序的数据库。
我确实需要重新使用现有的应用程序配置文件不变,这样我就不会在建立测试基线之前对自己进行重构。
我的第一个尝试是尝试实例化与每个环境相对应的不同配置上下文的相同豆类。
这是我的第一次尝试:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过切换到
AnnotationConfigapplicationContext
来使其起作用。代码的相关部分如下所示:
的内容导入正确的特定环境特定文件
ymmv-在我的情况下,
config_path
变量已在已经在各个地方使用,以用类似Got it working by switching to
AnnotationConfigApplicationContext
.The relevant section of code looks like this:
YMMV - in my case the
config_path
variable is used in various places already to import the correct environment-specific files with something like thisand this