spring 基础知识补充

spring bean的三种实例化方式
1.通过无参构造
2.静态工程实例化
inface -> Impl -> 创建静态工厂 -> 配置bean class="静态方法类实体类" factry-method="类对象内的静态方法"
实例:
3.工厂实例化
inface -> Impl -> 创建工厂 -> 配置两个bean 第一个bean class="静态方法类实体类" 第二个bean factory-bean="指向工厂bean id " factry-method="指向工厂类的实例化方法"
实例:

spring通过配置文件给对象赋值
1.普通数据类型

2.list对象



相应内容



3.map对象



相应内容
相应内容



spring配置数据源:
数据源作用
1.数据源是提高程序性能出现的 2.事先实例化数据源,初始化部分连接资源
3.使用连接资源时从数据源中获取 使用完毕之后连接资源归还给数据源

创建数据源实例 :c3p0
	 @Test
//测试手动创建c3p0数据源
public  void test() throws Exception {
        //创建数据源
    ComboPooledDataSource dataSource=new ComboPooledDataSource();
        //设置基本的连接参数
    dataSource.setDriverClass("com.mysql.jdbc.Driver");
    dataSource.setJdbcUrl("");
    dataSource.setUser("");
    dataSource.setPassword("");
    //获取资源
    Connection connection=dataSource.getConnection();
    System.out.println(connection);
    connection.close();
}

通过配置文件创建数据源:
	    @Test
//读取配置文件 创建数据源
public  void test1()throws  Exception{
    //读取配置文件
    ResourceBundle rd=ResourceBundle.getBundle("jdbc");  	 			jdbc文件内容:jdbc.driver=  jdbc.url=  jdbc.username= jdbc.password=
    String driver=rd.getString("jdbc.driver");
    String url=rd.getString("jdbc.driver");
    String username=rd.getString("jdbc.driver");
    String password=rd.getString("jdbc.driver");
    //创建数据源  设置连接参数
    ComboPooledDataSource dataSource=new ComboPooledDataSource();
    dataSource.setDriverClass("driver");
    dataSource.setJdbcUrl("url");
    dataSource.setUser("username");
    dataSource.setPassword("password");
    //获取资源
    Connection connection=dataSource.getConnection();
    System.out.println(connection);
    connection.close();
}

spring新注解
@Configyration:表示为spring核心配置文件
@ComponentScan("包名") 功能为:<context:component-scan base-package="ba06"/> 扫描包及其子包内注解
@Bean("名称") spring 会将当前方法返回值以指定名称存储到spring容器中
@Propertyource("classpath:配置文件名")
@Import(文件名.class)

原文地址:https://www.cnblogs.com/cengzhuquan/p/14622908.html