美文网首页
4.4基于注解+配置类方式整合三层架构组件

4.4基于注解+配置类方式整合三层架构组件

作者: 大也 | 来源:发表于2023-12-01 19:07 被阅读0次

package com.atguigu.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.core.JdbcTemplate;

/**

  • 在 xml+ 注解 格式的基础上使用完全注解
  • 1.创建 config文件 和类 在类上添加 @Configuration
  • 2.添加@ComponentScan == <context:component-scan
  •   value              == {"com.atguigu"} base-package="com.atguigu"/>
    
  •   扫描包 可以不扫描配置类所以多个时 value = 可以不加"com.atguigu.config"
    
  • 3,添加 @PropertySource == <context:property-placeholder
  •    ("classpath:jdbc.properties") == location="classpath:jdbc.properties"/>
    
  • 4.使用 @Bean 注册第三方插件配置
  •   返回值就是你需要的DruidDataSource对象
    
  •   方法名 就是 <dean id == @Bean(name = "oneTest")
    
  •   可以直接定义形参 使用@Value 引导外部值 也可以定义成属性再赋值(当值需要全局使用时)
    
  • 5.@import 整合配置类 把配置类整合到第一个配置类
  •   @Import({ConfigA.class,ConfigB.class})
    
  • 6.注意点
  •   1.第三方插件 引用用其他@Bean(必须是其他@Bean方法) 方法时
    
  •      1.直接调用 方法 dataSource() 需要放入参数 不推荐
    
  •      2.可以直接把 方法形参放入 如DruidDataSource dataSource
    
  •             1)必须要有这个形参 1个
    
  •             2)多个 改名 注入 可以使用@Bean("oneTest") 改名
    
  •                然后注入 jdbcTemplate(DruidDataSource oneTest)
    
  •   2.周期 方法如何指定 1.可以项目内使用 @PostConstruct @PreDestroy注解 不推荐
    
  •                    2.@Bean(name = "oneTest",initMethod = "",destroyMethod = "")
    
  •   3.作用域 都可以加  @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    

*/
//@ComponentScan(value = {"com.atguigu.controller","com.atguigu.dao"
// ,"com.atguigu.pojo","com.atguigu.service"})
@ComponentScan("com.atguigu")
@PropertySource("classpath:jdbc.properties")
@Configuration
@Import({ConfigA.class,ConfigB.class})
public class JavaConfig {
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
// @Bean(name = "oneTest",initMethod = "",destroyMethod = "")
@Bean(name = "oneTest")
public DruidDataSource dataSource(
@Value("{atguigu.password}") String password, @Value("{atguigu.username}")
String username,
@Value("{atguigu.url}") String url, @Value("{atguigu.driver}")
String driverClassName
){
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setPassword(password);
druidDataSource.setUsername(username);
druidDataSource.setUrl(url);
druidDataSource.setDriverClassName(driverClassName);
return druidDataSource;
}

@Bean(name = "twoTest")
public DruidDataSource dataSource1(
        @Value("${atguigu.password}")
        String password,
        @Value("${atguigu.username}")
        String username,
        @Value("${atguigu.url}")
        String url,
        @Value("${atguigu.driver}")
        String driverClassName
){
    DruidDataSource druidDataSource = new DruidDataSource();
    druidDataSource.setPassword(password);
    druidDataSource.setUsername(username);
    druidDataSource.setUrl(url);
    druidDataSource.setDriverClassName(driverClassName);
    return druidDataSource;
}
/**
 *     警告: Exception encountered during context initialization -
 *     cancelling refresh attempt:
 *     org.springframework.beans.factory.UnsatisfiedDependencyException:
 *     Error creating bean with name 'jdbcTemplate' defined
 *     in com.atguigu.config.JavaConfig:
 *     Unsatisfied dependency expressed through method 'jdbcTemplate' parameter 0;
 *     nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException:
 *     No qualifying bean of type 'com.alibaba.druid.pool.DruidDataSource' available:
 *     expected single matching bean but found 2: oneTest,twoTest
 * */


@Bean

// public JdbcTemplate jdbcTemplate(DruidDataSource dataSource){
public JdbcTemplate jdbcTemplate(DruidDataSource twoTest){
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(twoTest);
// jdbcTemplate.setDataSource(dataSource());
return jdbcTemplate;
}
}

相关文章

网友评论

      本文标题:4.4基于注解+配置类方式整合三层架构组件

      本文链接:https://www.haomeiwen.com/subject/tapowdtx.html