Tag: applicationcontext

无法使用@ContextConfiguration加载ApplicationContext(classes = {…})

我正在尝试使用Java和注释配置运行一些弹簧测试。 我在测试包中有两个配置类: @Configuration @ComponentScan(basePackages = “com.mypackages”) public class TestConfig { @Bean public MyService getMyService() { return new MyServiceImpl(); } } @Configuration @EnableTransactionManagement public class JpaTestConfig { @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(){ LocalContainerEntityManagerFactoryBean lcemfb = new LocalContainerEntityManagerFactoryBean(); lcemfb.setDataSource(this.dataSource()); lcemfb.setPackagesToScan(new String[] {“com.mypackages”}); lcemfb.setPersistenceUnitName(“MyTestPU”); HibernateJpaVendorAdapter va = new HibernateJpaVendorAdapter(); lcemfb.setJpaVendorAdapter(va); Properties ps = new Properties(); ps.put(“hibernate.dialect”, “org.hibernate.dialect.HSQLDialect”); ps.put(“hibernate.hbm2ddl.auto”, “create”); […]

从applicationContext.xml中读取环境变量

我需要读取我的web.xml中定义的环境变量 Path Repositorio NFS PATH_ENV java.lang.String C:/V3 来自我的applicationContext.xml 我怎样才能做到这一点 ? 最后我做了下一个: 1在context.xml中定义环境变量: 2在web.xml中定义env-entry Path Repositorio NFS PATH_ENV java.lang.String /WEB-INF/ 3在applicationContext.xml中定义 java:comp/env/PATH_ENV 这是正确运行的,但是如果我在以下位置定义完整路径: C:/V3/ 我有下一个问题: java.io.FileNotFoundException: Could not open ServletContext resource [/C:/V3/aesantasa.properties] 我无法在env-entry-value中定义完整路径为什么?

PropertyPlaceholderConfigurer和.properties文件中的环境变量

我有一个带有PropertyPlaceholderConfigurer的Spring application-context.xml,用于从.properties文件中获取属性值。 主文件夹和测试源文件夹具有单独的.properties文件。 问题是我需要在.properties文件中使用环境变量。 但是,当我按以下方式执行此操作时: property.name=${env.SYSTEM_PROPERTY} 我收到以下错误: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name ‘beanName’ defined in class path resource [com/example/applicationContext.xml]: Could not resolve placeholder ‘env.SYSTEM_PROPERTY’ 而占位符配置器定义为 任何想法如何使property.name被解释为环境变量(而不是占位符)? 最好的问候,德米特里。

修改活动配置文件并在Spring Boot应用程序中刷新ApplicationContext运行时

我有一个Spring启动Web应用程序。 使用@Configurable注释通过java类配置应用程序。 我介绍了两个配置文件:’install’,’normal’。 如果安assembly置文件处于活动状态,则不会加载任何需要数据库连接的Bean。 我想创建一个控制器,用户可以在其中设置db连接参数,完成后我想将活动配置文件从’install’切换到’normal’并刷新应用程序上下文,这样Spring就可以初始化每个需要的bean数据库数据源。 我可以从代码中修改活动配置文件列表,没有问题,但是当我尝试刷新应用程序上下文时,我得到以下exception : `java.lang.IllegalStateException: GenericApplicationContext does not support multiple refresh attempts: just call ‘refresh’ once` 这是我启动Spring启动应用程序的方式: `new SpringApplicationBuilder().sources(MyApp.class) .profiles(“my-profile”).build().run(args);` 有没有人知道如何启动春季启动应用程序让你多次刷新应用程序上下文?

ApplicationContext和ServletContext

当谈到Spring MVC Application时,我对两个ApplicationContext和ServletContext感到困惑。 我知道每个Spring Web应用程序只有一个ApplicationContext,每个Web应用程序也只有一个ServletContext。 要在web.xml中启动ApplicationContext和ServletContext的值,我们将在context-param标记中添加一些内容。 这让我感到困惑。 这两者之间有什么区别 (我知道ApplicationContext有一些使用bean的方法)? 当我们使用ApplicationContext 时何时使用ServletContext ?

在测试方法中重新加载或刷新Spring应用程序上下文?

我需要在我的测试类的单个方法中更改我的applicationContext中活动的Spring配置文件,为此我需要在刷新竞赛之前运行一行代码,因为我使用的是ProfileResolver。 我尝试过以下方法: @WebAppConfiguration @ContextConfiguration(locations = {“/web/WEB-INF/spring.xml”}) @ActiveProfiles(resolver = BaseActiveProfilesResolverTest.class) public class ControllerTest extends AbstractTestNGSpringContextTests { @Test public void test() throws Exception { codeToSetActiveProfiles(…); ((ConfigurableApplicationContext)this.applicationContext).refresh(); … tests here … codeToSetActiveProfiles(… back to prior profiles …); … ideally refresh/reload the context for future tests } } 但我得到: java.lang.IllegalStateException: GenericApplicationContext does not support multiple refresh attempts: just call […]