是否可以将@Bean注释也应用于这些函数?

时间:2019-06-25 10:03:20

标签: java spring

我是Spring Java的新手,对在何处使用@Bean注释有些困惑。 例如,请参见以下代码:

@Configuration
@EnableTransactionManagement
public class HibernateConfig {

    @Autowired
    private Environment env;

    private final Properties hibernateProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
        properties.put("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
        properties.put("hibernate.format_sql", env.getProperty("hibernate.format_sql"));
        properties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
        properties.put("hibernate.max_fetch_depth", env.getProperty("hibernate.max_fetch_depth"));
        properties.put("hibernate.cache.use_second_level_cache",env.getProperty("hibernate.cache.use_second_level_cache"));
        properties.put("hibernate.cache.use_minimal_puts", env.getProperty("hibernate.cache.use_minimal_puts"));
        properties.put("hibernate.connection.release_mode", env.getProperty("hibernate.connection.release_mode"));
        properties.put("hibernate.cache.use_query_cache",env.getProperty("hibernate.cache.use_query_cache"));
        return properties;
    }

    @Bean
    public DataSource dataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
        dataSource.setUrl(env.getProperty("spring.datasource.url"));
        dataSource.setUsername(env.getProperty("spring.datasource.username"));
        dataSource.setPassword(env.getProperty("spring.datasource.password"));

        return dataSource;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean lcemfb = new LocalContainerEntityManagerFactoryBean();
        lcemfb.setJpaVendorAdapter(getJpaVendorAdapter());
        lcemfb.setDataSource(dataSource());
        lcemfb.setPersistenceUnitName("entityManagerFactory");
        lcemfb.setPackagesToScan("com.sha.microservicecoursemanagement.model");
        lcemfb.setJpaProperties(hibernateProperties());
        return lcemfb;
    }


    @Bean
    public PlatformTransactionManager transactionManager() {
        return new JpaTransactionManager(entityManagerFactory().getObject());
    }

    @Bean
    public JpaVendorAdapter getJpaVendorAdapter() {
        JpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
        return adapter;
    }

}

现在在上面的代码dataSourceLocalContainerEntityManagerFactoryBeanPlatformTransactionManagerJpaVendorAdapter中,这些函数的属性为@Bean。 现在,我在互联网上读到的@Bean只能提供给类类型,而不能提供给函数。 有人可以消除我的困惑吗?

2 个答案:

答案 0 :(得分:2)

如果类用@Bean注释,则@Configuration可以应用于方法。 Refer this link

下面的示例中,我正在使用@Bean

@Configuration
public class ClientDataSourceConfig {

    @Value("${spring.datasource.driver}")
    private String driver;

    @Value("${spring.datasource.url}")
    private String url;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    public ClientDataSourceRouter dataSource;

    @Bean(name = "getDataSource")
    public ClientDataSourceRouter getDataSource() throws Exception {
        dataSource = new ClientDataSourceRouter();
        dataSource.init(driver, url, username, password);
        return dataSource;
    }
}

答案 1 :(得分:2)

@Bean只能赋予Class类型。在函数中,您可以看到您正在返回类实例,例如DataSource,LocalContainerEntityManagerFactoryBean等。您没有在函数中添加@Bean public DataSource dataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name")); dataSource.setUrl(env.getProperty("spring.datasource.url")); dataSource.setUsername(env.getProperty("spring.datasource.username")); dataSource.setPassword(env.getProperty("spring.datasource.password")); return dataSource; } 批注。您正在做的是,告诉Spring从该类返回一个bean。例如

@Autowired DataSource dataSource;

在这里,您告诉Spring返回一个DataSource对象。除非您有@Bean annocation,否则您无法自动装配数据源对象。因此@Bean允许您使用{{1}}。这些bean由Spring IoC容器管理。