1,代码演示
1.1 新建 OtherBean
package com.study.beans;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;
@Component
public class OtherBean {
@Bean("dataSource")
public DataSource dataSource(){
DruidDataSource druidDataSource = new DruidDataSource();
//设置基本参数 ...
return druidDataSource;
}
}
1.2 修改测试类 ApplicationContextDemo01
package com.study.demo;
import com.study.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ApplicationContextDemo01 {
public static void main(String[] args) {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
Object dataSource = context.getBean("dataSource");
System.out.println(dataSource);
}
}