Spring的注解开发-非自定义Bean的配置

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

非自定义Bean注解开发

  • 非自定义Bean不能象自定义Bean一样使用@Component注解及其衍生注解进行管理非自定义Bean要通过工厂的方式进行实例化使用@Bean标注即可@Bean的属性为beanName使用@Bean注解作用在方法中通过定义一个方法将非自定义bean的结果返回配合@Bean注解将其交给Spring容器管理。ps方法所在类必须交给Spring容器管理从而才能让Spring对类中的方法进行管理。
    • package com.example.Beans;
      
      import com.alibaba.druid.pool.DruidDataSource;
      import org.springframework.context.annotation.Bean;
      import org.springframework.stereotype.Component;
      
      import javax.sql.DataSource;
      
      @Component  // 将该类交给Spring容器管理
      public class otherBeans {
          @Bean("dataSource")  // 将非自定义的bean交给Spring容器管理同时设置bean的名称默认名称为方法名
          public DataSource dataSource() {
              DruidDataSource dataSource = new DruidDataSource();
              // 设置基本参数不进行设置简化一下
              return dataSource;
          }
      
      }
      

      非自定义的bean对象的名称默认为方法名如果不设置的话。

  • 非自定义bean需要注入参数时需要在方形参的位置使用注解来完成参数的注入

    • 使用@Autiwired根据类型自动进行Bean的匹配@Autowired可以省略

    • 使用@Qualifier根据名称进行Bean的匹配

    • 使用@Vaule根据名称进行普通数据类型的匹配

      借助上述例子中需要于数据建立连接注入对应的参数

    • package com.example.Beans;
      
      import com.alibaba.druid.pool.DruidDataSource;
      import com.example.DAO.UserDAO;
      import com.example.Service.UserService;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.beans.factory.annotation.Qualifier;
      import org.springframework.beans.factory.annotation.Value;
      import org.springframework.context.annotation.Bean;
      import org.springframework.stereotype.Component;
      
      import javax.sql.DataSource;
      
      @Component
      public class otherBeans {
          @Bean("dataSource")  // 将非自定义的bean交给Spring容器管理同时设置bean的名称默认名称为方法名
          public DataSource dataSource(@Value("${jdbc.driver}") String driverClassName/*普通属性*/,
                                       @Autowired UserDAO userDAO /*引用属性按照类型进行注入可以不使用注解*/,
                                       UserService userService,
                                       @Qualifier("userDAO2") UserDAO userDAO2/*根据bean的名称注入*/) {
              System.out.println("driverClassName:" + driverClassName);
              System.out.println("userDAO:" + userDAO);
              System.out.println("userService:" + userService);
              System.out.println("userDAO2:" + userDAO2);
              DruidDataSource dataSource = new DruidDataSource();
              // 设置基本参数
              dataSource.setDriverClassName(driverClassName);
              return dataSource;
          }
      
      }
      
    • ps为了达到属性自动注入的目的得配置好相关的属性并将其交给Spring容器管理

    • 上述代码形参方法中的参数都在Spring容器中有对应的bean对象

      • 具体如下

      • jdbc连接参数所属配置文件要交给application.xml配置文件管理

        • jdbc.driver=com.mysql.jdbc.Driver
          jdbc.url=jdbc:mysql://localhost:3306/db02
          jdbc.username=root
          jdbc.password=123456
        •  
      • 其它参数都是在往期文章中进行示例所创建的使用注解交给Spring容器管理即可。
    • 测试代码如下
      • package com.example.Test;
        
        
        import org.springframework.context.support.ClassPathXmlApplicationContext;
        public class TestApplicationContext {
            public static void main(String[] args) {
                ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
                System.out.println(context.getBean("dataSource"));
            }
        }
        
        
    • 运行结果如下
      •  
         
    • 注意Spring框架会自动扫描resource文件夹下的xml配置文件而不会自动扫描properties配置文件由此为了使properties配置文件生效需要将其配置在xml配置文件中。如何配置可自行搜索。
阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: Spring