java太难了!
2021-07-07 15:58
采纳率: 93.9%
浏览 93
已采纳

@Autowired 写在变量上取不到值,用在参数位置就能取到值 什么原因

jdbc.config

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;

@PropertySource("classpath:druid.properties")
public class JdbcConfig {

    @Value("${driverClassName}")
    private String driverClassName;
    @Value("${druid.url}")
    private String url;
    @Value("${druid.username}")
    private String username;
    @Value("${druid.password}")
    private String password;

    @Bean("dataSource")
    public DataSource getDataSource(){
        DruidDataSource ds = new DruidDataSource();

        ds.setDriverClassName(driverClassName);

        ds.setUrl(url);

        ds.setUsername(username);

        ds.setPassword(password);

        return ds;
    }
}


spring.config

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;

@Configuration
@ComponentScan("vx")
@Import({JdbcConfig.class,MybatisConfig.class})
public class SpringConfig {



}

mybatis.config 正常写法


mport org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import vx.service.Impl.MoneyServiceImpl;


import javax.sql.DataSource;

public class MybatisConfig {



    @Bean
    public SqlSessionFactoryBean getSqlSessionFactoryBean(@Autowired DataSource dataSource) {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();

        sqlSessionFactoryBean.setTypeAliasesPackage("vx.daomain");

        sqlSessionFactoryBean.setDataSource(dataSource);

        return sqlSessionFactoryBean;

    }

    

mybatis.config 错误写法


import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import vx.service.Impl.MoneyServiceImpl;


import javax.sql.DataSource;

public class MybatisConfig {

    @Autowired
    private DataSource dataSource;

    @Bean
    public SqlSessionFactoryBean getSqlSessionFactoryBean() {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();

        sqlSessionFactoryBean.setTypeAliasesPackage("vx.daomain");

        sqlSessionFactoryBean.setDataSource(dataSource);
        
        return sqlSessionFactoryBean;

    }

img

img

如上图 ,求指导

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • 得一以生 2021-07-08 09:12
    已采纳

    @Autowired
    private DataSource dataSource;
    换成
    @Resource
    private DataSource dataSource;试一下

    已采纳该答案
    打赏 评论
  • java通俗易懵 2021-07-07 17:47

     @Autowired 注解,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。好像没有看到你的DataSource dataSource配置在哪里。只看到getDataSource注册为bean

    打赏 评论
  • a1767028198 2021-07-07 18:04

    这种的话,十有八九是配置类加载先后的问题,
    MybatisConfig后于JdbcConfig的话,MybatisConfig可以加DependsOn注解,
    或者参考springboot mybatis的自动配置@AutoConfigureAfter({JdbcConfig.class})

    打赏 评论
  • 得一以生 2021-07-07 18:29

    @Autowired(required = false)或者
    @Autowired
    @Lazy

    打赏 评论

相关推荐 更多相似问题