Road SSM learning --spring fourth day _ pure annotation achieve transaction control

Some configuration before Xuechun annotation, not specifically record, here integrated in the school configuration before pure annotation method, recall, and to add new things based on transaction control.

In the previous section has transaction control, and comments by xml integrated manner, configure up, try this one completely removed xml files to xml configuration and the same function by Config entity classes.

A, pom.xml dependency

	<!--打包方式为jar-->
    <packaging>jar</packaging>
    <dependencies>

        <!--spring框架的基本依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <!--spring的aop依赖-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.7</version>
        </dependency>

        <!--spring的shiwu事务管理器依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <!--单元测试的依赖-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!--spring整合junit的依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <!--mysql的依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <!--spring的jdbc依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
    </dependencies>

Second, write code

Here Insert Picture Description
A lot of code here, such as IAccountDao interface and its implementation class, IAccountService interface and its implementation class, and Account account entity classes, and one is on the road SSM learning --spring fourth day _ using xml transaction control to achieve basic consistent, and using annotations to configure services, in --spring fourth day _ SSM way of learning to use annotations to achieve transaction control has been described, just a little bit of small change, various other places will be mentioned below

Different points:
a, AccountDaoImpl class
       before inherited a spring to provide us with the JdbcDaoSupportclass, because we now use annotations to add to the IOC container accountDaoImpl, and the JdbcDaoSupportclass of jdbcTemplate is not annotated by injection (see road SSM learning --spring fourth day _ using xml implement transaction control ), so we wanted to change:
Here Insert Picture Description

Comparison follows:
Here Insert Picture Description

这样,我们就去掉了xml中原本对应的Here Insert Picture Description
二、Client类:
由于我们之前是通过xml配置的,现在是通过纯注解,所以我们的main函数里面ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");引用的bean.xml已经不复存在,进而我们是从一个配置类SpringConfig类里面读取配置(具体的配置在下面会具体提到),所以这句代码应该改成:ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfig.class);

三、编写配置类(重点)

一、SpringConfig类:(主配置类)

//声明这是一个配置类
@Configuration
//声明注解扫描的包
@ComponentScan("com.itheima")
//导入子配置类
@Import({jdbcConfig.class,transactionConfig.class})
//指定配置类所需要读取的Properties文件,位于resources资源文件目录下
@PropertySource("dataSource.properties")
//声明支持事务管理器
@EnableTransactionManagement

public class SpringConfig {
}

二、jdbcConfig类:
这里的@Bean注解也是通过一个方法来返回创建所需要的jdbcTemplate对象和dataSource数据源对象,并加入spring的IOC容器中,并通过@Value来读取dataSource.properties文件里的数据,注入给数据源需要的字段。

public class jdbcConfig {


    @Value("${jdbc.driver}")
    private String driver;

    @Value("${jdbc.url}")
    private String url;

    @Value("${jdbc.username}")
    private String username;

    @Value("${jdbc.password}")
    private String password;


    @Bean("jdbcTemplate")
    public JdbcTemplate createJdbcTemplate(DataSource dataSource){
        return new JdbcTemplate(dataSource);
    }
    
    @Bean("dataSource")
    public DataSource createDataSource(){
        DriverManagerDataSource ds = new DriverManagerDataSource();
        ds.setDriverClassName(driver);
        ds.setUrl(url);
        ds.setUsername(username);
        ds.setPassword(password);
        return ds;
    }
}

这一段配置,去掉了原本xml中对应的Here Insert Picture Description
三、transactionConfig类:
注意这里的返回值是PlatformTransactionManager,但是返回的是一个DataSourceTransactionManager对象,并传入一个数据源对象给这个事务管理器

public class transactionConfig {
    @Bean("transferManager")
    public PlatformTransactionManager createTransactionManager(DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }
}

这一个配置,去掉了xml中原本对应的Here Insert Picture Description

结合上一篇的注解的内容,完完全全把bean.xml中的内容一点不剩的干掉了,所以把bean.xml完完全全删掉,也可以运行。这样我们通过了配置类和注解,实现了无xml的写法。但是我们通过xml和注解的配合使用,是可以更方便我们开发的,我们可以使用注解来创建一些类,通过xml来配置事务管理器(防止写很多个@Transactional)

至此,spring的入门内容已经学习的差不多了,接下来跟着视频继续学习springMVC的内容,进行更深入的将SSM框架。

Published 23 original articles · won praise 0 · Views 589

Guess you like

Origin blog.csdn.net/SixthMagnitude/article/details/104202798