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


    管理第三方Bean

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

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

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

    1. @Configuration
    2. public class SpringConfig {
    3. //1、定义一个方法获得要管理的对象
    4. //2、添加@Bean,表示当前方法的返回值是一个bean
    5. @Bean
    6. public DataSource dataSource(){
    7. DruidDataSource dataSource = new DruidDataSource();
    8. dataSource.setDriverClassName("123");
    9. dataSource.setUrl("123");
    10. dataSource.setUsername("123");
    11. dataSource.setPassword("123");
    12. return dataSource;
    13. }
    14. }

     4、打印获取到的对象

    1. public class App {
    2. public static void main(String[] args) {
    3. AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
    4. DataSource dataSource = ctx.getBean(DataSource.class);
    5. System.out.println(dataSource);
    6. }
    7. }

     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注入

    1. @Configuration
    2. @PropertySource("classpath:jdbc.properties")
    3. @Import(JdbcConfig.class)
    4. public class SpringConfig {
    5. }
    6. public class JdbcConfig {
    7. @Value("${jdbc.driver}")
    8. private String driver;
    9. @Value("${jdbc.url}")
    10. private String url;
    11. @Value("${jdbc.username}")
    12. private String username;
    13. @Value("${jdbc.password}")
    14. private String password;
    15. @Bean
    16. public DataSource dataSource(){
    17. DruidDataSource dataSource = new DruidDataSource();
    18. System.out.println(driver);
    19. System.out.println(url);
    20. System.out.println(username);
    21. System.out.println(password);
    22. dataSource.setDriverClassName(driver);
    23. dataSource.setUrl(url);
    24. dataSource.setUsername(username);
    25. dataSource.setPassword(password);
    26. return dataSource;
    27. }
    28. }

    引用类型

    根据类型自动装配

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

    1. public class JdbcConfig {
    2. @Value("${jdbc.driver}")
    3. private String driver;
    4. @Value("${jdbc.url}")
    5. private String url;
    6. @Value("${jdbc.username}")
    7. private String username;
    8. @Value("${jdbc.password}")
    9. private String password;
    10. @Bean
    11. public DataSource dataSource(BookDao bookDao){
    12. System.out.println(bookDao);
    13. DruidDataSource dataSource = new DruidDataSource();
    14. System.out.println(driver);
    15. System.out.println(url);
    16. System.out.println(username);
    17. System.out.println(password);
    18. dataSource.setDriverClassName(driver);
    19. dataSource.setUrl(url);
    20. dataSource.setUsername(username);
    21. dataSource.setPassword(password);
    22. return dataSource;
    23. }
    24. }
    25. @Repository
    26. public class BookDaoImpl implements BookDao {
    27. public void save() {
    28. System.out.println("book dao save...");
    29. }
    30. }
    31. @Configuration
    32. @PropertySource("classpath:jdbc.properties")
    33. @ComponentScan("com.hyk.dao")
    34. @Import(JdbcConfig.class)
    35. public class SpringConfig {
    36. }

  • 相关阅读:
    第7 部分 HDLC 和PPP
    pip使用豆瓣镜像源
    计算机毕业设计ssm高校就业管理系统157v3系统+程序+源码+lw+远程部署
    jetson nano补充:根目录/usr刷机扩容 瘦身
    学习笔记-角色权限
    Android原生实现控件选择背景变色方案(API28及以上)
    LeetCode 75 - 01 : 最小面积矩形
    全面解析内存泄漏检测与修复技术
    数据库MySQL(四):表中字段约束和外键约束
    Cookie、Session和Token三者区别以及各自应用场景
  • 原文地址:https://blog.csdn.net/u011572366/article/details/125890390