Spring注解
| 注解 | 说明 |
| @Component | 使用在类上用于实例化Bean |
| @Controller | 使用在Web层用于实例化Bean |
| @Service | 使用在Service层用于实例化Bean |
| @Repository | 使用在Dao层用于实例化Bean |
| @Autowried | 使用在字段上用于根据类型依赖注入 |
| @Qualifier | 结合@Autowrited一起使用用于根据名称进行依赖注入 |
| @Resource | 相当于@Autowried+@Qualifier 按照名称进行注入 |
| @Value | 注入普通属性 |
| @Scope | 标注Bean的作用范围 |
| @PostConstruct | 使用在方法上标注该方法是Bean的初始化方法 |
| @PreDestroy | 使用在方法上标注该方法是Bean的销毁方法 |
| @Configuration | 用于指定当前类是一个Spring配置类,当创建容器时会从该类上加载注解 |
| @ComponentScan |
用于指定Srping在初始化容器时要扫描的包。 作用和在Spring的Xml配置文件中的 <context:component-scan base-packge="com.zhan"/> 一样 |
| @Bean | 使用在方法上,标注将该方法的返回值存储到Spring容器中 |
| @PropertySource | 用于加载properties文件中的配置 |
| @Import | 用于导入其他配置类 |
| @RunWith | @runWith注解作用: --@RunWith就是一个运行器 --@RunWith(JUnit4.class)就是指用JUnit4来运行 --@RunWith(SpringJUnit4ClassRunner.class),让测试运行于Spring测试环 境,以便在测试开始的时候自动创建Spring的应用上下文 --@RunWith(Suite.class)的话就是一套测试集合 |
@ContextConfiguration |
@ContextConfiguration Spring整合JUnit4测试时,使用注解引入多个配置文件
@ContextConfiguration(locations="../applicationContext.xml") //导入配置文件 @ContextConfiguration(classes = SimpleConfiguration.class) //导入配置类 @ContextConfiguration(locations = { "classpath*:/spring1.xml", "classpath*:/spring2.xml" }) // 引用多个配置文件
|
Nice to see you all!

浙公网安备 33010602011771号