首页 > 其他分享 >spring1-小结

spring1-小结

时间:2022-12-14 21:56:44浏览次数:39  
标签:demospring1 OrderService orderService UserService spring1 小结 public 构造函数

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

相关文章

  • android 加载图片oom若干方案小结
    本文根据网上提供的一些技术方案加上自己实际开发中遇到的情况小结。众所周知,每个Android应用程序在运行时都有一定的内存限制,限制大小一般为16MB或24MB(视手机而定)。一般......
  • js中Math.floor、Math.ceil、Math.round和parseInt小数取整小结
    虽然知道结果都可以返回一个整数,但是四者的区别尤其是关于-0.5的取整情况貌似还是需要注意一下一、Math.floor(向下取整)作用:返回小于等于参数的最大整数。eg:Math.floor(5......
  • 2008 小结,2009计划
      2008总结下完成了几件重要的事情  1:SAP开发上自己做了总结,应付工作上2次开发已不成问题.(比去年不知道ERP,SAP为何物时强了)  2:今年5月考过了中级,  3:......
  • Elasticsearch Head插件使用小结
    作者:崔雄华1ElasticsearchHead是什么ElasticSearchhead就是一款能连接ElasticSearch搜索引擎,并提供可视化的操作页面对ElasticSearch搜索引擎进行各种设置和数据检索......
  • Elasticsearch Head插件使用小结
    作者:崔雄华1ElasticsearchHead是什么ElasticSearchhead就是一款能连接ElasticSearch搜索引擎,并提供可视化的操作页面对ElasticSearch搜索引擎进行各种设置和数据检索功能......
  • 12.5-12.9周末小结
    目录JavaScript网页脚本语言§ECMAScript一、基础1.注释语法2.结束符号3.在html内引入JS的方式二、变量与常量1.声明变量的关键字三、JS中的基本数据类型1.数值类型Number......
  • c++构建工具之cmake使用小结
    0.前言使用cmake的过程先是要编写一个cmakelists.txt的文本,然后使用cmake命令生成对应平台的工程。在windows下命令行或者使用cmakegui工具,生成v......
  • python爬虫小结1
    python爬虫小结11正则匹配中注意的:importrea='<div>指数</div>'word=re.findall('<div>(.*?)</div>',a)print(word)其中(.*?)是能匹配基本所有的字符,但是对于跨行的......
  • linux 下的sar工具命令小结
    1安装  tarzxvf xxx.tar.gz  ./configure  make  makeinstall 2 使用pidstat 2 5 //每隔2秒,显示5次,所有活动进程的CPU使用情况pidstat -p......
  • struts 2+hibernate 3+spring基本套路小结
    任何东西都是有一定的套路和规律的,struts2+hibernate3+spring也一样,struts2比struts1爽很多了,但目前唯一觉得不爽的是struts2居然没什么好的IDE工具去支持设计之,比......