1、简单的属性注入。无需增加get/set方法

Spring注解开发-属性依赖注入_xml

注意:@Value @Autowired它们可以修饰属性,也可以修饰setter方法,如果写在属性上,就不需要提供setter方法。


2、复杂的属性注入

Spring注解开发-属性依赖注入_xml_02
Spring注解开发-属性依赖注入_其他_03

3、如果要扫描多个包下的注解可以写成以下:

<?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">

    <!-- <context:annotation-config /> -->

    <!-- 下面的配置作用是指定spring扫描的包,它包含了context:annotation-config的功能,所以在开发中一般就不需要在配置上面的 -->
    <context:component-scan base-package="cn.nwtxxb.annatation,cn.nwtxxb.dao" />

</beans>

也可以要扫描的包名设置为cn.nwtxxb父包,那么就都可以扫描到了.