applicationContext.xml 文件
在類上 ,使用以下注解,實現bean 的聲明
@Component 泛指組件,當組件不好歸類的時候,我們可以使用這個注解進行標注。
@Service 用於標注業務層組件
@Controller 用於標注控制層組件(如srping mvc的controller,struts中的action)
@Repository 用於標注數據訪問組件,即DAO組件
示例:
@Controller
@RequestMapping(value = "/test")
public class TestController {
}
------------------------------------------------------------------------------------------------------------------
在類的成員變量上,使用以下注解,實現屬性的自動裝配
@Autowired : 按類 的 類型進行裝配
@Resource (推薦) : 1 如果同時指定了name和type,則從Spring上下文中找到唯一匹配的bean進行裝配,找不到則拋出異常
2. 如果指定了name,則從上下文中查找名稱(id)匹配的bean進行裝配,找不到則拋出異常
3.如果指定了type,則從上下文中找到類型匹配的唯一bean進行裝配,找不到或者找到多個,都會拋出異常
4.如果既沒有指定name,又沒有指定type,則自動按照byName方式進行裝配;如果沒有匹配,則回退為一個原始類型進行匹配,如果匹配則自動裝配;
@Resource注解在字段上,這樣就不用寫setter方法了,並且這個注解是屬於J2EE的,減少了與spring的耦合。
示例:
@Resource
private TestServiceImpl testServiceImpl;
applicationContext.xml模板:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> </beans>

1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 8 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 9 10 <!-- 配置自定掃描的包 --> 11 <context:component-scan base-package="com.atguigu.springmvc"></context:component-scan> 12 13 <!-- 配置視圖解析器: 如何把 handler 方法返回值解析為實際的物理視圖 --> 14 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 15 <property name="prefix" value="/WEB-INF/views/"></property> 16 <property name="suffix" value=".jsp"></property> 17 </bean> 18 19 <!-- 配置視圖 BeanNameViewResolver 解析器: 使用視圖的名字來解析視圖 --> 20 <!-- 通過 order 屬性來定義視圖解析器的優先級, order 值越小優先級越高 --> 21 <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"> 22 <property name="order" value="100"></property> 23 </bean> 24 25 <!-- 配置國際化資源文件 --> 26 <bean id="messageSource" 27 class="org.springframework.context.support.ResourceBundleMessageSource"> 28 <property name="basename" value="i18n"></property> 29 </bean> 30 31 <!-- 配置直接轉發的頁面 --> 32 <!-- 可以直接相應轉發的頁面, 而無需再經過 Handler 的方法. --> 33 <mvc:view-controller path="/success" view-name="success"/> 34 35 <!-- 在實際開發中通常都需配置 mvc:annotation-driven 標簽 --> 36 <mvc:annotation-driven></mvc:annotation-driven> 37 38 </beans>