Java框架学习:Spring纯注解配置

applicationContext.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:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--开启扫描,解析将bean交给Spring容器的四个注解-->
    <context:component-scan base-package="com.young"/>

    <!--开启关于aop注解-->
    <aop:aspectj-autoproxy />

    <!--读取配置文件-->
    <context:property-placeholder location="classpath*:db.properties"/>

    <!--配置数据源,将数据源交给Spring容器-->
    <bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close" >
        <property name="url" value="${db.url}" />
        <property name="username" value="${db.username}" />
        <property name="password" value="${db.password}" />
        <property name="driverClassName"  value="${db.driverClassName}" />
    </bean>

    <!--将JdbcTemplate交给Spring容器-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
        <constructor-arg name="dataSource" ref="druidDataSource" />
    </bean>

    <!--配置Spring提供的事务控制切面类,也叫做事务控制管理器,并交给Spring容器-->
    <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--指定进行事务的管理的数据源-->
        <property name="dataSource" ref="druidDataSource"/>
    </bean>

    <!--开启事务注解解析器-->
  	<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
</beans>

对于配置文件的中的配置的功能,我们都需要使用注解来代替
需要使用一个配置类SpringConfig

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration                  	//声明该类为配置类
@ComponentScan("com.young")     	//开启扫描
@EnableAspectJAutoProxy         	//开启关于aop的注解
@PropertySource("db.properties")    //加载properties配置文件
@EnableTransactionManagement        //开启事务注解解析器
public class SpringConfig {
    @Value("${db.username}")
    private String username;
    @Value("${db.password}")
    private String password;
    @Value("${db.url}")
    private String url;
    @Value("${db.driverClassName}")
    private String driverClassName;

	//配置数据源,将数据源交给Spring容器
    @Bean
    public DruidDataSource getDruidDataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setUrl(url);
        dataSource.setDriverClassName(driverClassName);
        return dataSource;
    }

	//将JdbcTemplate交给Spring容器
    @Bean
    public JdbcTemplate getJdbcTemplate(DataSource dataSource){
        return new JdbcTemplate(dataSource);
    }

	//配置事务控制管理器,并交给Spring容器
    @Bean
    public DataSourceTransactionManager getDataSourceTransactionManager(DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }
}

测试类

public class TestAnnotation {
    @Test
    public void testAnnotation(){
    	//使用AnnotationConfigApplicationContext读取配置类
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        IAccountService accountService = context.getBean("accountService", IAccountService.class);
        accountService.transfer(100);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值