Spring In Action(一):基于注解装配bean
来源:互联网 发布:苏州淘宝美工招聘 编辑:程序博客网 时间:2024/06/04 18:25
使用到的注解
(1)@Component注解,将一个类作为组件
(2)@ComponentScan注解,将会扫描包下的所有类,找出带有@Component注解的类,并实例化bean
(3)@Autowired注解,自动装配,默认使用byType进行装配,完成bean的实例化,@Autowired注解不仅适用于属性,也适用于setter方法和构造器,一般来说,只能有一个bean适合装配到@Autowried注解所标注的属性或参数中,如果没有合适的bean或者有多个,将会抛出异常。
(a)解决没有合适的bean用于装配的办法是,设置@Autowired(required=false)来配置自动装配是可选的,当没有合适的bean时,将会注入null。
(b)解决有多个bean,不确定使用哪个bean来装配的办法是使用@Qualifier注解,指定所需要的bean
1.创建一个Spring项目,修改pom文件,添加Spring单元测试所需依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.study</groupId> <artifactId>spring-study</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>spring-study Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <!--使用单元测试所需jar包--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.8.RELEASE</version> </dependency> <!--spring相关包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.8.RELEASE</version> </dependency> <!--其他--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> </dependencies> <build> <finalName>spring-study</finalName> </build></project>
public interface Music { void play();}
创建一个PopMusic类,实现Music接口,重写play方法,同时添加@Component注解
//使用component注解将此类作为组件类,Spring会为此组件实例化bean@Componentpublic class PopMusic implements Music { public void play() { System.out.println("play pop music"); }}
3.为了让Spring自动为带有@Componet注解的类实例化,还需要一些其他的配置
方法一:使用@ComponetScan注解,启动组件扫描
(1)在包com.study.autoconfig下创建MusicConfig配置类,添加@ComponetScan注解
(a)@ComponentScan注解会扫描这个包及它的子包,查找带有@Component注解的类,并为其创建一个bean,MusicConfig和PopMusic在同一个包下,因此Spring会为PopMusic实例化
(b)也可以使用设置basePackages或basePackClasses的方式,指定需要扫描的包或类
////ComponentScan启用组件扫描@Configuration@ComponentScan//@ComponentScan(basePackages = {"com.study.autoconfig"}) //使用给定包名的方式,扫描组件//@ComponentScan(basePackageClasses = {PopMusic.class,Music.class})//使用给定类名的方式,扫描组件public class MusicConfig {}
(2)单元测试
在test目录下创建对应的包,并创建MusicTest类
(a)@RunWith指定单元测试的执行类,使用SpringJUnit4ClassRunner类,自动创建spring的应用上下文
(b)@ContextConfiguration指定从哪里加载配置
(c)@Autowired注解,为Music自动注入,由于PopMusic是Music接口的实现类,当Spring为PopMusic实例化后,会自动将bean为Muisc注入
package com.study.autoconfig;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)//自动创建Spring的应用上下文@ContextConfiguration(classes=MusicConfig.class)//由于@ComponentScan注解在MusicConfig中,因此需要从MusicConfig中加载配置public class MusicTest { //自动注入 @Autowired private Music music; @Test public void playTest(){ music.play(); }}
(3)执行单元测试,可以看到,测试通过,控制台打印 play pop music
方法二:使用<context:component-scan>,直接在配置文件中开启注解扫描配置
(1)修改资源文件中的applicationContext.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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--组件自动扫描配置--> <context:component-scan base-package="com.study.autoconfig"></context:component-scan></beans>
package com.study.autoconfig;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)//自动创建Spring的应用上下文//@ContextConfiguration(classes=MusicConfig.class)//由于@ComponentScan注解在MusicConfig中,因此需要从MusicConfig中加载配置@ContextConfiguration(locations = { "classpath*:/spring/applicationContext.xml" })//指定配置文件public class MusicTest { //自动注入 @Autowired private Music music; @Test public void playTest(){ music.play(); }}
3.运行结果,可以看到测试通过
参考:
(1)Spring in Action 第四版
(2)http://blog.csdn.net/fenglibing/article/details/8584602
源码下载:http://download.csdn.net/detail/lom9357bye/9856022
- Spring In Action(一):基于注解装配bean
- Spring In Action(二):基于XML配置装配bean
- Spring(3、基于注解装配Bean)
- spring in action-装配Bean
- spring-装配Bean 基于注解
- spring装配bean(基于注解)
- Spring 装配Bean 基于注解
- Spring-装配Bean 基于注解
- Spring in action 01 -- 装配 Bean(@Autowired)
- Spring in action 03 -- 装配 Bean(xml配置)
- 《Spring In Action》 读书笔记(1) -- bean装配
- 《Spring In Action》 读书笔记(2) -- bean装配
- Spring In Action 01 ---装配Bean
- Spring-Bean之基于注解(Annotation)装配解析
- Spring(七)基于注解装配bean
- 装配Bean 基于注解
- bean装配基于注解
- 装配Bean基于注解
- 内存
- maven配合MyBatis-CMEU快速搭建ssm框架
- 1084. Broken Keyboard (20)(PAT甲)
- 类加载器
- 机器学习算法~异常检测
- Spring In Action(一):基于注解装配bean
- 集合
- linux lvs 负载均衡
- poj1742-coins
- 设置jvm内存
- JS判断用户使用的浏览器类型
- 浅谈Unity资源异步加载和Coroutine的使用
- Android Studio 最全使用技巧和快捷键
- POJ2442 堆