spring对于ioc其内部各个步骤的实现,大多依赖于反射操作,使用反射实例化,属性扫描,方法扫描,属性设值,注解扫描来实现无侵入式的Bean管理工具及Bean容器。

Spring的注入方式

手动注入:
构造方法
set方法
xml自动注入:在xml中使用autowire标签 替代之前的 property、constructor-arg,目的在于简化配置,仅此而已。
构造方法
set方法
注解自动注入: 使用反射逐个扫描类中属性和方法上的注解,然后交给容器进行注入(同时也会向父类扫描)
属性
构造方法
普通方法

Set方法注入

  1. ------ service ----
  2. public class DemoService {
  3. public String response() {
  4. System.out.println("23");
  5. return "sucess";
  6. }
  7. }
  8. ------ controller -------
  9. public class DemoController {
  10. DemoService demoService;
  11. public void response() {
  12. demoService.response();
  13. }
  14. // spring注入时,使用反射扫描到这个方法,调用该方法,同时参数带入从容器中找到的Bean
  15. public void setDemoService(DemoService demoService) {
  16. this.demoService= demoService;
  17. }
  18. }
  19. ------spring的配置 注意name是用在匹配注入方法中的,与set方法规范一致。 ref指注入的beanid
  20. ref不写的话,默认与name一致---
  21. <bean id="demoService" class="cn.com.aaic.spring.service.DemoService">
  22. </bean>
  23. <bean id="demoController" class="cn.com.aaic.spring.service.DemoController">
  24. <property name="demoService" ref="demoService"></property>
  25. </bean>
  26. ----------测试-------
  27. public class Test {
  28. public static void main(String[] args) {
  29. //初始化容器
  30. ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
  31. DemoController d = (DemoController)context.getBean("demoController");
  32. d.response();
  33. }
  34. }

构造方法注入

----- 构造方法进行注入 ------ 
public class DemoController {
    DemoService demoService;

    //在容器构建bean时,调用构造方法,同时将找到的demoService Bean带入
    public DemoController(DemoService demoService) {
        this.demoService = demoService;
    }

    public void response() {
        demoService.response();
    }
}

------配置,这里直接使用ref, 没有名字的匹配-----

    <bean id="demoService" class="cn.com.aaic.spring.service.DemoService">  
    </bean>

    <bean id="demoController" class="cn.com.aaic.spring.service.DemoController">  
            <constructor-arg ref="demoService"></constructor-arg>    
    </bean>

xml自动注入 - Set方法注入

与set方法注入区别不大,只是property配置改为自动匹配的方式

<bean id="demoService" class="cn.com.aaic.spring.service.DemoService">  
</bean>

------  使用autowire 会找到类中所有的Set方法 ,将bean注入。
    有byType和byName  ,byName时要特别注意set方法的规范性----

<bean id="demoController"  class="cn.com.aaic.spring.service.DemoController"  
    autowire="byName">  
</bean>

xml自动注入 - 构造方法注入

<bean id="demoService" class="cn.com.aaic.spring.service.DemoService">  
</bean>

------ autowire配置为constructor 即为构造方法自动注入 ------
<bean id="demoController"  class="cn.com.aaic.spring.service.DemoController"  
        autowire="constructor">  
</bean>

注解注入

用来简化、优化注入的配置操作
这里只展示一种属性的注解注入,其它两种基本一致

----配置  加入自动扫包,扫包的目的在于扫描哪些类被spring管理----

<context:annotation-config />  --打开注解有效开关
<context:component-scan base-package=" cn.com.aaic.spring.service">
</context:component-scan>    

----代码  @Controller 标识为该类被spring管理 @Autowired 标识注入点 ----- 

@Controller
public class DemoController {
    @Autowired
    DemoService demoService;

    public void response() {
        demoService.response();
    }
}

@Vlaue的的注入用法

    // 初始化时直接注入字符,不做任何处理
    @Value("注入的文本")
    String para ;

    // $ 从常量配置文件中读取val.para1   或者从yml中读取
    @Value("${val.para1}")
    String para1;

    //读取系统属性
    @Value("#{systemProperties['os.name']}")
    String systemPropertiesName; 

    //写SpEL表达式
    @Value("#{ T(java.lang.Math).random() * 100.0 }")
    double randomNumber; 

    //直接注入某个bean
    @Value("#{demoService}") 
    DemoService anotherBean;