zoukankan      html  css  js  c++  java
  • 07-XML 文件注解开发

    不论是 xml 开发或者注解开发都有一个问题是,我们还是不得不写 bean.xml 文件,这次解决这个问题

    创建 spring06 项目

    将上一个项目导入

    注解

    1. @Configuration

      • 作用:指定当前类是一个配置类,它的作用和 bean.xml 是一样的
      • 细节:当配置类作为 AnnotationConfigApplicationContext 对象创建的参数时,该注解可以不写
    2. @ComponentScan

      • 作用:用于通过注解指定 spring 在创建容器时要扫描的包。

      • 属性

        • value:
          • 她和 basePackages 的作用是一样的,都是用于指定创建容器要扫描的包。
          • 我们使用此注解就等同于在 xml 中配置了:
        <context:component-scan base-package="com"/>
        

        源码中,参数有value、basePackages,上面的注解表示,这两个相等

    3. @bean

      • 作用:
        • 用于把当前方法的返回值作为 bean 对象存入 spring 的 ioc 容器中
      • 属性:
        • name:
          • 用于指定 bean 的 id,当不写时,默认值是当前方法的名称
      • 细节:
        • 当我们使用注解配置方法时,如果方法有参数,spring 框架会去容器中查找有没有可用的 bean 对象。
        • 查找的方式和 Autowired 的注解作用是一样的
    4. @import

      • 作用:用于导入其他配置类
      • 属性:
        • value:用于指定其他配置类的字节码
        • 当我们使用Import 的注解之后,有 Import 注解的类就父类配置类,而导入的都是子类配置类
    5. @PropertySource

      • 作用:用于指定properties文件的位置
      • 属性:
        • value:指定文件的名称和路径
          • 关键字:classpath,表示类路劲下

    一、@Configuration

    作用:指定当前类是一个配置类

    细节:当配置类作为 AnnotationConfigApplicationContext 对象创建的参数时,该注解可以不写

    何时可以不写?

    在测试类中注入可不写

    new AnnotationConfigApplicationContext(SpringConfiguration.class);
    

    何时必须写?

    如果我们把里面的配置信息转移到 JdbcConfig 类中,JdbcConfig 类还在扫描的包中

    SpringConfiguration 里面内容为空,JdbcConfig 类就必须写@Configuration,或者也实行注入,实行注入的话,SpringConfiguration 与 JdbcConfig 同级

    ac=new AnnotationConfigApplicationContext(SpringConfiguration.class, JdbcConfig.class);
    

    @Import

    使用 Import 标签,可以不用写@Configuration 与注入

    @Configuration
    @Import(JdbcConfig.class)
    @ComponentScan(basePackages = {"com"})
    public class SpringConfiguration {
    

    二、@ComponentScan

    作用:用于通过注解指定 spring 在创建容器时要扫描的包。

    属性

    ​ value:她和 basePackages 的作用是一样的,都是用于指定创建容器要扫描的包。

    ​ 我们使用此注解就等同于在 xml 中配置了:

    <context:component-scan base-package="com"/>
    

    源码中,参数有value、basePackages,上面的注解表示,这两个相等

    而且这两个属性是数组

    	@AliasFor("basePackages")
    	String[] value() default {};
    
    	@AliasFor("value")
    	String[] basePackages() default {};
    

    当下面代码出场了,那么 bean.xml 中 <context:component-scan base-package="com"/> 可以被拿掉了

    package com.config;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @ComponentScan(basePackages = {"com"})
    public class SpringConfiguration {
    
    }
    
    

    三、@bean

        /**
         * 创建数据源对象
         * @return
         */
        @Bean(name = "dataSource")
        public DataSource createDataSource(){
            ComboPooledDataSource ds=new ComboPooledDataSource();
            try {
                ds.setDriverClass("com.mysql.cj.jdbc.Driver");
                ds.setJdbcUrl("jdbc:mysql://localhost:3306/test");
                ds.setUser("root");
                ds.setPassword("root");
            }catch (PropertyVetoException e){
                e.printStackTrace();
            }
            return ds;
        }
    

    细节:

    • 当我们使用注解配置方法时,如果方法有参数,spring 框架会去容器中查找有没有可用的 bean 对象。
    • 查找的方式和 Autowired 的注解作用是一样的,

    1.Config完整代码

    package com.config;
    
    import com.mchange.v2.c3p0.ComboPooledDataSource;
    import org.apache.commons.dbutils.QueryRunner;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    
    import javax.sql.DataSource;
    import java.beans.PropertyVetoException;
    
    /**
     * 描述:
     * 〈该类是一个配置类,他的作用和 bean.xml 是一样的〉
     *
     * @author zuiren
     * @create 2019/8/28
     * @since 1.0.0
     */
    @Configuration
    @ComponentScan(basePackages = {"com"})
    public class SpringConfiguration {
        /**
         * 用于创建一个 QueryRunner 对象
         * @param dataSource
         * @return
         */
        @Bean(name = "runner")
        //设置为多例对象
        @Scope(value = "prototype")
        public QueryRunner createQueryRunner(DataSource dataSource){
            return new QueryRunner(dataSource);
        }
    
        /**
         * 创建数据源对象
         * @return
         */
        @Bean(name = "dataSource")
        public DataSource createDataSource(){
            ComboPooledDataSource ds=new ComboPooledDataSource();
            try {
                ds.setDriverClass("com.mysql.cj.jdbc.Driver");
                //?useSSL=false&serverTimezone=GMT 我这里不加会出错
                ds.setJdbcUrl("jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=GMT");
                ds.setUser("root");
                ds.setPassword("root");
            }catch (PropertyVetoException e){
                e.printStackTrace();
            }
            return ds;
        }
    }
    
    

    2.测试类

    package com.test;
    
    import com.config.SpringConfiguration;
    import com.domain.Account;
    import com.service.IAccountService;
    import org.junit.Before;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import java.util.List;
    
    /**
     * 描述:
     * 〈使用 junit 单元测试:测试我们配置〉
     *
     * @author zuiren
     * @create 2019/8/28
     * @since 1.0.0
     */
    public class AccountServiceTest {
        //1.获取容器
        ApplicationContext ac=null;
        //2.得到业务层对象
        IAccountService as=null;
    
        @Before
        public void init(){
            //1.获取容器
            ac=new AnnotationConfigApplicationContext(SpringConfiguration.class);
            //2.得到业务层对象
            as=ac.getBean("accountService",IAccountService.class);
        }
    
        @Test
        public void testFindAllAccount(){
            //3.执行方法
            List<Account> accounts = as.findAllAccount();
            for (Account account:accounts){
                System.out.println(account);
            }
        }
    
        @Test
        public void testFindAccountById(){
            //3.执行方法
            Account account=as.findAccountById(1);
            System.out.println(account);
        }
    
        @Test
        public void testSaveAccount(){
            //3.执行方法
            Account account=new Account();
            account.setName("卡兹克");
            account.setMoney(300);
    
            as.saveAccount(account);
        }
    
        @Test
        public void testUpdateAccount(){
    
        }
    
        @Test
        public void testDeleteAccount(){
    
        }
    }
    
    
  • 相关阅读:
    iOS 9之后Url链接的NSUTF8StringEncoding转码实现
    iOS实现应用更新及强制更新
    Objective-C代码简写
    Mac下使用数据库将Excel数据转换存入.plist
    iOS开发之图片压缩实现
    iOS键盘事件实现、控制
    iOS 轻击、触摸和手势的检测
    使用CocoaPods时遇到 Permission denied 问题
    2016-3-1 Mac下使用Hexo搭建Blog
    2016-3-1 安装Hexo过程中遇到的问题
  • 原文地址:https://www.cnblogs.com/zuiren/p/11437511.html
Copyright © 2011-2022 走看看