Spring继承注入与加载多个配置文件

本文介绍了Spring框架中如何通过抽象类实现继承注入,并详细解释了不同引用对象的方式,包括local、bean和parent。此外还提供了使用容器加载多个配置文件的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.Spring继承(parent)注入

1.抽象继承类使用 abstract="true"
2.子类继承 parent="父类Id"

二.Spring中引用对象方式:

1.local:在当Xml文件中查找对象
<property name="student3">
<ref local="student3" />
</property>
2.bean:在所有的Xml文件中查找对象
spring 默认使用bean方式
bean有两种方式
方式一:
<property name="student3">
<ref bean="student3" />
</property>
方式二:
<property name="student3" ref="student3"/>
3. parent:在父对象中查找

三.使用容器加载多个配置文件

(1)数组方式:
new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml", "".... });
(2)通配符(*)
new ClassPathXmlApplicationContext("applicationContext*.xml"});
注意:配置文件命名统一
例:名字都是以applicationContext开头
applicationContext.xml(通用配置)
applicationContext-web.xml
applicationContext-biz.xml
applicationContext-dao.xml

举例:

Student1{studentId,studentName,password}

Student2{studentId,studentName}

Student3{address}

Student4{student2,student3}

两个配置文件

applicationContext.xml中

<!-- 公共类必须定义成 abstract="true",让子类继承parent="student" -->
	<bean id="student" abstract="true">
		<property name="studentId" value="1" />
		<property name="studentName" value="老赵" />
	</bean>

	<bean id="student2" class="com.tarena.entity.Student2" parent="student" />

applicationContext-bean.xml中

<bean id="student1" class="com.tarena.entity.Student1" parent="student">
		<property name="password" value="123456" />
	</bean>

	<bean id="student3" class="com.tarena.entity.Student3">
		<property name="address" value="北京海淀" />
	</bean>
	<bean id="student4" class="com.tarena.entity.Student4">
		<property name="student2">
			<!-- 在当前xml文件中找对象 -->
			<ref bean="student2" />
		</property>
		<!--<property name="student3">
			<ref local="student3" />
		</property>
	    -->
	    <property name="student3" ref="student3"/>
	</bean>

测试类:

public class StudetTest {
	private static Log log = LogFactory.getLog(StudetTest.class);
	private ApplicationContext ac;

	@Before
	public void setUp() {
		// ac =new ClassPathXmlApplicationContext("applicationContext.xml");
		/*ac = new ClassPathXmlApplicationContext(new String[] {
				"applicationContext.xml", "applicationContext-bean.xml" });*/
		ac = new ClassPathXmlApplicationContext("applicationContext*.xml");
	}

	@Test
	@Ignore
	public void testStudent1() {
		Student1 student = (Student1) ac.getBean("student1");
		log.info(student.getPassword());
		log.info(student.getStudentId());
		log.info(student.getStudentName());
	}

	@Test
	@Ignore
	public void testStudent2() {
		Student2 student = (Student2) ac.getBean("student2");
		log.info(student.getStudentId());
		log.info(student.getStudentName());
	}

	@Test
	@Ignore
	public void testStudent3() {
		Student3 student = (Student3) ac.getBean("student3");
		log.info(student.getAddress());
	}

	@Test
	//@Ignore
	public void testStudent4() {
		Student4 student4 = (Student4) ac.getBean("student4");
		log.info(student4.getStudent2().getStudentId());
		log.info(student4.getStudent2().getStudentName());
		log.info(student4.getStudent3().getAddress());
	}
}




### 如何在Spring Boot应用程序中正确加载和使用YAML配置文件 #### 加载YAML配置文件的方式 为了使Spring Boot能够识别并解析`.yml`或`.yaml`格式的配置文件,在项目的根目录下创建名为`application.yml`或`application.yaml`的文件即可[^1]。 #### YAML配置文件结构示例 下面是一个简单的`application.yml`文件的例子: ```yaml server: port: 8081 spring: datasource: url: jdbc:mysql://localhost:3306/testdb?useSSL=false&serverTimezone=UTC username: root password: secret ``` 此段代码定义了一个服务器端口以及数据源连接字符串、用户名和密码[^2]。 #### 使用@Value注解读取单个属性值 如果只需要读取少量配置项,则可以采用`@Value`注解来获取特定键对应的值。例如,要访问上面提到的数据源URL,可以在类成员变量上加上如下声明: ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class DataSourceConfig { @Value("${spring.datasource.url}") private String dbUrl; } ``` 这种方式适用于简单场景下的个别参数注入。 #### 创建自定义Java Bean映射多个属性 对于更复杂的设置或者当存在大量关联紧密的相关选项时,建议通过构建专门的对象模型来进行批量处理。首先定义一个POJO类表示这些配置信息: ```java import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Data @Component @ConfigurationProperties(prefix = "spring.datasource") public class DatabaseSettings { private String url; private String username; private String password; } ``` 接着确保启用了对`ConfigurationProperties`的支持——通常只需让主应用类继承`SpringBootApplication`就足够了;之后就可以像普通Bean那样依赖注入这个新组件实例,并从中方便地获得所需的信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值