天天看点

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 入门案例 注解开发