简单来说,就是不再使用xml配置文件来配置,改为使用配置类来进行配置
配置类
@Configuration
把一个类标记为配置类,把配置也作为容器中的组件(可以使用注入功能的注解)
@ComponentScan
扫描的范围
@EnableTransactionManagement
对应标签:<tx:annotation-driven transaction-manager="transactionManager"/>
@EnableAspectJAutoProxy
对应标签:<aop:aspectj-autoproxy/>
配置方法
@Bean
对应的是方法,将该方法的返回值注册为容器中的组件
返回值对应bean标签中的class属性值或其接口
value属性就是id,默认为方法名
配置参数
如果方法需要参数,且参数是容器中的组件,则使用
@Qualifier
value值为组件的id
配置测试类
@RunWith(SpringJUnit4ClassRunner.class)
使用junit写单元测试时,方便从Spring容器中取得对象
@ContextConfiguration(classes = ApplicationConfig.class)
配置类的位置
@Configuration
@ComponentScan("com.cskaoyan")
@EnableTransactionManagement
@EnableAspectJAutoProxy //aop:aspectj-autoproxy
public class ApplicationConfig {
@Autowired