spring学习笔记(持续更新)
前言
由于快到毕业季,本人无可奈何ssm的水平太差,所以决定开始写博客。目前工作还是没着落。只能够一边学习一边找了。希望能坚持下去。
注解注入
首先进入spring官网的spring framework(spring框架)
附网址(https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-definition)
直接看spring framework的指导手册的core部分。先转到1.9. Annotation-based Container Configuration(基于容器配置的注解)部分。1.9节开头便阐述了用XML注入和注解注入的的不同点,接下来正式开始学习IOC容器。
1.全注解方式
1.1基本注解
先转到1.10.3. Automatically Detecting Classes and Registering Bean Definitions(自动检测类及注册bean定义)
首先介绍的是一个基本的注解,能够替代XML的注入方式(扫描指定类路径里的bean),下面是XML的扫描并注册的方法。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="org.example"/>
</beans>
@Configuration
@ComponentScan(basePackages = "org.example",useDefaultFilters=false)
public class AppConfig {
// ...
}
useDefaultFilters=false 可以有效地将这个自动扫描组件的功能禁用掉。
@Configuration @ComponentScan (basePackages = “”)这两个注解适配以下四个注解,可以从指定类包里扫描并注册。
@Component(组件)
@Repository(仓库)
@Service(服务)
@Controller(控制器)