spring boolean类型注入

Spring类型转换系统对于boolean类型进行了容错处理,除了可以使用“true/false”标准的Java值进行注入,还能使用“yes/no”、“on/off”、“1/0”来代表“真/假”。

1.创建BooleanBean类

package com.spring.pojo;
//boolean 类型
public class BooleanBean {
    private boolean success;
	@Override
	public String toString() {
		return "BooleanBean [success=" + success + "]";
	}
	public boolean isSuccess() {
		return success;
	}
	public void setSuccess(boolean success) {
		this.success = success;
	}    
}

2.创建Bean配置spring-booleanInject.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">
	<!-- boolean 参数值可以用on/off 来代表真/假 -->
	<bean id="booleanBean" class="com.spring.pojo.BooleanBean">
		<property name="success" value="on" />
	</bean>
	<!--boolean参数可以用yes/no 来表示真/假 -->
	<bean id="booleanBean1" class="com.spring.pojo.BooleanBean">
		<property name="success" value="no" />
	</bean>
	<!-- boolean参数可以用 1/0来表示真/假 -->
	<bean id="booleanBean2" class="com.spring.pojo.BooleanBean">
		<property name="success" value="0" />
	</bean>
</beans>

3.创建BooleanTest测试类

package com.spring.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.pojo.BooleanBean;
public class BooleanTest {
@SuppressWarnings("resource")
@Test
public void BooleanTest(){
	ApplicationContext act=new ClassPathXmlApplicationContext("spring-booleanInject.xml");
    BooleanBean boolea=act.getBean("booleanBean", BooleanBean.class);
    System.out.println("on/off :"+boolea.toString());
    BooleanBean boolea1=act.getBean("booleanBean1", BooleanBean.class);
    System.out.println("yes/no :"+boolea1.toString());
    BooleanBean boolea2=act.getBean("booleanBean2", BooleanBean.class);
    System.out.println("1/0 :"+boolea2.toString());
}
}

4.测试结果

on/off :BooleanBean [success=true]
yes/no :BooleanBean [success=false]
1/0 :BooleanBean [success=false]

 

 

### Spring框架中依赖注入的对象类型 Spring框架中的依赖注入(Dependency Injection, DI)可以注入多种类型的对象,这些对象主要包括以下几类: #### 1. **基本数据类型和字符串** 基本数据类型(如`int`, `long`, `boolean`等)以及`String`类型可以通过依赖注入的方式被注入到Bean中。这种类型注入通常用于配置一些简单的属性值[^3]。 #### 2. **其他Bean类型** 这是最常见的依赖注入场景之一。当一个Bean需要另一个Bean作为其依赖时,Spring容器会负责将所需的Bean实例注入到目标Bean中。这种方式使得组件之间能够实现松耦合的设计[^1]。 #### 3. **复杂类型/集合类型** 集合类型包括`List`, `Set`, `Map`, 和`Properties`等。Spring允许通过依赖注入的方式来初始化这些集合类型的属性。例如,如果某个Bean需要一组配置项或者多个子模块的列表,可以直接通过XML配置或注解完成注入操作[^3]。 --- 以下是基于不同注入方式的一个简单示例代码展示如何处理不同类型的数据: ```java // 使用构造器注入和其他形式组合的例子 @Component public class ExampleService { private final int number; // 基础类型 private final String message; // 字符串类型 private AnotherService anotherService; // 其他Bean类型 private List<String> items; // 集合类型 @Autowired public ExampleService(int number, String message) { this.number = number; this.message = message; } @Autowired public void setAnotherService(AnotherService anotherService) { this.anotherService = anotherService; } @Value("#{exampleConfig.items}") public void setItems(List<String> items) { this.items = items; } } ``` 在这个例子中展示了基础类型、字符串、其他Bean和服务端点的集合是如何一起工作的。 --- ### 总结 Spring框架提供了灵活的支持机制来满足各种业务需求下的依赖注入要求,无论是简单的数值还是复杂的结构化数据都可以轻松定义并由IoC容器统一管理[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值