技术:Configuration,ComponentScan,PropertySource,EnableTransactionManagement,Bean,Value
说明:文中只贴出了配置代码,完整的测试代码在github上。
源码:https://github.com/ITDragonBlog/daydayup/tree/master/Spring/itdragon-spring-anno
文章目录结构:
一、Java编程配置
在Spring4.x之前,应用的基本配置中一般使用xml配置的方式,而在业务逻辑中建议使用注解的方式。可在Spring4.x以后,官方便开始推荐使用Java的编程配置来代替xml配置,这又是为什么?这两种配置又有什么优缺点呢?
Java编程配置和xml配置
xml配置优点:对于我们这些老一辈的程序员来说(┬_┬),xml非常亲切,使用简单,容易扩展,修改应用配置参数不需要重新编译。
xml配置缺点:配置文件的读取和解析需要耗时,xml配置文件内容太多会显得很臃肿,不方便管理。
Java编程配置优点:相对于xml配置而言,其结构更清晰,可读性更高,同时也节省了解析xml耗时。
Java编程配置缺点:修改应用配置参数需要重新编译。其实并不是一个大的问题,实际生成环境中,应用配置完成后一般都不会也不敢去随意修改。
两者各有千秋,考虑到Spring4.x和SpringBoot都在推荐使用Java编程配置的方式,那我们也应该顺应时代潮流,你可以不用,但你应该要懂!
Java编程配置步骤
第一步:创建配置类,在类名上添加注解Configuration,告知Spring这是一个配置类,其作用类似xml文件
第二步:加载外部配置文件,在类名上添加注解PropertySource,指定properties文件的读取路径
第三步:获取应用配置属性值,在属性变量上添加注解Value,通过${}表达式获取配置文件中参数
第四步:依赖注入,在方法上添加Bean注解,也可以用FactoryBean
第一步和第四步的语法,文章第二部分会详细介绍。第二步和第三步的语法,文章第三部分会详细介绍。
import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotati
《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》
【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享
on.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import com.mchange.v2.c3p0.ComboPooledDataSource; /** * Spring 配置类 * 配置数据源,事务管理,bean,自动扫描包 * @author itdragon */ @Configuration // 声明该类为配置类 @PropertySource({“classpath:propertySource.properties”}) // 引入外部文件 @ComponentScan(“com.itdragon”) // 配置自动扫描包的路径 @EnableTransactionManagement // 开启基于注解的事务管理功能 public class ApplicationContextConfig { @Value(" D A T A U S E R " ) p r i v a t e S t r i n g D A T A U S E R ; @ V a l u e ( " {DATA_USER}") private String DATA_USER; @Value(" DATAUSER")privateStringDATAUSER;@Value("{DATA_PAWD}") private String DATA_PAWD; @Value(" D A T A D R I V E R " ) p r i v a t e S t r i n g D A T A D R I V E R ; @ V a l u e ( " {DATA_DRIVER}") private String DATA_DRIVER; @Value(" DATADRIVER")privateStringDATADRIVER;@Value("{DATA_JDBC_URL}") private String DATA_JDBC_URL; @Bean // 数据源 public DataSource dataSource() throws Exception{ ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setUser(DATA_USER); dataSource.setPassword(DATA_PAWD); dataSource.setDriverClass(DATA_DRIVER); dataSource.setJdbcUrl(DATA_JDBC_URL); return dataSource; } @Bean // jdbc模板 public JdbcTemplate jdbcTemplate() throws Exception{ JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource()); return jdbcTemplate; } @Bean // 事务管理 public PlatformTransactionManager transactionManager() throws Exception{ return new DataSourceTransactionManager(dataSource()); } }
事务类
import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.itdragon.dao.ITDragonDao; @Service public class ITDragonServer { @Autowired(required=false) private ITDragonDao itdragonDao; public List<Map<String,Object>> findAll() { return itdragonDao.findAll(); } @Transactional public void updateNameById(String name, Long id) { itdragonDao.updateUserNameById(name, id); System.out.println(0/0); // 事务异常 } }
完整代码,请异步github
二、组件注入
Bean注解类似xml文件中的<bean>
标签,其中被Bean注解修饰的方法名对应<bean>
标签中的id,也可以通过Bean注解的value属性设置id的值。在SpringBoot底层代码中被大量使用。
Bean注解类似xml文件中的<bean>
标签,其中被Bean注解修饰的方法名对应<bean>
标签中的id,也可以通过Bean注解的value属性设置id的值。在SpringBoot底层代码中被大量使用。