被管理的对象
@Service("bookService")
@Scope("prototype")
public class BookServiceImpl implements BookService{
@Autowired
private BookDao bookDao1;
@Value("mysql") // 不用提供set方法
private String databaseName;
@Value("10")
private int connectionNum;
@Value("${username}")
private String username;
}
配置类(相当于之前的xml文件)
@Configuration // 定义为 配置类
@ComponentScan({"dao","service"}) // bean的扫描路径, 这里是包名
@PropertySource("classpath:druid.properties") // 引入 properties 文件
@Import({DataSourceConfig.class}) // 引入管理第三方 bean的类
public class SpringConfig {
}
测试
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
BookService b = (BookService) ctx.getBean("bookService");