该类被注解标记,则是一个配置类,作用和bean一样,相当于用java代码代替XML文件配置
@Configuration
指定当前类是一个配置类
当AnnotationConfigApplicationContext对象参数中声明了该类,此注解可以省略,但是如果该类没有配置该注解时,通过@ComponentScan扫描其他包时,如果扫描其他的类中没有配置@Configuration注解则不能生效
@ComponentScan
作用:用于通过注解指定spring在创建容器时要扫描的包
属性:value 和basePackages作用一样,用于指定创建容器时需要扫描的包
等同于XML中的
<context:component-scan
base-package="com.chenhui"></context:component-scan>
@Bean
作用:把当前方法的返回值作为bean对象存入spring的IOC容器中
属性 name表示存入容器中的key,用于指定bean的id,当不写时默认值是当前方法的名称
@Import
作用:用于导入其他的配置类
属性:value用于指定其他配置类的字节码,此时当AnnotationConfigApplicationContext对象传入字节码类时,可以写父配置类
@PropertySource
作用:用于指定properties文件的位置
属性:value:指定文件的名称和路径(可以写classpath:jdbcconfig.properties表示类路径下)
测试:
dao接口类:
public interface AccountDao {
//查询所有
List<Book> findAll();
//查询一条
Book queryOne(Integer id);
//保存数据
Integer saveAccount(Book book);
//更新数据
Integer update(Book book);
//删除数据
Integer delete(Integer id);
}
dao接口实现类:
//使用注解交给Spring容器来管理
@Repository(value = "accountDao")
public class AccountDaoImpl implements AccountDao {
//使用注解从spring容器中自动注入bean对象
@Autowired
//dbutils工具类中用来连接数据库CURD操作的对象
private QueryRunner queryRunner;
//查询所有书本记录
public List<Book> findAll() {
try {
return queryRunner.query("select * from Book", new BeanListHandler<Book>(Book.class));
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
//查询一条书本记录
public Book queryOne(Integer id) {
try {
return queryRunner.query("select * from Book where id=?", new BeanHandler<Book>(Book.class), id);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
//保存一条书本记录
public Integer saveAccount(Book book) {
try {
queryRunner.insert("insert into Book(name,price) values(?,?)", new BeanHandler<Book>(Book.class), book.getName(), book.getPrice());
return 1;
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
//更新一条书本记录
public Integer update(Book book) {
try {
queryRunner.update("update Book set name=?,price=? where id=?", book.getName(), book.getPrice(), book.getId());
return 1;
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
//删除一条书本记录
public Integer delete(Integer id) {
try {
queryRunner.update("delete from Book where id=?", id);
return 1;
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
}
实体类book
public class Book implements Serializable {
private Integer id;
private String name;
private Double price;
public Book() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
'}';
}
}
Service业务接口类
public interface AccountService {
//查询所有
List<Book> findAll();
//查询一条
Book queryOne(Integer id);
//保存数据
Integer saveAccount(Book book);
//更新数据
Integer update(Book book);
//删除数据
Integer delete(Integer id);
}
Service业务接口类实现类:
//使用注解将该bean对象交给Spring来管理
@Service(value = "accountService")
public class AccountServiceImpl implements AccountService{
//使用注解从spring容器中自动注入bean对象(dao层对象)
@Autowired
private AccountDao accountDao;
//调用dao层的查询所有方法
public List<Book> findAll() {
return accountDao.findAll();
}
//调用dao层的查询一条记录方法
public Book queryOne(Integer id) {
return accountDao.queryOne(id);
}
//调用dao层的保存方法
public Integer saveAccount(Book book) {
return accountDao.saveAccount(book);
}
//调用dao层的更新方法
public Integer update(Book book) {
return accountDao.update(book);
}
//调用dao层的删除方法
public Integer delete(Integer id) {
return accountDao.delete(id);
}
}
编写javaSpring核心配置类
//此注解不是必须的,当AnnotationConfigApplicationContext对象参数中声明了该类,此注解可以省略
@Configuration
@ComponentScan(basePackages = "com.chenhui")
//指定properties文件的位置
@PropertySource(value = "jdbcconfig.properties")
public class SpringConfiguration {
//从jdbcconfig.properties使用El表达式取值
@Value("${driver}")
private String driver;
@Value("${url}")
private String url;
@Value("${user}")
private String user;
@Value("${password}")
private String password;
//将方法的返回值交给Spring容器来管理
@Bean(value = "queryRunner")
//由于当前存入的QueryRunner类型是单例的,需要改变作用范围
@Scope("prototype")
public QueryRunner createQueryRunner(DataSource dataSource){
return new QueryRunner(dataSource);
}
//将方法的返回值交给Spring容器来管理
@Bean(name = "dataSource")
public DataSource createDateSource(){
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
try {
comboPooledDataSource.setDriverClass(driver);
comboPooledDataSource.setJdbcUrl(url);
comboPooledDataSource.setUser(user);
comboPooledDataSource.setPassword(password);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
return comboPooledDataSource;
}
}
测试类:
@Test
public void test6(){
//通过AnnotationConfigApplicationContext对象读取注解配置类的字节码参数读取配置文件
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfiguration.class);
//获取spring容器中bean对象
AccountService accountService = applicationContext.getBean("accountService", AccountService.class);
//执行方法
List<Book> all = accountService.findAll();
System.out.println(all);
}
结果: