前提:
引入約束:
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
開啓Spring註解模式:
<!--開啓Spring註解掃描(掃描com包下所有的類)-->
<context:component-scan base-package="com"></context:component-scan>
1、基礎註解:
關於類:
pojo包中自己寫的類:@Component 等價於 @Component("類名首字母小寫")
Component的三個衍生註解分別用於三層類:@Controller,@Service,@Repository
[修改對象的作用範圍:@Scope:singleton/prototype]
關於類中的成員變量:
值類型注入:在成員變量/該set()方法上加入@Value("值")
引用類型注入:
自動裝配(按類型進行裝配):@Autowired
按名稱裝配:
@Autowired + @Qualifier("一般爲bean的id[類名首字母小寫]")
自動裝配+按名稱裝配:@Resource(name=" ")