Article From:https://segmentfault.com/q/1010000009911086
Question:

I seem to have configured a data source. Why do I hint at multiple injection?

package com.example.demo.config;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:application.properties")
public class DataSourceConfiguration {
    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;
    @Value("${jdbc.maxActive}")
    private int maxActive;
    @Value("${jdbc.maxIdel}")
    private int maxIdel;
    @Value("${jdbc.maxWait}")
    private long maxWait;

    @Bean
    //It should be this one.Public BasicDataSource dataSource () {BasicDataSource dataSource = new BasicDataSOurce ();DataSource.setDriverClassName (driver);DataSource.setUrl (URL);DataSource.setUsername (username);DataSource.setPassword (password);DataSource.setMaxActIve (maxActive);DataSource.setMaxIdle (maxIdel);DataSource.setMaxWait (maxWait);DataSource.setValidationQuery ("SELECT 1");DataSource.setTestOnBorrow (true);RetUrn dataSource;}}
package com.example.demo.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

import javax.annotation.Resource;
import javax.annotation.Resources;
import javax.sql.DataSource;


@Configuration
@EnableTransactionManagement
public class MyBatisConfig implements TransactionManagementConfigurer {

    @Autowired
    //How can we not use @Autowired here? It is possible to use Resource.Private DataSource dataSource;@OverridePublic PlatforMTransactionManager annotationDrivenTransactionManager () {Return new DataSourceTransactionMAnager (dataSource);}@Bean (name = "sqlSessionFactory")Public SqlSessionFactory sqlSesSionFactoryBean () {SqlSessionFactoryBean bean = new SqlSessionFactoryBean ();Bean.sEtDataSource (dataSource);Try {Return bean.getObject ();} catch (Excep)Tion E) {E.printStackTrace ();Throw new RuntimeException (E);}}@BeanPublic SqlSessionTemplate sqlSessionTemplate (SqlSessionFactory sqlSessionFactory) {Return new SqlSessionTemplate (sqlSessionFactory);}}

Answer 0:

Run it and read the wrong information.

Answer 1:

I also encountered such problems.

Leave a Reply

Your email address will not be published. Required fields are marked *