Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"ListableBeanFactory.getBeansOfType" will trigger some beans to be initialized before the configuration from Nacos is loaded. #72

Closed
hxy1991 opened this issue Dec 12, 2018 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@hxy1991
Copy link
Contributor

hxy1991 commented Dec 12, 2018

For example, when using both Druid and Mybatis, the mapper class is initialized during nacos-spring-context loading.

nacos.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:nacos="http://nacos.io/schema/nacos"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://nacos.io/schema/nacos
       http://nacos.io/schema/nacos.xsd">

    <nacos:annotation-driven/>

    <nacos:global-properties server-addr="127.0.0.1:8848" />

    <nacos:property-source data-id="jdbc.properties"/>
</beans>

data-source.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          init-method="init" destroy-method="close">
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="initialSize" value="1"/>
        <property name="maxActive" value="20"/>
    </bean>

    <bean id="txManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <tx:annotation-driven transaction-manager="txManager"/>
</beans>

spring-config-mybatis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean class="org.mybatis.spring.SqlSessionFactoryBean"
          id="sqlSessionFactory">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperFactoryBean"
          id="userMapper">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
        <property name="mapperInterface" value="com.alibaba.nacos.example.spring.dao.UserMapper"/>
    </bean>
</beans>

UserMapper

import com.alibaba.nacos.example.spring.model.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

public interface UserMapper {

    @Select(value = "select id, name from user where id = #{id}")
    User findById(@Param("id") long id);
}
@hxy1991 hxy1991 added the bug Something isn't working label Dec 12, 2018
@hxy1991 hxy1991 self-assigned this Dec 12, 2018
@hxy1991 hxy1991 mentioned this issue Dec 12, 2018
hxy1991 added a commit to hxy1991/nacos-spring-project that referenced this issue Dec 12, 2018
mercyblitz added a commit that referenced this issue Dec 12, 2018
@annpeter
Copy link

我在一个普通的spring项目中跑通了acm的对接流程,当我把项目迁移到我们正式的项目中(一个SpringBoot的项目,里面引用了SpringBatch),项目启动的时候报错了。我跟了一下源代码,在nacos-spring-context-0.2.2-RC1.jar的AnnotationListenerMethodProcessor的98行中applicationContext.getBeansOfType(Object.class)导致了spring-beans-5.0.7.RELEASE.jar中DefaultListableBeanFactory515行初始了所有的对象。

这边强制初始所有对象是有问题的,在SpringBatch中,有作用域的概念,对于StepScope类型的对象,并非单例对象,是在运行过程中初始化的,不能在项目启动的时候进行初始化

@jackhbr
Copy link

jackhbr commented Nov 20, 2019

我在一个普通的spring项目中跑通了acm的对接流程,当我把项目迁移到我们正式的项目中(一个SpringBoot的项目,里面引用了SpringBatch),项目启动的时候报错了。我跟了一下源代码,在nacos-spring-context-0.2.2-RC1.jar的AnnotationListenerMethodProcessor的98行中applicationContext.getBeansOfType(Object.class)导致了spring-beans-5.0.7.RELEASE.jar中DefaultListableBeanFactory515行初始了所有的对象。

这边强制初始所有对象是有问题的,在SpringBatch中,有作用域的概念,对于StepScope类型的对象,并非单例对象,是在运行过程中初始化的,不能在项目启动的时候进行初始化

那大哥,这个问题咋解决啊?我现在的项目也遇到了这个问题,nacos迁移不到springbatch上,对这里的原理不是很熟悉,求大佬赐教啊!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants