配置
log4j.properties
applicationContext.xml
引入约束地址:
spring-framework-4.2.4.RELEASE\docs\spring-framework-reference\html\xsd-configur
ation.html
加一句:<context:component-scan />
- 引入约束:(引入 context 的约束):
<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.xsdhttp://www.springframew
ork.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
Spring的Bean管理中的常用注解
-
@Component:组件(作用在类上)
Spring中提供@Component的三个衍生注解:(功能目前来说是一致的)- @Controller :WEB层
- @Service :业务层
- @Repository :持久层
这三个注解是为了让标注类本身的用途清晰,Spring在后续版本会对其增强
-
属性注入的注解:(使用注解注入的方式,不用提供set方法)
@value :用于注入普通类型
@Autowired :自动装配
默认按类型进行装配按名称注入:@Qualifier:强制使用名称注入
@Resource相当于@Autowired和@Qualifier一起使用
Bean的作用范围的注解
@Scope:
singleton:单例
prototype:多例
Bean的生命周期的配置
@PostConstruct:相当于init-method
@PreDestroy :相当于destroy-method
XML和注解
XML的结构清晰,注解方式开发方便
实际开发中有一种XML和注解整合开发
Bean有XML配置,但是使用的属性使用注解注入