@Configuration是spring.xml的注解版。

@ComponentScan是<context:component-scan base-package="com.coshaho.*" />标签的注解版。

@ImportResource @Import是<import resource>标签的注解版。

@PropertySource是<context:property-placeholder location="classpath:jdbc.properties"/>标签的注解版。

@Bean是<bean>标签的注解版。

@EnableTransactionManagement是tx:annotation-driven标签的注解版。

我们把如下spring配置文件修改为@Configuration配置类

<?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.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 注解注入 -->
<context:component-scan base-package="com.coshaho.*" /> <!-- 引入jdbc配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 使用spring初始化DataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 使用JdbcTemplate封装DataSource -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 定义事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--使用注释事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>

对应配置类为

package com.coshaho;

import java.beans.PropertyVetoException;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Import;
//import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.core.JdbcTemplate;
//import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement; import com.coshaho.dao.UserDao;
import com.coshaho.service.UserService;
import com.coshaho.service.UserServiceImpl;
import com.mchange.v2.c3p0.ComboPooledDataSource; @Configuration
@ComponentScan
@PropertySource("classpath:jdbc.properties")
//@Import(OtherConfiguration.class)
@EnableTransactionManagement
//@ImportResource("classpath:transaction.xml")
public class ApplicationConfig
{
@Value("${jdbc.driverClass}")
private String driverClass; @Value("${jdbc.jdbcUrl}")
private String jdbcUrl; @Value("${jdbc.user}")
private String user; @Value("${jdbc.password}")
private String password; @Bean
public ComboPooledDataSource dataSource() throws PropertyVetoException
{
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(driverClass);
dataSource.setJdbcUrl(jdbcUrl);
dataSource.setUser(user);
dataSource.setPassword(password);
return dataSource;
} @Bean
public JdbcTemplate jdbcTemplate() throws PropertyVetoException
{
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource());
return jdbcTemplate;
} // @Bean
// public DataSourceTransactionManager transactionManager() throws PropertyVetoException
// {
// DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
// transactionManager.setDataSource(dataSource());
// return transactionManager;
// } @Bean
public UserService userService(UserDao userDao)
{
UserServiceImpl userService = new UserServiceImpl();
userService.setUserDao(userDao);
return userService;
} private static ApplicationContext context;
public static void main(String[] args)
{
context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
UserService userService = (UserService) context.getBean("userService");
System.out.println(userService.query(1));
userService.create2User();
}
}

详细解释一下

1、@ComponentScan不添加base-package值,则表示默认从配置类路径扫描。

2、UserService依赖UserDao,则可以直接在构造方法中添加UserDao参数,spring自动按照类型注入。

3、@Import可以加载其他配置类。

4、一开始不知道如何在配置类中开启事务,便把事务配置在一个单独的xml中,通过@ImportResource引入。实际上直接使用@EnableTransactionManagement即可开启事务。

vo层,dao层代码和《基于注解的Spring事务配置》中的代码一致,用例中新增ApplicationConfig和service层代码,service层代码如下

package com.coshaho.service;

import com.coshaho.vo.User;

public interface UserService
{
public void create(String name, int age);
public User query(int id);
public void create2User();
}
package com.coshaho.service;

import com.coshaho.vo.User;
import com.coshaho.dao.UserDao; public class UserServiceImpl implements UserService
{
private UserDao UserDao; public void create(String name, int age)
{
UserDao.create(name, age);
}
public User query(int id)
{
return UserDao.query(id);
}
public void create2User()
{
UserDao.create2User();
}
public void setUserDao(UserDao userDao) {
UserDao = userDao;
}
}

Spring Configuration注解使用的更多相关文章

  1. Spring @Configuration注解

    1 该注解的用途 这个注解表示这个类可以作为spring ioc容器bean的来源,其本质上它是对xml文件中创建bean的一种替换.有了这个注释,Spring framework就能在需要的时候构造 ...

  2. spring configuration 注解

    org.springframework.context.annotation @annotation.Target({ElementType.TYPE}) @annotation.Retention( ...

  3. Spring 学习——Spring常用注解——@Component、@Scope、@Repository、@Service、@Controller、@Required、@Autowired、@Qualifier、@Configuration、@ImportResource、@Value

    Bean管理注解实现 Classpath扫描与组件管理 类的自动检测与注册Bean 类的注解@Component.@Service等作用是将这个实例自动装配到Bean容器中管理 而类似于@Autowi ...

  4. [转]Spring注解-@Configuration注解、@Bean注解以及配置自动扫描、bean作用域

    1.@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans>,作用为:配置spring容器(应用上下文) package com.test.s ...

  5. Spring boot之SpringApplicationBuilder,@@Configuration注解,@Component注解

    SpringApplicationBuilder: 该方法的作用是可以把项目打包成war包 需要配置启动类,pom.xml文件等,具体见:http://blog.csdn.net/linzhiqian ...

  6. Spring注解-@Configuration注解、@Bean注解以及配置自动扫描、bean作用域

    1.@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans>,作用为:配置spring容器(应用上下文) package com.test.s ...

  7. spring和springmvc中,Configuration注解Bean重复加载

    问题:bean重复加载1.如下代码所示,开启Configuration注解,实现Bean代码注入,发现bean重复加载 @Configuration public class EhCacheConfi ...

  8. 品Spring:注解之王@Configuration和它的一众“小弟们”

    其实对Spring的了解达到一定程度后,你就会发现,无论是使用Spring框架开发的应用,还是Spring框架本身的开发都是围绕着注解构建起来的. 空口无凭,那就说个最普通的例子吧. 在Spring中 ...

  9. Spring源码之@Configuration注解解析

    1.前言 ​ Spring注解开发中,我们只需求要类上加上@Configuration注解,然后在类中的方法上面加上@Bean注解即可完成Spring Bean组件的注册.相较于之前的xml配置文件定 ...

随机推荐

  1. angular.isDefined()

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. Java三大集合框架

    定义:java中集合类:是一种工具类,就像是容器,储存任意数量的具有共同属性的对象 一.List集合 1.List实现的超级父类接口:Collection 2.了解ArrayList类 A):定义的格 ...

  3. 利用PIL和Selenium实现页面元素截图

    预备 照张相片 selenium.webdriver可以实现对显示页面的截图: from selenium import webdriver dr = webdriver.Firefox() dr.g ...

  4. 根据屏幕自适应宽度:@media

    @media screen and (min-width: 1490px){ .w1224{ width: 1400px !important; }}@media screen and (max-wi ...

  5. day 23 二十三、对象方法,类方法,封装,绑定方法

    一.对象的特有名称空间 __init__方法会在实例化对象时被调用 1.会为实例化的对象形成空的名称空间 2.就是一个方法,可以被传参,在类名(实参)这种方式下调用并传参 __init__(self ...

  6. Python 学习笔记6 变量-元组

    我们在上一篇中了解了变量list(列表), 今天我们来介绍下元组.元组是由括号和逗号,组织起来的一个元素的集合.和list不同的是,它其中的元素是不能被修改的,和其他语言中的常量相类似. 需要注意的是 ...

  7. Python自动化培训第一周学习总结

    Python自动化培训第一周学习结束,看视频复习,把作业完成了. 总体来说,开卷有益. 首先,工具真是好东西,能够极大提升效率,也是人区别于动物所在.想起前任大领导对工具的不屑,本质也是对效率的不屑, ...

  8. 第二次C语言实验

    Part1: printf(),scanf()函数的用法 /* C语言程序设计教程学习指导>p119 实验内容(2) 这是一个格式化输入输出函数及格式符使用练习 找出两处错误,修改并运行程序 为 ...

  9. 完美脱离Windows!! Linux发行版第一系统 Manjaro 开箱教程 :)

    没兴趣? 来几张图敌敌畏(kai kai wei) !! 0x00 预览(zhuangbi) 0x01 引言(feihua) 当我们想用ssh工具时,不像telnet那样是系统自带的软件,需要额外安装 ...

  10. memory error python报错

    np.array时报错内存溢出,检查了python安装的是64位版本,通过下面dtype=np.uint8不再报错texts_vec = (np.array(texts_vec,dtype=np.ui ...