Netbeans .properties:放置文件的位置" classpath:ds / datasource-cfg.properties"

时间:2016-12-25 13:54:57

标签: java spring netbeans

所以我试图在这里运行一个例子Spring MVC and Spring JDBC Transaction Tutorial

根据文章, Eclipse 的位置在这里:

enter image description here

所以我也在 Netbeans

中做到了

enter image description here

但它给了我错误:

    org.springframework.beans.factory.BeanDefinitionStoreException: Failed to load bean class: org.o7planning.springmvcjdbc.config.ApplicationContextConfig; 
nested exception is java.io.FileNotFoundException: class path resource [ds/datasource-cfg.properties] cannot be opened because it does not exist

java.io.FileNotFoundException: class path resource [ds/datasource-cfg.properties] cannot be opened because it does not exist
    org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158)

我一直在阅读关于netbeans中.properties资源访问权限的一些帖子,但似乎没什么用。错误只来自那些(访问部分)。这些代码总体来说还不错。

" 类路径:ds / datasource-cfg.properties "在下面的类中声明。 ApplicationContextConfig.java

package org.o7planning.springmvcjdbc.config;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.*;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@ComponentScan("org.o7planning.springmvcjdbc.*")

@EnableTransactionManagement

// HERE ..!!!
// Load to Environment.
@PropertySources({ @PropertySource("classpath:ds/datasource-cfg.properties") })

public class ApplicationContextConfig {

   // The Environment class serves as the property holder
   // and stores all the properties loaded by the @PropertySource
   @Autowired
   private Environment env;

   @Bean(name = "viewResolver")
   public InternalResourceViewResolver getViewResolver() {
       InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();

       viewResolver.setPrefix("/WEB-INF/pages/");
       viewResolver.setSuffix(".jsp");

       return viewResolver;
   }

   @Bean(name = "dataSource")
   public DataSource getDataSource() {
       DriverManagerDataSource dataSource = new DriverManagerDataSource();

       // See: datasouce-cfg.properties
       dataSource.setDriverClassName(env.getProperty("ds.database-driver"));
       dataSource.setUrl(env.getProperty("ds.url"));
       dataSource.setUsername(env.getProperty("ds.username"));
       dataSource.setPassword(env.getProperty("ds.password"));

       System.out.println("## getDataSource: " + dataSource);

       return dataSource;
   }

   @Bean(name = "transactionManager")
   public DataSourceTransactionManager getTransactionManager() {
       DataSourceTransactionManager txManager = new DataSourceTransactionManager();

       DataSource dataSource = this.getDataSource();
       txManager.setDataSource(dataSource);

       return txManager;
   } 
}

那么将这个resources文件夹放在netbeans中的位置?

1 个答案:

答案 0 :(得分:0)

好的,我找到了:

我评论这个:

//@PropertySources({ @PropertySource("classpath:ds/datasource-cfg.properties") })

然后我添加这个

@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {  
         registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }

在扩展WebMvcConfigurerAdapter

的课程中