当我尝试使用@PropertySource注释在我的Spring项目中从类路径中读取属性时,我看到下面的错误。我在下面粘贴了我的代码,请您帮忙告诉我我错过了什么?感谢。
警告:上下文初始化期间遇到异常 - 取消刷新尝试: org.springframework.beans.factory.BeanCreationException:错误 创建名为' customerRepository'的bean:注入自动装配 依赖失败;嵌套异常是 java.lang.IllegalArgumentException:无法解析占位符 '名称'在字符串值" $ {name}"
线程中的异常" main" org.springframework.beans.factory.BeanCreationException:错误 创建名为' customerRepository'的bean:注入自动装配 依赖失败;嵌套异常是 java.lang.IllegalArgumentException:无法解析占位符 '名称'在字符串值" $ {name}"
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());
}
}
HibernateCustomerRepository.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
答案 0 :(得分:0)
可能需要在spring配置文件中声明PropertySourcesPlaceholderConfigurer。
chapter