Spring中的部分注解(只是Spring中)及其要求的xml配置

相较于在Java代码中添加注解,在*.xml中进行配置就变的繁琐无比,但是不敢保证所有的项目都是使用注解的形式所以两者都要会。

 @autowired注解(自动注入)

          当某个属性有了该注解时,在IOC容器创建时,就会自动的创建对相应类型的bean实例(默认单例的)

PS:但是会出现一些问题。当属性有多个实现类时容器不知道到底要创建那个一个对象。

这时需要跟 @Qualifiter(value="bean的id")配合使用(value的值是对应实现类的bean的id。如果是注解形式的事项类要是没有特别的声明,那么value的值就是全类名(第一个字母小写))

package com.thekingqj.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.thekingqj.dao.UserDao;
import com.thekingqj.service.UserService;

@Service
public class UserServiceImpl implements UserService{

	  @Autowired
	  @Qualifier(value="userDaoImpl2")
	  private UserDao us;
	  
	@Override
	public void add() {
		System.out.println("调用service方法");
		us.add();
	}

}

 

@Component(bean)@Repository (dao) @Service (service)@Controller("")(servlet)

这几个其实本质的意思就是组件,只不过针对于不同的层次不同的命名规则,最要按照上面的来添加注解(项目中必须这样来)

 

重点:

   你添加了注解,但是IOC容器在创建的时候怎么识别你呢,不识别就没法创建了啊。

  

<context:component-scan base-package="com.thekingqj" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/>
    
    </context:component-scan>

所以要添加这个在applicationContext.xml里面

use-default-filters="false"这个默认是true的   base-package="com.thekingqj"是基础的包(true的情况下扫描基础包下的所有类)

 

@Transactional事务

     Spring针对于不同的事务有着不同个的实现

  1. DataSourceTransactionManager:在应用程序中只需要处理一个数据源,而且通过JDBC存取。
  2. JtaTransactionManager:在JavaEE应用服务器上用JTA(Java Transaction API)进行事务管理
  3. HibernateTransactionManager:用Hibernate框架存取数据库

 要想实现事务需要配置配置事务管理器,由于这里使用的是注解所以还要启用事务注解。配置代码如下:

<?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"
	xmlns:tx="http://www.springframework.org/schema/tx"
	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-4.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

<!-- 引入外部的属性文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 扫描 -->
 <context:component-scan base-package="com.atguigu.spring.jdbc"></context:component-scan>
<!-- 管理数据源dataSource -->
   <bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource">
      <property name="username" value="${db.username}"></property>
      <property name="password" value="${db.password}"></property>
      <property name="url" value="${db.url}"></property>
      <property name="driverClassName" value="${db.driver}"></property>
   </bean>
   
   <!-- spring 整合jdbc   整合:使用spring管理jdbc
      jdbcTemplate :操作数据库的工具类 dbutils baseDao jdbcutil
    -->
   
   <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
      <property name="dataSource" ref="datasource"></property>
   </bean>
   
   <!-- 配置事务管理器:    管理事务 -->
     <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
         <property name="dataSource" ref="datasource"></property>
     </bean>
     <!-- 启用注解事务管理 -->
     <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
     
     
</beans>

后三者是重点,

事务注解中的部分主要的参数:

                                                                                                                                                   
                 //事务的传播行为                      //事务的隔离级别           //是否只读                   
    @Transactional(propagation=Propagation.REQUIRES_NEW,isolation=Isolation.REPEATABLE_READ,
readOnly=false,
            //超出时间   //不回滚的异常                //回滚的异常                                                                
             timeout=3,noRollbackFor=StockException.class,rollbackFor=RuntimeException.class)
@Override
	public void purchase(String userName, String isbn) {
           int price = bsd.findBookPrice(isbn);//查询单价
           System.out.println(price);
           int updateStock = bsd.updateStock(isbn);//减库存
           int updateAcount = bsd.updateAcount(userName, price);//减余额
          
	}

事务的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"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	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-4.0.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

<!-- 引入外部的属性文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 扫描 -->
 <context:component-scan base-package="com.atguigu.spring.jdbc"></context:component-scan>
<!-- 管理数据源dataSource -->
   <bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource">
      <property name="username" value="${db.username}"></property>
      <property name="password" value="${db.password}"></property>
      <property name="url" value="${db.url}"></property>
      <property name="driverClassName" value="${db.driver}"></property>
   </bean>
   
   <!-- spring 整合jdbc   整合:使用spring管理jdbc
      jdbcTemplate :操作数据库的工具类 dbutils baseDao jdbcutil
    -->
   
   <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
      <property name="dataSource" ref="datasource"></property>
   </bean>
   
   <!-- 配置事务管理器:    管理事务 -->
     <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
         <property name="dataSource" ref="datasource"></property>
     </bean>
     <!-- 启用注解事务管理 -->
   <!--    <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>  -->
    
    
    <!-- bean形式添加事务 --> 
    <tx:advice id="advice" transaction-manager="dataSourceTransactionManager">
      <tx:attributes ><!-- 具体添加事务的方法 -->
                      <!-- name是方法名称 -->
      		<tx:method name="purchase" timeout="-1" isolation="REPEATABLE_READ" read-only="false" propagation="REQUIRED"/>
      		<tx:method name="byBooks"/>
      </tx:attributes>
   </tx:advice>
     
     <!-- 为事务注册代理对象 -->
     <aop:config>
        <aop:pointcut expression="execution(* com.atguigu.spring.jdbc.service.*.*(..))" id="cut"/>
         <aop:advisor advice-ref="advice" pointcut-ref="cut"/>
     </aop:config>
     
     
</beans>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值