# @PropertySource
- SpringConfig 配置类
@SuppressWarnings("all") | |
// 配置当前类为配置类 | |
@Configuration | |
// 扫描路径,扫描配置为 bean 实例的类 | |
@ComponentScan("com.dkx.spring") | |
// 加载配置文件配置 | |
@PropertySource("a.properties") | |
public class SpringConfig { | |
} |
- DaoImpl
@SuppressWarnings("all") | |
@Repository("bookDao1") | |
public class BookDaoImpl implements BookDao { | |
@Value("${name}") | |
private String name; | |
public void save(){ | |
System.out.println("BookDaoImpl save rning..."+","+name); | |
} | |
} |
- ServiceImpl
@SuppressWarnings("all") | |
@Service("bookService") | |
public class BookServiceImpl implements BookService { | |
@Autowired | |
@Qualifier("bookDao1") | |
private BookDao bookDao; | |
public void save(){ | |
System.out.println("BookServiceImpl save rning..."); | |
bookDao.save(); | |
} | |
} |
- 测试代码
public void test(){ | |
ApplicationContext c = new AnnotationConfigApplicationContext(SpringConfig.class); | |
BookService service = c.getBean("bookService",BookService.class); | |
service.save(); | |
} |
Run Result

# 使用 claspath * : * (Error)
可使用 classpath: 而不可使用 classpath*:*
- SpringConfig
@SuppressWarnings("all") | |
// 配置当前类为配置类 | |
@Configuration | |
// 扫描路径,扫描配置为 bean 实例的类 | |
@ComponentScan("com.dkx.spring") | |
// 加载配置文件配置 | |
@PropertySource("classpath*:*properties") | |
public class SpringConfig { | |
} |
Run Result
