복붙노트

[SPRING] Autowired 환경이 null입니다.

SPRING

Autowired 환경이 null입니다.

Spring 프로젝트에 환경을 연결하는 데 문제가 있습니다. 이 수업에서

@Configuration
@ComponentScan(basePackages = "my.pack.offer.*")
@PropertySource("classpath:OfferService.properties")
public class PropertiesUtil {
    @Autowired
    private Environment environment;



    @Bean
    public String load(String propertyName)
    {
        return environment.getRequiredProperty(propertyName);
    }
}

환경은 항상 null입니다.

해결법

  1. ==============================

    1.autowiring은 load ()가 호출 된 이후에 발생합니다 (어떤 이유로).

    autowiring은 load ()가 호출 된 이후에 발생합니다 (어떤 이유로).

    해결 방법은 EnvironmentAware를 구현하고 Spring 호출 setEnvironment () 메소드를 사용하는 것입니다.

    @Configuration
    @ComponentScan(basePackages = "my.pack.offer.*")
    @PropertySource("classpath:OfferService.properties")
    public class PropertiesUtil implements EnvironmentAware {
        private Environment environment;
    
        @Override
        public void setEnvironment(final Environment environment) {
            this.environment = environment;
        }
    
        @Bean
        public String load(String propertyName)
        {
            return environment.getRequiredProperty(propertyName);
        }
    }
    
  2. ==============================

    2.@Resource (javax.annotation)에서 @Autowired를 변경하고 public으로 설정합니다 (예 :

    @Resource (javax.annotation)에서 @Autowired를 변경하고 public으로 설정합니다 (예 :

    @Configuration
    @PropertySource("classpath:database.properties")
    public class HibernateConfigurer {
    
        @Resource
        public Environment env;
    
        @Bean
        public DataSource dataSource() {
            BasicDataSource dataSource = new BasicDataSource();
            dataSource.setDriverClassName(env.getProperty("database.driverClassName"));
            dataSource.setUrl(env.getProperty("database.url"));
            dataSource.setUsername(env.getProperty("database.username"));
            dataSource.setPassword(env.getProperty("database.password"));
            dataSource.setValidationQuery(env.getProperty("database.validationQuery"));
    
            return dataSource;
        }
    }
    

    이 경우 WebApplicationInitializer에 구성 자 클래스를 등록해야합니다.

    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.register(ApplicationConfigurer.class); //ApplicationConfigurer imports HibernateConfigurer
    

    그것은 나를 위해 일하고있어! 내가 만든 테스트 프로젝트를 확인하고 싶을 수도 있습니다.

  3. ==============================

    3.환경을 autowire하려고하는 클래스 안에이 코드를 넣으십시오.

    환경을 autowire하려고하는 클래스 안에이 코드를 넣으십시오.

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyPlaceHolderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
    

    그것은 내 문제를 해결했다. 나는 너에게 나의 수업을 준다.

    @Configuration
    @EnableTransactionManagement
    public class DatabaseConfig {   
    /**
     * DataSource definition for database connection. Settings are read from the
     * application.properties file (using the env object).
     */
    @Bean
    public DataSource dataSource() {
    
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(env.getProperty("db.driver"));
        dataSource.setUrl(env.getProperty("db.url"));
        dataSource.setUsername(env.getProperty("db.username"));
        dataSource.setPassword(env.getProperty("db.password"));
        return dataSource;
    }
    
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyPlaceHolderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
    
      @Autowired
      private Environment env;
    
    }
    
  4. from https://stackoverflow.com/questions/19421092/autowired-environment-is-null by cc-by-sa and MIT license