1、继承关系配置
类之间的继承关系,可以通过使用【parent】来进行设置。在子类的bean配置中,设置parent指向父类的bean的id即可。如果父类是抽象类或者不想让父类的bean被获取到,则可以在父类对应的bean中使用【abstract=true】,来标识该bean只能被继承代码中不能被获取。代码示例:
package entity; public class Person { private Double height; private Double weight; public Person() { System.out.println("Person无参构造"); } public Double getHeight() { return height; } public void setHeight(Double height) { this.height = height; } public Double getWeight() { return weight; } public void setWeight(Double weight) { this.weight = weight; } @Override public String toString() { return "Person{" + "height=" + height + ", weight=" + weight + '}'; } }
package entity; /** * @Author: LiZhongyuan * @Description: * @createTime: 2019/09/21 12:45 * @version: */ public class Teacher extends Person { private Integer t_id; private String t_name; private Integer t_age; public Teacher(Integer t_id, String t_name, Integer t_age) { System.out.println("teacher有参构造"); this.t_id = t_id; this.t_name = t_name; this.t_age = t_age; } public Teacher(){ System.out.println("Teacher无参构造"); } public Integer getT_id() { return t_id; } public void setT_id(Integer t_id) { this.t_id = t_id; } public String getT_name() { return t_name; } public void setT_name(String t_name) { this.t_name = t_name; } public Integer getT_age() { return t_age; } public void setT_age(Integer t_age) { this.t_age = t_age; } @Override public String toString() { return "Teacher{" + super.toString() + "t_id=" + t_id + ", t_name='" + t_name + '\'' + ", t_age=" + t_age + '}'; } }
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:util="http://www.springframework.org/schema/util" 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/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="person1" class="entity.Person" abstract="true"> <property name="height" value="178.5"></property> <property name="weight" value="65.5"></property> </bean> <bean id="person2" class="entity.Person"> <property name="height" value="170.5"></property> <property name="weight" value="48.5"></property> </bean> <bean id="teacher1" class="entity.Teacher" parent="person1"> <constructor-arg name="t_id" value="1"></constructor-arg> <constructor-arg name="t_name" value="MrLi"></constructor-arg> <constructor-arg name="t_age" value="27"></constructor-arg> </bean> <bean id="teacher2" class="entity.Teacher" parent="person2"> <constructor-arg name="t_id" value="2"></constructor-arg> <constructor-arg name="t_name" value="MissWang"></constructor-arg> <constructor-arg name="t_age" value="27"></constructor-arg> </bean> </beans>
2、bean的作用域
作用域:
1、singleton:bean的作用域的默认配置,在spring容器加载时,就创建单例的bean,每次获取对象时都是获取的同一个bean;
2、prototype:spring容器加载时并不会去创建bean,而是在每次获取对象时都创建一个新的bean
3、request:每次http请求过来时,创建一个新的bean,同一请求中多次获取bean,得到的是相同的bean,不同请求中获取bean时,得到的bean不同。该作用域仅适用于web的Spring WebApplicationContext环境;
4、session:同一个http session会话中共享同一个bean,新的会话将创建新的bean。该作用域仅适用于web的Spring WebApplicationContext环境;
5、application:限定一个Bean的作用域为ServletContext的生命周期。该作用域仅适用于web的Spring WebApplicationContext环境。
配置文件中通过在bean节点上设置scope来管理bean的作用域,如果使用注解配置bean,可以配合使用@Scope来管理作用域,如:@Scope("singleton")、@Scope("prototype")
3、延迟加载
如果, bean是单例的时,在加载spring容器时,bean就会被创建。可以通过配置延迟加载,使bean在被调用的时候再创建。
配置文件配置bean时:通过在bean节点上使用【lazy-init】可以设置是否启用延迟加载。lazy-init="default"与lazy-init="false"等同,也就是说,默认情况下,延迟加载不启用。当设置lazy-init="true"时,启用延迟加载,这时在spring容器加载时不会去创建bean,而是在获取bean时再创建。
注解配置bean时:通过配合使用@Lazy注解来达到相同目的,默认情况下,加了此注解就启用延迟加载,如果@Lazy(value=false),则不启用
4、数据库连接
以连接mysql数据库为例:
首先在pom文件中增加数据库连接包:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
- 方式一:使用Spring自带的DriverManagerDataSource进行连接
在spring的xml配置文件中增加数据源配置:
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://127.0.0.1:3306/test"></property> <property name="username" value="root"></property> <property name="password" value="123"></property> </bean>
测试结果:
package controller; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jdbc.datasource.DriverManagerDataSource; import java.sql.Connection; public class Main { public static void main(String[] args){ // 读取配置文件 ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-bean.xml"); // 根据id获取数据源的bean DriverManagerDataSource driverManagerDataSource = (DriverManagerDataSource)ctx.getBean("datasource"); System.out.println(driverManagerDataSource); try { // 获取连接 Connection conn = driverManagerDataSource.getConnection(); System.out.println(conn); System.out.println("连接成功!!"); } catch (Exception e){ e.printStackTrace(); System.out.println("连接失败!!"); } } }
打印结果为:
说明spring连接成功
但是DriverManagerDataSource只要数据库连接就新创建一个connection,所以在访问量大,并发的情况下表现很差,这时可以使用配置连接池进行连接。
- 方式二:使用连接池连接数据库
连接池的种类有很多,比如:C3p0、dbcp、Proxool、BoneCP等,使用方法大同小异,这里以C3p0为例:
首先,引入C3p0相关的jar包:
<dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency>
然后,在xml文件进行配置,配置方式同使用DriverManagerDataSource配置类似,只不过不同的数据源参数名称可能会有一些差异:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/test"></property> <property name="user" value="root"></property> <property name="password" value="123"></property> </bean>
测试代码:
package controller; import com.mchange.v2.c3p0.ComboPooledDataSource; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class Main { public static void main(String[] args){ // 读取配置文件 ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-bean.xml"); ComboPooledDataSource comboPooledDataSource = (ComboPooledDataSource)ctx.getBean("dataSource"); System.out.println(comboPooledDataSource); Connection conn = null; try { conn = DriverManager.getConnection(comboPooledDataSource.getJdbcUrl(), comboPooledDataSource.getUser(), comboPooledDataSource.getPassword()); String sql = "select * from number_two"; PreparedStatement preparedStatement = conn.prepareStatement(sql); ResultSet resultSet = preparedStatement.executeQuery(); while (resultSet.next()){ System.out.print(resultSet.getInt(1)); System.out.println(resultSet.getString(2)); } System.out.println(conn); System.out.println("连接成功!!"); } catch (Exception e){ e.printStackTrace(); System.out.println("连接失败!!"); } finally { if(conn!=null){ try { conn.close(); } catch (Exception e){ e.printStackTrace(); } } } } }
5、引入外部文件
如:
引入同目录下的数据库连接信息文件:
<context:property-placeholder location="jdbc.properties"></context:property-placeholder>
jdbc.properties文件内容:
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/test jdbc.username=root jdbc.password=123
所以,在配置数据源时,可以写作:
<context:property-placeholder location="jdbc.properties"></context:property-placeholder> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClassName}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean>
6、jdbcTemplate的使用
- 首先,配置数据源连接,可以使用DriverManagerDataSource或数据连接池
- 配置jdbcTemplate的bean
- 获取jdbcTemplate的bean,执行数据库操作
代码示例:
<?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:util="http://www.springframework.org/schema/util" 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/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--引入外部文件--> <context:property-placeholder location="jdbc.properties"></context:property-placeholder> <!--配置数据库连接(使用C3p0)--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClassName}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!--配置jdbcTemplate--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> </beans>
测试类:
package controller; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jdbc.core.JdbcTemplate; import java.util.Map; public class Main { public static void main(String[] args){ // 读取配置文件 ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-bean.xml"); JdbcTemplate jdbcTemplate = (JdbcTemplate)ctx.getBean("jdbcTemplate"); Map<String, Object> map = jdbcTemplate.queryForMap("select * from number_two WHERE id = 1"); System.out.println(map); } }
具体jdbcTemplat的使用,再额外记录