zoukankan      html  css  js  c++  java
  • [Spring Boot] Complex Scope Scenarios of a Spring Bean

    We have the following example:

    @SpringBootApplication
    public class In28minutesScopeApplication {
    
        private static Logger LOGGER = LoggerFactory.getLogger(In28minutesScopeApplication.class);
    
        public static void main(String[] args) {
            // Application Context
            ApplicationContext applicationContext =
                    SpringApplication.run(In28minutesScopeApplication.class, args);
            PersonDAO personDAO = applicationContext.getBean(PersonDAO.class);
            PersonDAO personDAO1 = applicationContext.getBean(PersonDAO.class);
    
            LOGGER.info("{}", personDAO);
            LOGGER.info("{}", personDAO.getJdbcConnection());
    
            LOGGER.info("{}", personDAO1);
            LOGGER.info("{}", personDAO1.getJdbcConnection());
    
        }
    }
    @Component
    public class PersonDAO {
    
        @Autowired
        JDBCConnection jdbcConnection;
    
        public JDBCConnection getJdbcConnection() {
            return jdbcConnection;
        }
    
        public void setJdbcConnection(JDBCConnection jdbcConnection) {
            this.jdbcConnection = jdbcConnection;
        }
    }
    
    
    @Component
    public class JDBCConnection {
        public JDBCConnection () {
            System.out.println("JDBC Connection");
        }
    }

    The idea is to understand in different cases, how those instanse are created.

    Currently when running the application, we got:

    com.example.in28minutes.scope.PersonDAO@6c61a903
    com.example.in28minutes.scope.JDBCConnection@59d4cd39
    com.example.in28minutes.scope.PersonDAO@6c61a903 com.example.in28minutes.scope.JDBCConnection@59d4cd39

    Each class share the same instanse.

    What if we want that 'PersonDAO' using Singleton but JDBCConnection use 'Prototype'?

    @Component
    @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
    public class PersonDAO {
    
        @Autowired
        JDBCConnection jdbcConnection;
    
        public JDBCConnection getJdbcConnection() {
            return jdbcConnection;
        }
    
        public void setJdbcConnection(JDBCConnection jdbcConnection) {
            this.jdbcConnection = jdbcConnection;
        }
    }
    
    @Component
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public class JDBCConnection {
        public JDBCConnection () {
            System.out.println("JDBC Connection");
        }
    }

    In the end, we got the same result:

    com.example.in28minutes.scope.PersonDAO@14008db3
    com.example.in28minutes.scope.JDBCConnection@16aa8654
    com.example.in28minutes.scope.PersonDAO@14008db3
    com.example.in28minutes.scope.JDBCConnection@16aa8654

    If we really want JDBCConnection use a different instance, we can add Proxy to it:

    @Component
    @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.TARGET_CLASS)
    public class JDBCConnection {
        public JDBCConnection () {
            System.out.println("JDBC Connection");
        }
    }
  • 相关阅读:
    【08月20日】A股滚动市净率PB历史新低排名
    沪深300指数的跟踪基金最近1年收益排名
    基金前15大重仓股持仓股排名
    中证500指数的跟踪基金最近1年收益排名
    【08月14日】A股ROE最高排名
    【08月13日】预分红股息率最高排名
    【08月09日】北上资金持股比例排名
    最近一月研报推荐次数最多的最热股票
    JDK源码阅读-------自学笔记(二十一)(java.util.ArrayList详细版集合类)
    MyBatis-Plus 3.0.3 Sql注入器添加,即全局配置Sql注入器,sqlInjector改写
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10658915.html
Copyright © 2011-2022 走看看