通过javaconfig配置bean,自动依赖内部bean,为什么会生成两次

阳阳 · 2022-06-13 13:51
被依赖的bean不是应该只用生成一次,然后到处注入吗 代码: @Bean(initMethod = "",destroyMethod = "",name = {"druidDataSource","dataSource"}) public DruidDataSource dataSource(Role role){ System.out.println("-----------dataSource start------------------"); DruidDataSource dataSource = new DruidDataSource(); dataSource.setName(name); dataSource.setPassword(password); dataSource.setUrl(url); dataSource.setDriverClassName(driverClassName); System.out.println(role); System.out.println(user3()); System.out.println("-----------dataSource end------------------"); return dataSource; } @Bean public User user3(){ System.out.println("@Bean 加载user3"); User user = new User(); System.out.println(user); return user; } 控制台输出: -----------dataSource start------------------ cn.zhouyang.beans.Role@c33b74f @Bean 加载user3 构造器 user 已加载 cn.zhouyang.beans.User@130161f7 cn.zhouyang.beans.User@130161f7 -----------dataSource end------------------ @Bean 加载user3 构造器 user 已加载 cn.zhouyan
0 个回答
暂无回答