2016-06-20 19 views
0

Springプロジェクトでクラスパスからプロパティを読み込もうとしたときにエラーが表示される@PropertySource注釈。私は以下のコードを貼り付けました。私が逃しているものを教えてください。ありがとう。

WARNING:例外は、コンテキストの初期化中に発生した - キャンセルリフレッシュしよう: org.springframework.beans.factory.BeanCreationException:エラー 'customerRepository' 名前を持つBeanの作成 :autowired 依存性の注入を失敗しました。ネストされた例外は java.lang.IllegalArgumentExceptionがある:文字列値「$ {名前}」のスレッドで

例外でプレースホルダ 「名前」を解決できませんでした「メイン」 org.springframework.beans.factory.BeanCreationException:エラー 「customerRepository」という名前のBeanを作成しています:autowired 依存の注入が失敗しました。入れ子の例外 java.lang.IllegalArgumentExceptionがある:文字列値にプレースホルダ '名前' を解決できませんでした "$ {名前}"

AppConfig.java

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.PropertySource; 
import org.springframework.context.annotation.Scope; 

import com.vivek.service.CustomerService; 
import com.vivek.service.CustomerServiceImpl; 

@Configuration 
@ComponentScan("com.vivek") 
@PropertySource("app.properties") 
public class AppConfig { 

    @Bean 
    public static PropertyPlaceholderConfigurer getPropertyPlaceholderConfigurer(){ 
     PropertyPlaceholderConfigurer p = new PropertyPlaceholderConfigurer(); 
     //p.setIgnoreUnresolvablePlaceholders(true); 
     return p; 
    } 

    @Bean(name="customerService") 
    @Scope("prototype") 
    public CustomerService getCustomerService(){ 
     CustomerServiceImpl service = new CustomerServiceImpl(); 
     //service.setRepository(getCustomerRepository()); 
     return service; 
    } 

/* @Bean(name="customerRepository") 
    public CustomerRepository getCustomerRepository(){ 
     CustomerRepository repository = new HibernateCustomerRepository(); 
     return repository; 
    }*/ 
} 

Application.java

import org.springframework.context.ApplicationContext; 
import org.springframework.context.annotation.AnnotationConfigApplicationContext; 

import com.vivek.service.CustomerService; 
import com.vivek.service.CustomerServiceImpl; 

public class Application { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); 
     CustomerService service = context.getBean("customerService",CustomerService.class); 
     System.out.println(service); 
     CustomerService service1 = context.getBean("customerService",CustomerService.class); 
     System.out.println(service1); 

     System.out.println(service.findAll().get(0).getFirstName()); 
     System.out.println(service.findAll().get(0).getLastName()); 
    } 

} 

HibernateCustomerRepo sitory.java

package com.vivek.repository; 

import java.util.ArrayList; 
import java.util.List; 

import org.springframework.beans.factory.annotation.Value; 
import org.springframework.stereotype.Repository; 

import com.vivek.model.Customer; 

@Repository("customerRepository") 
public class HibernateCustomerRepository implements CustomerRepository { 

    /* (non-Javadoc) 
    * @see com.vivek.repository.CustomerRepository#findAll() 
    */ 
    @Value("${name}") 
    private String name; 

    @Override 
    public List<Customer> findAll(){ 
     List<Customer> customerList = new ArrayList<Customer>(); 

     Customer customer = new Customer(); 
     customer.setFirstName(name); 
     customer.setLastName("Shah"); 

     customerList.add(customer); 

     return customerList; 
    } 
} 

CustomerServiceImpl.java

package com.vivek.service; 

import java.util.List; 

import org.springframework.beans.factory.annotation.Autowired; 

import com.vivek.model.Customer; 
import com.vivek.repository.CustomerRepository; 
import com.vivek.repository.HibernateCustomerRepository; 

public class CustomerServiceImpl implements CustomerService { 

    /* (non-Javadoc) 
    * @see com.vivek.service.CustomerService#findAll() 
    * 
    */ 

    private CustomerRepository repository; 

    @Autowired 
    public void setRepository(CustomerRepository repository) { 
     this.repository = repository; 
    } 

    @Override 
    public List<Customer> findAll(){ 
     CustomerRepository repository = new HibernateCustomerRepository(); 
     return repository.findAll(); 
    } 



} 

app.properties

name=Arnold 
+1

アプリの設定でPropertySourcesPlaceholderConfigurerを試すことができますか? – Veeram

+0

ありがとうShiv V !!私はそれを試して、エラーがなくなった。ただし、名前はnullとして出力されます。私が間違っていることが他に何かありますか? –

+0

問題ありません。プロパティファイルがclasspathにあり、springがファイルにアクセスできることを確認してください。場合によっては、filenotfoundexceptionのログをチェックしてください。 – Veeram

答えて

0

あなたは春の設定ファイルにPropertySourcesPlaceholderConfigurerを宣言する必要があるかもしれませ。

<bean 
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="location"> 
     <value>config.properties</value> 
    </property> 
</bean> 
+0

私はxml configの代わりにJava configを使いたいと思っていました。 –

関連する問題