3、Spring创建第三方bean对象
在导入如上druid、mysql的依赖后,在ico.xml配置文件中进行如下配置:
<!--spring管理第三方bean-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
<property name="url" value="jbdc:mysql://localhost:3306/demo"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
</bean>
然后,测试:
public class MyTest {
public static void main(String[] args) throws SQLException {
ApplicationContext context = new ClassPathXmlApplicationContext("ioc.xml");
DruidDataSource dataSource = context.getBean("dataSource", DruidDataSource.class);
System.out.println(dataSource);
//System.out.println(dataSource.getConnection());
}
}
结果:
如果一些信息不想像上面的ioc.xml配置文件中那样写死了,可以引入外部的配置文件。
4、spring引用外部配置文件
代码示例:
pom.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zhoulz</groupId>
<artifactId>spring_study_maven_3</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--添加的maven依赖-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<!--添加的druid依赖-->
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.14</version>
</dependency>
<!--添加的mysql依赖-->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
</dependencies>
</project>
ioc.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
">
<bean id="person" class="com.zhoulz.bean.Person" > <!--单例-->
<property name="id" value="1"></property>
<property name="name" value="lisi"></property>
<property name="age" value="20"></property>
<property name="gender" value="男"></property>
</bean>
<!--spring管理第三方bean-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
<property name="url" value="jbdc:mysql://localhost:3306/demo"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
</bean>
<!--当需要引入外部的配置文件的时候,需要导入一些context的命名空间-->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!--
在配置文件编写属性的时候,需要注意:
spring容器在进行启动的时候,会读取当前系统的某些环境变量的配置,
当前系统的用户名是用username来表示的,所以最好的方式是添加前缀来作区分,如,下面的username不能再加jbdc.了
-->
<bean id="dataSource2" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="${username}"></property>
<property name="password" value="${jbdc.password}"></property>
<property name="url" value="${url}"></property>
<property name="driverClassName" value="${driverClassName}"></property>
</bean>
<bean id="person2" class="com.zhoulz.bean.Person">
<property name="name" value="${jbdc.username}"></property>
</bean>
</beans>
在resource文件夹下新创建db.properties配置文件(文件类型为File):
jbdc.username=root123
jbdc.password=123456
url=jbdc:mysql://localhost:3306/demo
driverClassName=com.mysql.jdbc.Driver
测试类:
import com.alibaba.druid.pool.DruidDataSource;
import com.zhoulz.bean.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.sql.SQLException;
public class MyTest {
public static void main(String[] args) throws SQLException {
ApplicationContext context = new ClassPathXmlApplicationContext("ioc.xml");
/* DruidDataSource dataSource = context.getBean("dataSource", DruidDataSource.class);
System.out.println(dataSource);
//System.out.println(dataSource.getConnection());*/
/*DruidDataSource dataSource2 = context.getBean("dataSource2", DruidDataSource.class);
System.out.println(dataSource2);
//System.out.println(dataSource.getConnection());*/
Person person2 = context.getBean("person2", Person.class);
System.out.println(person2);
}
}