如何用idea快速创建一个Spring项目

1、 创建一个maven项目

2、导入依赖坐标

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.2.10.RELEASE</version>
</dependency>

3、添加配置文件或者使用注解注入bean

(1) 创建配置文件applicationcontext.xml添加配置

bean生成类

@Component

@Controller

@Service

@Repository

注入bean的注解

@Autowired 自动装配注解,在需要注入的bean上添加

@Qualifier("bean的id") 配合Autowired可以匹配指定bean

@Resource("bean的id") 功能和@Autowired+@Qualifier("bean的id")一样的功能

@Primary在类上添加指定可以匹配的bean

@Inject在构造方法上使用 @Inject 时,其参数在运行时由配置好的IoC容器提供

配置相关

@Configuration 标记一个类为配置类,此类中可能有n个被@Bean注解标记的方法用来完成spring容器的初始化.

@ComponentScan 用于配置类,实现对需要扫描的范围的配置

@PropertySource 加载配置文件

@Value 可以实现普通注入。

@Bean 使所修饰的类的返回值交由spring容器来控制

@Import 导入其他类包

环境切换

@Profile

@Conditional

AOP

@After,@Before,@Around,@PointCut

异步

@EnableAsync

@Async

定时任务

@EnableScheduling

@Scheduled

经验分享 程序员 微信小程序 职场和发展