spring注解开发
spring注解开发
applicationContext的继承体系
ApplicationContext的实现类
1)ClassPathXmlApplicationContext
它是从类的根路径下加载配置文件推荐使用这种
2)FileSystemXmlApplicationContext
它是从磁盘路径上加载配置文件,配置文件可以在磁盘的任意位置。
3)AnnotationConfigApplicationContext
当使用注解配置容器对象时,需要使用此类来创建spring 容器。它用来读取注解。
getBean的使用方法
public Object getBean(String name) throws BeansException {assertBeanFactoryActive();return getBeanFactory().getBean(name);}
public <T> T getBean(Class<T> requiredType) throws BeansException {assertBeanFactoryActive();return getBeanFactory().getBean(requiredType);}
其中,当参数的数据类型是字符串时,表示根据Bean的id从容器中获得Bean实例,返回是Object,需要强转。
当参数的数据类型是Class类型时,表示根据类型从容器中匹配Bean实例,当容器中相同类型的Bean有多个时,则此方法会报错。
Spring配置数据源
数据源的开发步骤
①导入数据源的坐标和数据库驱动坐标
②创建数据源对象
③设置数据源的基本连接数据
④使用数据源获取连接资源和归还连接资源
以前我们创建数据源都是使用手动创建的,现在使用的是spring自动帮我们进行创建
手动创建数据源
spring配置数据源的方式
- 可以将DataSource的创建权交由Spring容器去完成
- DataSource有无参构造方法,而Spring默认就是通过无参构造方法实例化对象的
- DataSource要想使用需要通过set方法设置数据库连接信息,而Spring可以通过set方法进行字符串注入
- 利用注入的方式进行spring配置数据源
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="com.mysql.jdbc.Driver"/><property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"/><property name="user" value="root"/>
<property name="password" value="root"/></bean>
抽取jdbc配置数据源
从applicationContext中加载jdbc.properties配置文件获取连接信息
引入context命名空间和约束文件
- 命名空间:xmlns:context="http://www.springframework.org/schema/context"
- 约束路径:http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd
之后使用正则表达式获取相关连接信息
<context:property-placeholder location="classpath:jdbc.properties"/><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driver}"/><property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></bean>
编写相关代码
package cn.rlfit.test;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ResourceBundle;
public class TestDurid {
//测试手动获取数据源连接
@Test
public void test1() throws SQLException {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/test");
dataSource.setUsername("root");
dataSource.setPassword("681290");
//得到连接对象
Connection connection = dataSource.getConnection();
System.out.println(connection);
}
//读取配置获取连接
@Test
public void test2() throws SQLException {
ResourceBundle jdbc = ResourceBundle.getBundle("jdbc");
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(jdbc.getString("jdbc.driver"));
dataSource.setUrl(jdbc.getString("jdbc.url"));
dataSource.setUsername(jdbc.getString("jdbc.username"));
dataSource.setPassword(jdbc.getString("jdbc.password"));
//得到连接对象
Connection connection = dataSource.getConnection();
System.out.println(connection);
}
//通过配置文件进行获取数据源
@Test
public void test3() throws SQLException {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContest.xml");
DruidDataSource dataSource = (DruidDataSource) app.getBean("dataSource");
DruidPooledConnection connection = dataSource.getConnection();
System.out.println(connection);
}
}
配置文件的配置
<?xml version="1.0" encoding="UTF-8"?>
<!-- 引入context命名空间和约束路径-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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="dataSource" class="com.alibaba.druid.pool.DruidDataSource">-->
<!-- <property name="driverClassName" value="com.mysql.jdbc.Driver"/>-->
<!-- <property name="url" value="jdbc:mysql://localhost:3306/test"/>-->
<!-- <property name="username" value="root" />-->
<!-- <property name="password" value="681290"/>-->
<!--</bean>-->
<!-- 将application.xml和jdbc.properties文件分开-->
<context:property-placeholder location="jdbc.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}"/>
</bean>
</beans>
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=681290
spring注解开发
Spring是轻代码而重配置的框架,配置比较繁重,影响开发效率,所以注解开发是一种趋势,注解代替xml配置文件可以简化配置,提高开发效率。
原始注解
注意
使用注解进行开发时,需要在applicationContext.xml中配置组件扫描,作用是指定哪个包及其子包下的Bean需要进行扫描以便识别使用注解配置的类、字段和方法。
spring新注解
- 非自定义的Bean的配置:
<bean>
- 加载properties文件的配置:
<context:property-placeholder>
- 组件扫描的配置:
<context:component-scan>
- 引入其他文件:
<import>
注意新注解都是配置在一个配置类当中的,实际上就是将配置文件转换为配置类
spring集成Junit
在原始的测试类当中每一个测试方法都会包含下面两行代码
ApplicationContext ac = newClassPathXmlApplicationContext("bean.xml");
IAccountService as = ac.getBean("accountService",IAccountService.class);
这样就会造成大量的重复性代码
解决思路
- 让SpringJunit负责创建Spring容器,但是需要将配置文件的名称告诉它
- 将需要进行测试Bean直接在测试类中进行注入
spring集成junit步骤
①导入spring集成Junit的坐标
②使用@Runwith注解替换原来的运行期
③使用@ContextConfiguration指定配置文件或配置类
④使用@Autowired注入需要测试的对象
⑤创建测试方法进行测试
代码实现
- 本文标签: Spring Java
- 本文链接: https://www.rlfit.cn/article/31
- 版权声明: 本文由若离风原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权