/** * 注入druid数据库连接池对象,返回连接池对象类型,方法名就是xml配置中的id,其余属性在方法中set * @return */ @Bean public DruidDataSource dataSource(){ DruidDataSource dataSource = new DruidDataSource(); dataSource.setName("root"); dataSource.setPassword("747699"); dataSource.setUrl("jdbc:mysql://localhost:3306/db1"); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); return dataSource; }
测试方法:
@Test public void f2(){ DruidDataSource bean = ioc.getBean(DruidDataSource.class); System.out.println(bean); }
输出结果:
{ CreateTime:"2022-05-30 22:19:53", ActiveCount:0, PoolingCount:0, CreateCount:0, DestroyCount:0, CloseCount:0, ConnectCount:0, Connections:[ ] }
没有问题。