package com.example.demospring1; import com.example.demospring1.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import java.lang.reflect.Field; /** * @description: * @author: 范子祺 **/ public class Test { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); UserService userService = (UserService)context.getBean("userService"); //测试2 依赖注入 userService.test(); //结果2 orderService seecom.example.demospring1.service.OrderService@1e67a849 //测试1 默认都是单例bean // UserService userService1 = (UserService)context.getBean("userService"); // UserService userService2 = (UserService)context.getBean("userService"); // System.out.println(userService); // System.out.println(userService1); // System.out.println(userService2); //结果1 // com.example.demospring1.service.UserService@1e67a849 // com.example.demospring1.service.UserService@1e67a849 // com.example.demospring1.service.UserService@1e67a849 // UserService userService3 = new UserService(); // context.getBeanFactory().registerSingleton("xxx",userService3); // UserService userService1 = new UserService(); // // for (Field field: userService1.getClass().getDeclaredFields()){ // if (field.isAnnotationPresent(Autowired.class)){ // field.set(userService1,??); // } // } } }
@ComponentScan("com.example.demospring1") public class AppConfig { } @Component public class UserService { @Autowired private OrderService orderService; public void test(){ System.out.println("orderService see"+orderService); } } @Component public class OrderService { }
1 依赖注入,2 单例bean,
userservice类->无参构造函数->对象->依赖注入->初始化->放入Map单例池->bean对象
希望使用类的时候,它里面的某个属性,已经从数据库中取到值了
userservice中增加
@PostConstruct public void a(){ System.out.println("初始化前"); }
结果
14:24:27.525 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'userService'
初始化前
orderService seecom.example.demospring1.service.OrderService@75f9eccc
继续增加
public class UserService implements InitializingBean { @PostConstruct public void a(){ System.out.println("初始化前-PostConstruct"); } @Override public void afterPropertiesSet() throws Exception { System.out.println("初始化前-afterPropertiesSet"); } 结果 14:28:19.050 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'userService' 初始化前-PostConstruct 初始化前-afterPropertiesSet orderService see:com.example.demospring1.service.OrderService@5d47c63f
构造函数的证明
类UserService 增加 public UserService() { System.out.println("空参构造函数:"+orderService); } public UserService(OrderService orderService) { this.orderService = orderService; System.out.println("1个入参orderService构造函数:"+orderService); } 结果 空参构造函数:null 初始化前-PostConstruct 初始化前-afterPropertiesSet orderService see:com.example.demospring1.service.OrderService@5d47c63f
自动选择了无参构造函数
OrderService 增加 public OrderService() { System.out.println("空参OrderService"+this); } UserService 修改 // public UserService() { // System.out.println("空参构造函数:"+orderService); // } public UserService(OrderService orderService) { this.orderService = orderService; System.out.println("1个入参的UserService构造函数:"+orderService); } 结果 17:49:18.193 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'orderService' 空参OrderServicecom.example.demospring1.service.OrderService@1445d7f 17:49:18.198 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'userService' 17:49:18.365 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Autowiring by type from bean name 'userService' via
constructor to bean named 'orderService' 1个入参的UserService构造函数:com.example.demospring1.service.OrderService@1445d7f 初始化前-PostConstruct 初始化前-afterPropertiesSet orderService see:com.example.demospring1.service.OrderService@1445d7f
1 依赖注入, 2 单例bean
UserService增加 public UserService(OrderService orderService1,OrderService orderService2) { this.orderService = orderService; System.out.println("2个入参的UserService构造函数:"+orderService); System.out.println("2个入参的UserService构造函数h2:"+orderService1+"="+orderService2); } 结果 17:53:44.235 [main] WARN org.springframework.context.annotation.AnnotationConfigApplicationContext -
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.
BeanCreationException: Error creating bean with name 'userService' defined in file [/Users/shenshaonian/Desktop/selfPro/spr
ing/demoSpring1/target/classes/com/example/demospring1/service/UserService.class]: Instantiation of bean failed; nested exc
eption is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.demospring1.service.U
serService]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.example.demospring1.service.UserService.<init>()
多个有参的构造函数,无法判断使用哪个构造函数创建对象
UserService 修改 // public UserService(OrderService orderService) { // this.orderService = orderService; // System.out.println("1个入参的UserService构造函数:"+orderService); // } public UserService(OrderService orderService1,OrderService orderService2) { this.orderService = orderService; System.out.println("2个入参的UserService构造函数:"+orderService); System.out.println("2个入参的UserService构造函数h2:"+orderService1+"="+orderService2); } public void test(){ orderService.orderServicePrint(); System.out.println("userservice look orderService see:"+orderService+"=="); } OrderService 增加 public void orderServicePrint(){ System.out.println("orderServicePrint look"); } 结果空参 OrderServicecom.example.demospring1.service.OrderService@1445d7f 18:01:15.133 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'userService' 18:01:15.236 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Autowiring by type from bean name 'userService' via constructor to bean named 'orderService' 2个入参的UserService构造函数:null 2个入参的UserService构造函数h2:com.example.demospring1.service.OrderService@1445d7f=com.example.demospring1.service.OrderService@1445d7f 初始化前-PostConstruct 初始化前-afterPropertiesSet orderServicePrint look userservice look orderService see:com.example.demospring1.service.OrderService@1445d7f==
1 autowire注释的bean一般会在对象使用的时候才会实例化,2 对象放到构造函数的参数中也是一种提前的实例化, 最终orderservice的地址都是
1445d7f, 这里面是通过cglib代理实现的, cglib是啥, asm是啥, 这里我有点搞不清了。 可能有错误的地方。
标签:demospring1,OrderService,orderService,UserService,spring1,小结,public,构造函数 From: https://www.cnblogs.com/woainixxx/p/16983701.html