您的位置:首页 > 编程语言 > Java开发

Spring IOC 笔记

2018-03-08 11:18 302 查看
1.导包

<!-- 配置spring版本 -->
<properties>
<org.springframework.version>4.2.1.RELEASE</org.springframework.version>
</properties>

<dependencies>
<!-- Spring基础jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- 测试包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version> ${org.springframework.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
</dependencies>


2.使用java config配置Spring

@Configuration//定义以java config来配置spring

@ComponentScan//扫描本路径下所有包

@ComponentScan(“包名”)//指定要扫描的包

@ComponentScan(“basePackage={“”,”“,…}”)//指定多个要扫描的包

3.@Component默认以类名首字母小写为ID名,可以使用@Component(”*“)重新命名

4.不同类实现同一接口在自动装配的使用需要用@Qualifier(“类名ID”)来装备对应类

5.@Bean实现显示注解

6.@profile(“**”)设置profile

7.@ActiveProfiles(”**”)选择profile

8.@Primary标识首选Bean

9.@Scope(”“)//限定作用域

10.使用@PropertySource注解和Environment注入外部的值

11.使用#{}和SpEL表达式注入,使用”?.”判断值是否为空

12.#{2 * T(java.lang.Math).PI * circle.radius}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: