spring框架bean.xml文件配置

Bean.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"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 </beans>

bean配置中的理解

## 关于 id 的介绍
	**在基本配置中 id 首字母要小写,id 在xml中进行对象的传递,在代码中 要使用相应的
	对象时,需要在加载完xml 文件后,通过id 来获取指定的对象**
	
## 关于 class 的介绍
	**class 是通过类的全路径来获取类的类对象 并在 id 中进行起名**
	
## 关于 property 介绍
	**property 是倒入 该类 需要使用的依赖,比如其他类对象,在使用set 注入时 才使用 property 进行配置
	ref=“需要引入的对象id名”**
	
	## set注入代码
	*private IAccountDao accountDao;
	 public void setAccountDao(IAccountDao accountDao) {
    this.accountDao = accountDao;}*
    
## 关于constructor-arg 标签介绍
	**constructor-arg 是构造器注入 因为他没有set 注入,只是 有最后一个注入对象在使用它
	所以使用的是 构造器 注入**
	
## 在 <bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
	中scope 是进行多例 配置 防止在执行操作时发生 数据冲突【安全】

基本配置

 <!-- id值第一个字母使用小写 -->
<bean id="accountServiceImp" class="cn.service.serviceImp.AccountServiceImp">
    <property name="accountDao" ref="accountDao"/>
</bean>

<bean id="accountDao" class="cn.dao.daoImp.AccountDaoImp">
    <!-- 使用 property 都是使用的是set 注入-->
    <property name="runner" ref="runner"/>
</bean>
<!--配置QueryRunner-->
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
    <!-- 注入数据源  因为没有使用set 注入 故而使用的是构造器 进行数据的注入 -->
    <constructor-arg name="ds" ref="dataSource"/>
</bean>
<!--配置数据源-->
<!--  下面的属性配置 就相当于 jdbc 的配置文件 -->
<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/mybatis"/>
    <property name="user" value="root"/>
    <property name="password" value="root"/>
</bean>

关于set注入的使用【在代码中如何使用】

## 如何在代码中使用
public class AccountServiceImp implements IAccountService {
private IAccountDao accountDao;
public void setAccountDao(IAccountDao accountDao) {
    this.accountDao = accountDao;
}}

在测试中加载bean.xml配置文件

## 读取bean.xml配置文件【并用器对象获取 要使用的 对象】

private ApplicationContext ac = new ClassPathXmlApplicationContext(“bean.xml”);

获取测试中要使用的对象

private IAccountService ias= ac.getBean(“accountServiceImp”, IAccountService.class);
private IAccountDao iad = ac.getBean(“accountDao”,IAccountDao.class);

在pom.xml导入的jar包

 <dependencies>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.4.5</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.12</version>
    </dependency>
    <dependency>
        <groupId>c3p0</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.1.2</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>commons-dbutils</groupId>
        <artifactId>commons-dbutils</artifactId>
        <version>1.7</version>
    </dependency>
</dependencies>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值