【Java闭关修炼】Spring-Spring最新注解开发
原始注解不能替代的配置
Spring新注解
配置组件扫描注解
package com.itheima.config;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.itheima")
public class SpringConfiguration {
}
配置druid资源注解
package com.itheima.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import javax.sql.DataSource;
@Configuration
@ComponentScan("com.itheima")
@PropertySource("classpath:jdbc.properties")
public class SpringConfig {
@Value(("${jdbc.driver"))
private String driver;
@Value(("${jdbc.url"))
private String url;
@Value(("${jdbc.username"))
private String username;
@Value(("${jdbc.password"))
private String password;
@Bean("dataSource")
public DataSource getDataSource(){
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
dataSource.setDriverClass(driver);
dataSource.setJdbcUrl(url);
dataSource.setUser(username);
dataSource.setPassword(password);
return dataSource;
}
}