spring之配置文件更改成注解模式

当需要使用注解的时候

  • <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            https://www.springframework.org/schema/context/spring-context.xsd">
    
        <context:annotation-config/>
    
    </beans>

当不需要注解的时候

  • <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
    </beans>

替换部分配置

  • 第一步:
    更换注解头部
     

  • 第二步:
    替换如下配置

    1、给对应的类加上@Service、@Repository、将他们注入到spring容器
    2、使用Autowired注入数据

  • 第三步:
    配置自动扫描
     

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

替换全部配置文件

  • 替换bean.xml

    @Configuration
    public class BeanConfiguration {
    }
  • 替换扫描

    @ComponentScan(basePackages = {"com.spring"})
  • 替换QueryRunner

        @Bean
        public QueryRunner createQueryRunner(DataSource dataSource){
            return new QueryRunner(dataSource);
        }
  • 替换数据源

        @Bean(name = "dataSource")
        public DataSource createDataSource(){
            ComboPooledDataSource ds;
            try{
                ds = new ComboPooledDataSource();
                ds.setDriverClass("");
                ds.setJdbcUrl("");
                ds.setUser("");
                ds.setPassword("");
    
            }catch (Exception e){
                throw  new RuntimeException(e);
            }
            return ds;
        }

     
  • 测试需要替换

           ApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanConfiguration.class);
  • 也可以将@configuration拆分成多个,但是一定要能扫描到

  • @configuration 分类可以不写,只需要一个主配置类,但是需要导入其他配置类@Import()

    @Import(DataSourceConfiguration.class)
  • 注入propertier中的数据
      需要在配置文件中加入PropertySource这个属性

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

  • 测试方法也改成配置类

     

            导包        
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>5.0.2.RELEASE</version>
            </dependency>
            <!-- 需要更换成4.12版本以上 -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>



    替换

git地址:https://gitee.com/Xiaokeworksveryhard/springXmlToAnnotations.git

发布了158 篇原创文章 · 获赞 26 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/qq_41650354/article/details/103747742