Spring-注解开发管理第三方Bean

管理第三方Bean

1、创建一个SpringConfig配置类。添加注解@Configuration

2、定义一个方法获得要管理的对象

3、添加@Bean,表示当前方法的返回值是一个bean

@Configuration
public class SpringConfig {

    //1、定义一个方法获得要管理的对象
    //2、添加@Bean,表示当前方法的返回值是一个bean
    @Bean
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("123");
        dataSource.setUrl("123");
        dataSource.setUsername("123");
        dataSource.setPassword("123");
        return  dataSource;
    }
}

 4、打印获取到的对象

public class App {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
        DataSource dataSource = ctx.getBean(DataSource.class);
        System.out.println(dataSource);
    }
}

 5、打印结果

{
    CreateTime:"2022-07-20 13:41:44",
    ActiveCount:0,
    PoolingCount:0,
    CreateCount:0,
    DestroyCount:0,
    CloseCount:0,
    ConnectCount:0,
    Connections:[
    ]
}

6、或者创建一个JdbcConfig配置类,

  • 在其中定义一个方法获得要管理的对象,添加@Bean
  • 在SpringConfig中使用注解@Import(JdbcConfig.class)

第三方Bean注入资源

简单类型

使用@Value注入

@Configuration
@PropertySource("classpath:jdbc.properties")
@Import(JdbcConfig.class)
public class SpringConfig {

}






public class JdbcConfig {

    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    @Bean
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        System.out.println(driver);
        System.out.println(url);
        System.out.println(username);
        System.out.println(password);
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return  dataSource;
    }
}

引用类型

根据类型自动装配

注意点:SpringConfig要使用注解@ComponentScan 扫描定义Bean的包

public class JdbcConfig {

    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    @Bean
    public DataSource dataSource(BookDao bookDao){
        System.out.println(bookDao);
        DruidDataSource dataSource = new DruidDataSource();
        System.out.println(driver);
        System.out.println(url);
        System.out.println(username);
        System.out.println(password);
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return  dataSource;
    }
}

@Repository
public class BookDaoImpl implements BookDao {

    public void save() {
        System.out.println("book dao save...");
    }
    
}


@Configuration
@PropertySource("classpath:jdbc.properties")
@ComponentScan("com.hyk.dao")
@Import(JdbcConfig.class)
public class SpringConfig {

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值