zoukankan      html  css  js  c++  java
  • spring boot @propertySource @importResource @Bean [六]

    @propertySource

    指定property的配置源。

    创建一个person.property:

    然后修改person注解;

    在运行test之后,结果为:

    @importResource

    这个importResource是用来兼容spring的。

    HelloService:

    package com.axm.demo.service;
    public class HelloService {
        public  HelloService(){
    
        }
    }
    

    beans.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <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 http://www.springframework.org/schema/beans/spring-beans.xsd">
       <bean id="helloService" class="com.axm.demo.service.HelloService"></bean>
    </beans>
    

    然后注入:

    @SpringBootApplication
    @ImportResource(locations = {"classpath:beans.xml"}) //关键
    public class DemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }
    

    测试:

    @Test
    void contextLoads() {
        boolean has=ioc.containsBean("helloService");
        System.out.println(has);
    }
    

    结果:

    然后再spring boot 有新的方式去注入服务:

    添加一个配置文件:

    @Configuration
    public class MyApplication {
        @Bean
        public HelloService helloService(){
            return  new HelloService();
        }
    }
    

    然后就会自动注入这个HelloService 服务。

  • 相关阅读:
    数据结构(java语言描述)顺序栈的使用(两个大数相加)
    DB2 Sql性能查看与优化
    spring默认为单例模式
    Java map的匿名类的初始化
    使用nohup后台执行ftp传输命令
    停止一个java的线程执行
    静态类
    XML语法随记
    Crontab有关
    string转Date转回String(JAVA)
  • 原文地址:https://www.cnblogs.com/aoximin/p/12906912.html
Copyright © 2011-2022 走看看