1.扫描含有注解的类
<context:component-scan base-package="…">
2.常见的注解
@Component 组件,任意bean
WEB
@Controller web层
@Service service层
@Repository dao层
注入 --> 字段或setter方法
普通值:@Value
引用值:
类型:@Autowired
名称1:@Autowired @Qualifier(“名称”)
名称2:@Resource(“名称”)
作用域:@Scope(“prototype”)
生命周期:
初始化:@PostConstruct
销毁方法:@PreDestroy
1.4注解和xml混合使用
1.将所有的bean都配置xml中
2.将所有的依赖都使用注解
@Autowired
默认不生效。为了生效,需要在xml配置:context:annotation-config
总结:
注解1:<context:component-scan base-package=" ">
注解2:context:annotation-config
1.一般情况两个注解不一起使用。
2.“注解1”扫描含有注解(@Component 等)类,注入注解自动生效。
“注解2”只在xml和注解(注入)混合使用时,使注入注解生效。