Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 为什么要将@Bean添加到方法I';我会直接打电话的_Spring - Fatal编程技术网

Spring 为什么要将@Bean添加到方法I';我会直接打电话的

Spring 为什么要将@Bean添加到方法I';我会直接打电话的,spring,Spring,我通过@configuration和@Bean注释看到了很多关于Spring配置的例子。但我重新认识到,向直接调用以填充其他Bean的方法添加@Bean注释是一种常见的做法。例如: @Bean public Properties hibernateProperties() { Properties hibernateProp = new Properties(); hibernateProp.put("hibernate.dialect",

我通过@configuration和@Bean注释看到了很多关于Spring配置的例子。但我重新认识到,向直接调用以填充其他Bean的方法添加@Bean注释是一种常见的做法。例如:

@Bean
public Properties hibernateProperties() {
    Properties hibernateProp = new Properties();
    hibernateProp.put("hibernate.dialect",
            "org.hibernate.dialect.H2Dialect");
    hibernateProp.put("hibernate.hbm2ddl.auto", "create-drop");
    hibernateProp.put("hibernate.format_sql", true);
    hibernateProp.put("hibernate.use_sql_comments", true);
    hibernateProp.put("hibernate.show_sql", true);
    return hibernateProp;
}

@Bean public SessionFactory sessionFactory() {
    return new LocalSessionFactoryBuilder(dataSource())
            .scanPackages("com.ps.ents")
            .addProperties(hibernateProperties())
            .buildSessionFactory();}
因此,我想知道如果不使用@Bean注释而将hibernateProperties()声明为private是否更好

我想知道这是一种不好/不必要的普遍做法,还是背后有原因

提前谢谢

根据bean间依赖注入是一种很好的方法,可以用一种简单的形式定义bean依赖。当然,如果将您的
hibernateProperties()
定义为private,它将起作用,但无法通过Spring容器将其注入应用程序中的其他组件

只需根据bean依赖的类的数量以及是否需要重用它来调用其方法或将其注入其他类来决定。根据bean间依赖注入,以简单的形式定义bean依赖是一种很好的方法。当然,如果将您的
hibernateProperties()
定义为private,它将起作用,但无法通过Spring容器将其注入应用程序中的其他组件


只需根据bean所依赖的类的数量以及是否需要重用它来调用其方法或将其注入其他类来决定。

@bean
装饰
@Configuration
类中的方法意味着该方法的返回值将成为Springbean

默认情况下,这些bean是单实例(在应用程序的生命周期中只有一个实例)

在您的示例中,Spring知道
hibernateProperties()
是一个单例bean,并且只创建一个单例bean。因此,这里:

@Bean public SessionFactory sessionFactory() {
    return new LocalSessionFactoryBuilder(dataSource())
            .scanPackages("com.ps.ents")
            .addProperties(hibernateProperties())
            .buildSessionFactory();
}
hibernateProperties()方法将不再执行,但bean将从应用程序上下文中获取。如果不使用
@Bean
注释hibernateProperties(),它将是一个简单的java方法,并且在调用它时将执行它。这取决于你想要什么

需要指出的是,在
@Configuration
类中进行依赖项注入的另一种方法是添加一个参数。像这样:

@Bean public SessionFactory sessionFactory(Properties props) {
        return new LocalSessionFactoryBuilder(dataSource())
                .scanPackages("com.ps.ents")
                .addProperties(props)
                .buildSessionFactory();
    }

当Spring尝试创建sessionFactory()bean时,它将首先在应用程序上下文中查找类型为
Properties
的bean

@Bean
装饰
@Configuration
类中的方法意味着该方法的返回值将成为Springbean

默认情况下,这些bean是单实例(在应用程序的生命周期中只有一个实例)

在您的示例中,Spring知道
hibernateProperties()
是一个单例bean,并且只创建一个单例bean。因此,这里:

@Bean public SessionFactory sessionFactory() {
    return new LocalSessionFactoryBuilder(dataSource())
            .scanPackages("com.ps.ents")
            .addProperties(hibernateProperties())
            .buildSessionFactory();
}
hibernateProperties()方法将不再执行,但bean将从应用程序上下文中获取。如果不使用
@Bean
注释hibernateProperties(),它将是一个简单的java方法,并且在调用它时将执行它。这取决于你想要什么

需要指出的是,在
@Configuration
类中进行依赖项注入的另一种方法是添加一个参数。像这样:

@Bean public SessionFactory sessionFactory(Properties props) {
        return new LocalSessionFactoryBuilder(dataSource())
                .scanPackages("com.ps.ents")
                .addProperties(props)
                .buildSessionFactory();
    }

当Spring尝试创建sessionFactory()bean时,它将首先在应用程序上下文中查找类型为
Properties
的bean

谢谢!我知道了,谢谢!我知道了,谢谢!我知道了,谢谢!我得到了它。