spring @configuration 注解中 proxyBeanMethods = true的作用二

现在有个问题如果我将@Autowired 去掉会怎么样?
config :proxyBeanMethods = false

package org.demo.config;

import org.demo.service.MyService;
import org.demo.service.MyService1;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import javax.annotation.Resource;

@ComponentScan("org.demo")
@Configuration(proxyBeanMethods = false)
@DependsOn(value ="staticProperties")
public class MyConfig {
    @Resource
    StaticProperties staticProperties;
    @Bean
    public MyService myService(){
        MyService myService = new MyService();
        myService.setHeight(staticProperties.getHeight());
        myService.setName(staticProperties.getName());
        System.out.println("config:myservice:"+myService.toString());
        return myService;
    }
    @Bean
    public MyService1 myService1(){
        MyService myService =myService();
        System.out.println("config:myservice1:"+myService.toString());
        MyService1 myService1 = new MyService1(myService);
        System.out.println("config:myservice1:"+myService1.toString());

        return myService1;

    }
}


去掉注解@Autowired

package org.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

//@Service
public class MyService {
    //@Autowired
    private List myServiceInterfaceList;
    private String name;
    private Long height;
    //@Autowired
    public MyService1 myService1;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getHeight() {
        return height;
    }

    public void setHeight(Long height) {
        this.height = height;
    }

    public MyService() {
    }
    //@PostConstruct
    public void send(){
        System.out.println("myServicePostConstruct");
    }


    public void toList(){
        myServiceInterfaceList.stream().forEach(myServiceInterface -> System.out.println(myServiceInterface.getClass()));
    }


    public MyService(MyService1 myService1) {
        this.myService1 = myService1;
    }
}


/******************************************************************/
package org.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

@Order(11)
//@Service
public class MyService1  implements MyServiceInterface{
    //@Autowired
    public MyService myService;
    //@PostConstruct
    public void init(){
        System.out.println("postConstruce:myService1");
    }

    public MyService1(MyService myService) {
        this.myService = myService;
    }

    public MyService1() {
    }
}


/******************************************************************/
package org.demo;

import org.demo.config.MyConfig;
import org.demo.service.MyService;
import org.demo.service.MyService1;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Application {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
        annotationConfigApplicationContext.register(MyConfig.class);
        annotationConfigApplicationContext.refresh();

        MyService myService = annotationConfigApplicationContext.getBean(MyService.class);
        System.out.println("mian:"+myService);
        MyService1 myService1 = annotationConfigApplicationContext.getBean(MyService1.class);
        System.out.println("mian:"+myService1.myService);
        System.out.println("mian:myService1:"+myService1.myService.myService1);


    }
}

输出日志

config:myservice:org.demo.service.MyService@543c6f6d
config:myservice:org.demo.service.MyService@18bf3d14
config:myservice1:org.demo.service.MyService@18bf3d14
config:myservice1:org.demo.service.MyService1@4fb64261
mian:org.demo.service.MyService@543c6f6d
mian:org.demo.service.MyService@18bf3d14
mian:myService1:null

config :proxyBeanMethods = true
输出日志

config:myservice:org.demo.service.MyService@4d826d77
config:myservice1:org.demo.service.MyService@4d826d77
config:myservice1:org.demo.service.MyService1@36f0f1be
mian:org.demo.service.MyService@4d826d77
mian:org.demo.service.MyService@4d826d77
mian:myService1:org.demo.service.MyService1@36f0f1be

对比发现:当proxyBeanMethods = true 的时候我调用Service1.service.service1的时候是有对象的;但是为proxyBeanMethods = false 的时候发现Service1.service.service1 是null 这是为啥呢。首先service1.service 这个对象是我手动new 出来的,手动new 出来的有个弊端是什么呢,那就是你new的对象里的属性对象如果你没有手动的初始化那么是这里的属性就是空的,spring 是不会帮你注入的。

你可能感兴趣的:(spring @configuration 注解中 proxyBeanMethods = true的作用二)