天天看點

Spring4 入門案例 注解開發

1 新鍵web項目

2 引入jar包---在Spring4(3不用)的版本中,除了引入基本的開發包以外,還需要引入aop的包

Spring4 入門案例 注解開發

3 建立配置檔案applicationContext.xml

          3.1引入限制spring-framework-4.2.4.RELEASE-dist.zip\spring-framework-4.2.4.RELEASE\docs\spring-framework-reference\html\xsd-configuguration.html裡面的40.2.8 the context schema

<?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: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"> <!-- bean definitions here -->

</beans>
           

       3.2 配置掃描--指定那些包下的類使用IOC注解

<context:component-scan base-package="com.spring.demo1"></context:component-scan>
           

4 在類上添加注解

@Component(value="UserDao")//相當于<bean id="UserDao" class="com.spring.demo1.UserDapImpl">
           
Spring4 入門案例 注解開發

5 使用注解方式設定屬性值

        屬性如果有set方法,需要将屬性注入的注解添加到set方法。

Spring4 入門案例 注解開發

        屬性如果沒有set方法,需要将屬性注入的注解添加屬性上。

Spring4 入門案例 注解開發