创建配置文件 properties
name=刘桑666 |
- 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); | |
} | |
} |
- 测试代码
public void test(){ | |
ApplicationContext c = new AnnotationConfigApplicationContext(SpringConfig.class); | |
BookService service = c.getBean("bookService",BookService.class); | |
service.save(); | |
} |
Run Result