Spring 注解1 部分注解

4 篇文章 0 订阅
3 篇文章 0 订阅

Spring原始注解 目的是代替Spring的的配置
(xml)

@Component
使⽤在类上⽤于实例化Bean
@Controller
使⽤在Web层类上⽤于实例化Bean
@Service
使⽤在Service层类上⽤于实例化Bean
@Repository
使⽤在Dao层类上⽤于实例化Bean
@Autowired
使⽤在字段上⽤于根据类型依赖注⼊
@Qualifier
结合@Autowired⼀起使⽤⽤于根据名称进⾏依赖注⼊
@Resource
相当于@Autowired+@Qualifier,按照名称进⾏注⼊
@Value
注⼊普通属性
@Scope
标注Bean的作⽤范围
@PostConstruct
使⽤在⽅法上标注该⽅法是Bean的初始化⽅法
@PreDestroy
使⽤在⽅法上标注该⽅法是Bean的销毁⽅法
@Transactional
在需要进⾏事务控制的类或是⽅法上开启事务

1,需要在applicationContext.xml中配置组件扫描,作⽤是指定哪个包及其⼦包下的Bean需要进⾏
扫描以便识别使⽤注解配置的类、字段和⽅法;

<!--配置包扫描   扫描注解-->
<context:component-scan base-package="com.hpe"/>

2,配置⽂件中要开启事务的注解驱动<tx:annotation-driven />,同时指定事务管理器;

<!--事务的注解驱动-->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>

3,在使⽤原始注解情况下,第三⽅的类(不是⾃⼰定义的类)只能在XML中使⽤ 标签配
置,⽆法使⽤上述注解进⾏配置。也就是说不能完全去除xml文件 但可以简化
变成这个样子
难看,找给工具粘贴一下,注释都是可以省略的

<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           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.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--引入外部配置文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--配置包扫描   扫描注解-->
    <context:component-scan base-package="com.hpe"/>
    <!--dataSource-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!--注入属性--><!--.trim()可以去除空格-->
        <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>
    <!--JdbcTemploate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
    <!--配置dao-->
    <!--<bean id="accoountDao" class="com.hpe.dao.impl.AccoountDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>-->
    <!--切点    service-->
    <!--<bean id="accountService" class="com.hpe.service.impl.AccountService">
        <property name="accountDao" ref="accoountDao"/>
    </bean>-->
    <!--配置平台事务管理器   提交和回滚事务-->
    <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--通知, 事务的增强-->
   <!-- <tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager">
        <tx:attributes>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>-->
    <!--织入-->
<!--<aop:config>-->
    <!--切点表达式   任意包下的任意类的任意方法-->
    <!--<aop:pointcut id="pt" expression="execution(* com.hpe.service.impl.*.*(..))" />-->
    <!--<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>-->
<!--</aop:config>-->
    <!--事务的注解驱动-->
    <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
</beans>

在dao层

//<bean id="accoountDao" class="com.hpe.dao.impl.AccoountDaoImpl">
@Repository("accountDao")  //代替上面的一行
public class AccoountDaoImpl implements IAccountDao {
   1//@Autowired//根据类型进行注入   可以省略set方法
       //@Qualifier("jdbcTemplate")//根据id进行注入 就可以建多个template了
                                  //多个bean进行选择时用
    2@Resource(name = "jdbcTemplate")//相当于@Autowired+@Qualifier  根据id进行注入 就可以建多个template了
    private JdbcTemplate jdbcTemplate;
//    public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
//        this.jdbcTemplate = jdbcTemplate;
可以不用set方法了
//  }

@PostConstruct   Bean的初始化
public void init(){
    System.out.println("初始化方法");
}
@PreDestroy    Bean的销毁
public void destoy(){ 
    System.out.println("销毁");
}

配置service

//<bean id="accountService" class="com.hpe.service.impl.AccountService">
@Service("accountService")
public class AccountService implements IAccountService {
     //<property name="accountDao" ref="accoountDao"/>
    @Autowired
    private IAccountDao accountDao;
@Override
@Transactional //表示对此方法进行事务相关操作
public void transfer(Integer srcId, Integer dstId, Integer money) throws Exception {
    Account src = accountDao.findById(srcId);
    Account dst = accountDao.findById(dstId);
    if(src == null){
        throw new RuntimeException("转出用户不存在");
    }
    if (dst == null){
        throw new RuntimeException("转入用户不存在");
    }
    if (src.getMoney() < money){
        throw new RuntimeException("转出账户余额不足");
    }
    src.setMoney(src.getMoney() - money);
    dst.setMoney(dst.getMoney() + money);

    accountDao.update(src);
    accountDao.update(dst);
}
}

测试类中

@Test
public void Testrans() throws Exception {
    //创建spring容器
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    //获取代理对象
    IAccountService accountService = (IAccountService) context.getBean("accountService");
    //转账
    accountService.transfer(1,2,10);
    //销毁
   context.close();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值