1、 创建一个maven项目
2、导入依赖坐标
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
3、添加配置文件或者使用注解注入bean
(1) 创建配置文件applicationcontext.xml添加配置

(2)纯注解开发,相关注解
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
本文介绍了如何利用IntelliJ IDEA创建一个Spring项目,包括创建Maven项目、导入必要的依赖,以及通过配置文件或注解方式来管理Bean。涉及到的注解包括@Component、@Controller、@Service、@Repository、@Autowired、@Qualifier等,同时讲解了@Configuration、@ComponentScan、@PropertySource等配置类相关注解,以及环境切换、AOP和异步处理的相关概念。
1788

被折叠的 条评论
为什么被折叠?



