Spring架构学习(2)通过Properties文件实现依赖注入

还是用上次的生产者——消费者例子,代码如下:

package my.spring.bean.demo;

public interface IoCImpl {
	
	public void run();
	
}
package my.spring.bean.demo;

import my.spring.bean.pojo.Dispatcher;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;

public class PropertyFileImpl implements IoCImpl {

	@Override
	public void run() {
		// TODO Auto-generated method stub
		DefaultListableBeanFactory registry=new DefaultListableBeanFactory();
		BeanFactory container=bindViaPropertyFile(registry);
		Dispatcher dispatcher=(Dispatcher)container.getBean("dispatcherB");
		dispatcher.dispatch();
	}
	
	public static BeanFactory bindViaPropertyFile(BeanDefinitionRegistry registry){
		PropertiesBeanDefinitionReader reader=new PropertiesBeanDefinitionReader(registry);
		reader.loadBeanDefinitions(ClassLoader.getSystemResource("")+"../binding-config.properties");
		return (BeanFactory) registry;
	}
}


注意properties文件的目录,这里采用ClassLoader获取当前的classpath。

properties文件配置如下,其中包含了“constructor注入”和“setter注入”两种方式

dispatcherB.(class)=my.spring.bean.pojo.Dispatcher
#Introject via constructor
dispatcherB.$0(ref)=producerB
dispatcherB.$1(ref)=consumerB
#Introject by setting properties
#dispatcherB.producer(ref)=producerB
#dispatcherB.consumer(ref)=consumerB

producerB.(class)=my.spring.bean.pojo.Producer
producerB.$0=106

consumerB.(class)=my.spring.bean.pojo.Consumer
consumerB.$0=88

具体语法参照SpringAPI中PropertiesBeanDefinitionReader示例说明,当然也可以自己重写语义规则 

最后单元测试

package my.spring.bean.demo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class IoCImplTest {
	
	private static IoCImpl impl=null;
	@Test
	public void testPropertyFileImpl(){
		impl=new PropertyFileImpl();
		impl.run();
	}
}


这里要对核心类PropertiesBeanDefinitionReader稍作讲解,先看下各个类之间的关系

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
回答: Spring Boot的架构文件结构通常包括以下几个主要部分: 1. 主应用程序:这是Spring Boot应用程序的入口点,通常包含`@SpringBootApplication`注解。它负责启动应用程序并自动配置Spring环境。 2. 配置文件Spring Boot支持多种配置文件格式,如properties和yaml。配置文件用于配置应用程序的各种属性,如数据库连接、端口号等。 3. 控制器:控制器用于处理HTTP请求并返回响应。它们通常使用`@RestController`或`@Controller`注解进行标记。 4. 服务:服务包含应用程序的业务逻辑。它们通常使用`@Service`注解进行标记,并通过依赖注入来使用其他组件。 5. 数据访问层:数据访问层负责与数据库或其他数据源进行交互。它们通常使用`@Repository`注解进行标记,并使用Spring Data JPA或其他ORM框架来简化数据访问操作。 6. 静态资源:静态资源文件(如CSS、JavaScript和图像文件)可以放置在`resources/static`目录下。Spring Boot会自动将这些文件提供给客户端。 7. 模板引擎:Spring Boot支持多种模板引擎,如Thymeleaf和Freemarker。模板引擎用于生成动态内容,如HTML页面。模板文件通常放置在`resources/templates`目录下。 总之,Spring Boot的架构文件结构是根据约定进行组织的,以便快速开发和部署应用程序。它提供了自动配置和默认值,使开发人员可以专注于业务逻辑而不必关注繁琐的配置。 #### 引用[.reference_title] - *1* [SpringBoot架构](https://blog.csdn.net/m0_58212960/article/details/125920222)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [SpringBoot 目录与文件结构的介绍](https://blog.csdn.net/hlx20080808/article/details/122344682)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值