spring其他注解

package config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.context.annotation.*;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

/**
* @Author: lijiahao
* @Description: 该类是一个配置类=bean.xml
* @Data: Create in 14:33 2020/2/6
* @Modified By:
*/

/**
* spring的新注解
* @Configuration
* 作用:指定当前是一个配置类
* 细节: 当配置类作为AnnotationConfigApplicationContext对象创建的参数时,该注解可以不写
* @ComponentScan
* 作用:指定spring在创建容器时要扫描的包
* 属性:value:和basePackage作用一样
* 使用此注解=在xml中配置了:
* <context:component-scan base-package="com.itheima"></context:component-scan>
*@Bean
* 作用:用于把当前方法的返回值作为bean对象存入spring的ioc容器中
* 属性:name:用于指定bean的id,不写时,默认值是当前方法的名称
* 细节:当我们使用注解配置方法时,如果方法有参数,spring回去容器中查找有没有可用的bean对象
* 查找的方式和AutoWired注解的作用一样
*@Import
* 作用:用于导入其他的配置类
* 属性:value:用于指定其他配置类的字节码
* 当我们使用Import注解后,有Import注解的类就位父配置类,导入的为子配置类
*@Properties
* 作用:用于指定properties文件的位置
* 属性:
* value:指定关键字的名称和路径
* 关键字:classpath:表示类路径下*/

@Configuration
@ComponentScan("com.itheima")
@Import(JdbcConfig.class)
@PropertySource("classpath:jdbcConfig.properties")
public class SpringConfiguration {
/**
*@Description:用于创建一个QueryRunner对象
*@Date: 14:45 2020/2/6
*@param
*@return
*/
/* @Bean(name="runner")
public QueryRunner createQueryRunner(DataSource dataSource){
return new QueryRunner(dataSource);
}

//创建数据源对象
@Bean(name="dateSource")
public DataSource createDataSource(){
ComboPooledDataSource dataSource = new ComboPooledDataSource();
try {
dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/eesy?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false");
dataSource.setUser("root");
dataSource.setPassword("123456");
return dataSource;
} catch (Exception e) {
throw new RuntimeException(e);
}
}*/
}
posted @ 2020-02-06 18:42  lijiahaoAA  阅读(143)  评论(0编辑  收藏  举报