为什么当我尝试从属性文件输出数据时,显示的数据是错误的?
在我的ChatApp项目中,我有datasource-cfg.properties文件:
# DataSource
ds.database-driver=org.apache.derby.jdbc.ClientDriver
ds.url=jdbc:derby://localhost:1527/sample
ds.username=app
ds.password=app
当我尝试在程序中的某处输出数据时,数据是错误的。 这是我的ApplicationContextConfig:
@Configuration
@ComponentScan("com.icomm.chatapp.*")
@EnableTransactionManagement
@PropertySource(value="classpath:datasource-cfg.properties")
public class ApplicationContextConfig
{
@Autowired
private Environment env1s;
@Bean
public ResourceBundleMessageSource messageSource()
{
ResourceBundleMessageSource rb = new ResourceBundleMessageSource();
rb.setBasenames(new String[]
{
"messages/validator"
});
return rb;
}
@Bean(name = "viewResolver")
public InternalResourceViewResolver getViewResolver()
{
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("WEB-INF/jsp/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Bean(name = "dataSource")
public DataSource getDataSource()
{
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env1s.getProperty("ds.database-driver"));
dataSource.setUrl(env1s.getProperty("ds.url"));
dataSource.setUsername(env1s.getProperty("ds.username"));
dataSource.setPassword(env1s.getProperty("ds.password"));
System.out.println("--------------Property File------------");
System.out.println("" + env1s.getProperty("ds.database-driver"));
System.out.println("" + env1s.getProperty("ds.url"));
System.out.println("" + env1s.getProperty("ds.username"));
System.out.println("" + env1s.getProperty("ds.password"));
System.out.println("--------------Data Source------------");
System.out.println("" + dataSource.getUrl());
System.out.println("" + dataSource.getUsername());
System.out.println("" + dataSource.getPassword());
return dataSource;
}
@Autowired
@Bean(name = "transactionManager")
public DataSourceTransactionManager getTransactionManager(DataSource dataSource)
{
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(dataSource);
return transactionManager;
}
}
结果与datasource-cfg.properties文件中的内容不一样
Info: Loaded JDBC driver: com.mysql.jdbc.Driver
Info: --------------Property File------------
Info: com.mysql.jdbc.Driver
Info: jdbc:mysql://192.168.28.52:3306/retina
Info: dbmail
Info: dbmail
Info: --------------Data Source------------
Info: jdbc:mysql://192.168.28.52:3306/retina
Info: dbmail
Info: dbmail
我注意到这些数据来自另一个项目。在我的SpringFrameworkApp中。有人可以帮我这个。我想访问ChatApp项目中的datasource-cfg.properties。
答案 0 :(得分:1)
根据检查,访问的datasource-cfg-properties确实在Chat App中,但由于某种原因,它位于Other Sources>之下。 src / main / sources / default package / datasource-cfg.properties。
我正在使用Netbeans 8.0,我以为我正在访问源包中的datasource-cfg.properties。很抱歉没有进一步检查并直接发布问题。