開發中使用註解 取代 xml配置文件
- @Component 取代 <bean class="">
- @Component("id") 取代 <bean id="" class="">
web開發中提供3個@Component註解衍生註解(功能一樣)
- @Repository("beanID(名)"): dao層
- @Service("beanID(名)"): service層
- @Controller("beanID(名)"): web層
常用註解
- @Autowired: 自動 根據 類型 注入
- @Qualifier("beanID(名)"):指定自動注入的id名稱
- @Resource("beanID(名)") == = @Autowired+@Qualifier("beanID(名)")
- @scope("prototype") 不寫默認單例
- @ PostConstruct 自定義初始化
- @ PreDestroy 自定義銷燬
Spring開啓註解 : xml文件配置
<?xml version="1.0" encoding="UTF-8"?>
<!--xmlns xml namespace:xml命名空間-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p ="http://www.springframework.org/schema/p"
xmlns:context ="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 開啓註解-->
<context:annotation-config/>
<!-- 註解掃描包位置-->
<context:component-scan base-package="com.zjc.包名--掃描路徑"/>
</beans>