Spring笔记六 - 注解编程基础
作者: Wyt
系列文章目录
Spring笔记(1) - 工厂
Spring笔记(2) - AOP编程
Spring笔记(3) - 持久层整合
Spring笔记(4) - MVC框架整合
Spring笔记(5) - 注解编程入门
Spring笔记(6) - 注解编程基础
3. Spring的高级注解 (Spring3.x 及以上)
3.1 配置Bean
//Spring在3.x提供的新的注解,用于替换XML配置文件
@Configuration
public class AppConfig {
}
3.1.1 配置Bean的引用
1. 创建工厂代码
ApplicationContext ctx = new AnnotationConfigApplicationContext();
2. 指定配置文件
(1) 指定配置bean的Class
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
(2) 指定配置bean所在的路径
ApplicationContext ctx = new AnnotationConfigApplicationContext("com.wyt");
3.1.2 配置Bean开发的细节分析
3.1.2.1 基于注解开发使用日志
不能集成log4j
集成logback
(1) 引入jar
<!-- <dependency>-->
<!-- <groupId>org.slf4j</groupId>-->
<!-- <artifactId>slf4j-log4j12</artifactId>-->
<!-- <version>1.7.7</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>log4j</groupId>-->
<!-- <artifactId>log4j</artifactId>-->
<!-- <version>1.2.17</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.logback-extensions</groupId>
<artifactId