注册bean的注解
@Component 用在类上,相当于bean标签
@Controller 用在web层类上,注册一个bean(是@Component
的衍生注解)
@Service 用在service层类上,注册一个bean(是@Component
的衍生注解)
@Repository 用在dao层类上,注册一个bean(是@Component
的衍生注解)
配置bean的注解
@Scope 相当于bean标签的scope
属性 (默认单例singleton)多例(prototype)
@PostConstruct 相当于bean标签的init-method
属性
@ PreDestroy 相当于bean标签的destory-method
属性
依赖注入的注解
@Autowired 相当于property标签的ref
@Qualifier 结合@Autowired
使用,用于根据名称注入依赖
@Resource 相当于@Autowired + @Qualifier
@Value 相当于property标签的value 注入简单值
纯注解开发IoC
@Configuration 被此注解标记的类,是配置类,可以代替掉xml文件
@ComponentScan 用在配置类上,开启组件扫描。使用basePackage属性指定扫描的包
@PropertySource 用在配置类上,加载properties文件。使用value属性指定properties文件路径
@Import 用在配置类上,引入子配置类。用value属性指定子配置类的Class
@Bean 用在配置类的方法上,把返回值注册bean对象。用name/value属性指定bean的id
Spring整合Junit
@Runwith 用在测试类上,用于声明不再使用Junit,而是使用Spring提供的运行环境
@ ContextConfiguration 用在测试类上,用于指定Spring配置类、或者Spring的配置文件