我在春季自动装配Bean时遇到了这种奇怪的情况。首先,我声明这个bean;<beans:bean id="customerInfo" class="my.web.app.com.CustomerInfoSession" scope="session" > <aop:scoped-proxy /></beans:bean>当我在customerInfo中设置值时,有两种情况:首先,我这样设置:SqlRowSet srs =jdbcTemplate.queryForRowSet(query, qparams); if (srs.isBeforeFirst()==true) { while (srs.next()) { customerInfo.setLoginId(srs.getString("LOGINID")); customerInfo.setCompanyId(srs.getString("COMPANYID")); } }System.out.println("Instance : "+customerInfo);//for first pointing check然后我通过@Autowired bean检查另一个类中的Autowiring指针;在测试课程中:@Controllerpublic class Test {@Autowiredprivate CustomerInfoSession customerInfo;public void checkObject(){System.out.println("Call back : "+customerInfo);//for second pointing check}}结果:  实例:my.web.app.com.CustomerInfoSession@1e7c92cc    致电:my.web.app.com.CustomerInfoSession@1e7c92cc如我们所见,@ Autowiring正在调用应有的同一个bean实例,但是当我更改为这样设置值时出现了问题:customerInfo = (CustomerInfoSession) jdbcTemplate.queryForObject(query,qparam,new BeanPropertyRowMapper<>(CustomerInfoSession.class));System.out.println("Instance : "+customerInfo);//for first pointing check通过使用相同的Test类,结果为:  实例:my.web.app.com.CustomerInfoSession@2d700bd6    致电:my.web.app.com.CustomerInfoSession@5e33e39c如我们所见,@Autowired没有指向同一个实例...为什么使用不同的jdbc模板会影响@Autowired会话作用域bean?为什么bean没有像应该那样指向同一实例? 最佳答案 在第一种情况下,您将设置Spring注入的对象的属性。但是在下一种情况下,jdbcTemplate正在创建CustomerInfoSession对象的新实例,您已将customerInfo对象ref指向此新创建的对象。以下声明  customerInfo =(CustomerInfoSession)jdbcTemplate.queryForObject(query,qparam,new BeanPropertyRowMapper (CustomerInfoSession.class));实际上等效于  CustomerInfoSession temp =(CustomerInfoSession)  jdbcTemplate.queryForObject(query,qparam,                新的BeanPropertyRowMapper (CustomerInfoSession.class));    customerInfo = temp;为了可视化(单击下面的图像以更好地查看它),情况一:情况2:
08-07 05:51