applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 加载外部的properties文件 -->
<!-- <context:property-placeholder location="classpath:Jdbc.properties"></context:property-placeholder>-->
<!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">-->
<!-- <property name="driverClass" value="${jdbc.driver}"></property>-->
<!-- <property name="jdbcUrl" value="${jdbc.url}"></property>-->
<!-- <property name="user" value="${jdbc.username}"></property>-->
<!-- <property name="password" value="${jdbc.password}"></property>-->
<!-- </bean>-->
<!-- 想用注解 得配置组件扫描,告诉spring 在哪个包下面的Bean需要Spring进行扫描,扫描到注解从而创建对象-->
<!-- <context:component-scan base-package="com.itheima"/>-->
<!-- <import resource=""/>-->
</beans>
# Jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/book
jdbc.username=root
jdbc.password=123456
@Configuration
@ComponentScan("com.itheima")
@Import(DataSourceConfiguration.class)
public class SpringConfiguration {
}
@PropertySource("classpath:Jdbc.properties")
public class DataSourceConfiguration {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean("dataSource")
public DataSource getDataSource() throws PropertyVetoException {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setDriverClass(driver);
comboPooledDataSource.setJdbcUrl(url);
comboPooledDataSource.setUser(username);
comboPooledDataSource.setPassword(password);
return comboPooledDataSource;
}
}
public interface UserDao {
public void save();
}
@Repository("userDao")
public class UserDaoImpl implements UserDao {
public void save() {
System.out.println("存你奶奶个der......");
}
}
public interface UserService {
public void save();
}
@Service("userService")
@Scope("singleton")
public class UserServiceImpl implements UserService {
@Value("${jdbc.driver}")
private String driver;
@Resource(name = "userDao")
private UserDao userDao;
public void save() {
userDao.save();
System.out.println(driver);
}
@PostConstruct
public void init(){
System.out.println("Service 对象的初始化方法");
}
@PreDestroy
public void destory(){
System.out.println("Service 对象的销毁方法");
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SpringConfiguration.class})
public class SpringJunitTest {
@Autowired
private DataSource dataSource;
@Autowired
private UserService userService;
@Test
public void test1() throws SQLException {
userService.save();
System.out.println(dataSource.getConnection());
}
}
public class DataSourceTest {
@Test
public void test4() throws SQLException {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource bean = context.getBean(DataSource.class);
Connection connection = bean.getConnection();
System.out.println(connection);
connection.close();
}
@Test
public void test3() throws Exception {
ResourceBundle resourceBundle=ResourceBundle.getBundle("Jdbc");
String driver=resourceBundle.getString("jdbc.driver");
String url=resourceBundle.getString("jdbc.url");
String username=resourceBundle.getString("jdbc.username");
String password=resourceBundle.getString("jdbc.password");
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();
}
@Test
public void test2() throws PropertyVetoException, SQLException {
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
druidDataSource.setUrl("jdbc:mysql://localhost:3306/book");
druidDataSource.setUsername("root");
druidDataSource.setPassword("123456");
Connection connection = druidDataSource.getConnection();
System.out.println(connection);
}
@Test
public void test1() throws PropertyVetoException, SQLException {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");
comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/book");
comboPooledDataSource.setUser("root");
comboPooledDataSource.setPassword("123456");
Connection connection = comboPooledDataSource.getConnection();
System.out.println(connection);
}
}