1)Bean属性的直接赋值
这里是使用XML文件,对Bean的属性进行赋值,在赋值的过程中不仅有基本类型的属性,也有集合类型的属性,在对XML文件进行配置的时候,这些集合类型属性的赋值有些许差别,需要注意。
Bean类
import java
.util
.Date
;
import java
.util
.List
;
import java
.util
.Map
;
import java
.util
.Set
;
import lombok
.Data
;
@Data
public class Bean1 {
private String strValue
;
private int intValue
;
private List listValue
;
private Set setValue
;
private Map mapValue
;
private String
[] strArray
;
private Date dateValue
;
}
applicationContext-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="bean1" class="com.mechanicalwood.bean.Bean1">
<property name="strValue" value="abc"/>
<property name="intValue" value="123"/>
<property name="listValue">
<list>
<value>value1
</value>
<value>value2
</value>
<value>value3
</value>
</list>
</property>
<property name="strArray">
<list>
<value>arrayValue1
</value>
<value>arrayValue2
</value>
<value>arrayValue3
</value>
</list>
</property>
<property name="mapValue">
<map>
<entry key="mapkey1" value="mapValue1"/>
<entry key="mapkey2" value="mapValue2"/>
<entry key="mapkey3" value="mapValue3"/>
</map>
</property>
<property name="setValue">
<set>
<value>setValue1
</value>
<value>setValue2
</value>
<value>setValue3
</value>
</set>
</property>
<property name="dateValue" value="2019-5-26"/>
</bean>
</beans>
运行结果
2)配置Bean类依赖关系
Bean关系
import lombok
.Data
;
@Data
class Bean2 {
private Bean3 bean3
;
private Bean4 bean4
;
private Bean5 bean5
;
}
class Bean3 {
private int id
;
private String username
;
private String password
;
}
class Bean4 {
private int id
;
private String username
;
}
class Bean5 {
private int age
;
}
首先进行分析Bean3和Bean4有共同的属性,可以在XML文件中设置抽象Bean,Bean3、Bean4、Bean5同时又是Bean2的属性,所以要配置彼此间的关系。
applicationContext-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="abstractBean" abstract="true">
<property name="id" value="888"/>
<property name="username" value="MechanicalWood"/>
</bean>
<bean id="bean3" class="com.mechanicalwood.bean.Bean3" parent="abstractBean">
<property name="password" value="666"/>
</bean>
<bean id="bean4" class="com.mechanicalwood.bean.Bean4" parent="abstractBean"/>
<bean id="bean5" class="com.mechanicalwood.bean.Bean5">
<property name="age" value="18"/>
</bean>
<bean id="bean2" class="com.mechanicalwood.bean.Bean2">
<property name="bean3" ref="bean3"/>
<property name="bean4" ref="bean4"/>
<property name="bean5" ref="bean5"/>
</bean>
</beans>
运行结果
自定义属性配置
1)自定义属性编辑器
public class MyEditConvert extends PropertyEditorSupport {
private String dateParttern
= "yyyy-MM-dd";
@Override
public void setAsText(String text
) throws IllegalArgumentException
{
Date date
= null
;
try {
SimpleDateFormat sdf
= new SimpleDateFormat(dateParttern
);
date
= sdf
.parse(text
);
setValue(date
);
}catch (Exception ex
){
ex
.printStackTrace();
}
}
}
2)通过xml文件注册属性编辑器给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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date" value="com.mechanicalwood.MyEditConvert"/>
</map>
</property>
</bean>
</beans>
singleton/prototype(作用域)
Bean6
public class Bean6 {
private String strValue
;
public Bean6(){
}
public String
getStrValue() {
return strValue
;
}
public void setStrValue(String strValue
) {
this.strValue
= strValue
;
}
}
默认情况下,Bean在IoC容器中只存在一个实例,所有对象对其的引用都返回同一个。 设置为多例:
<?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="bean6" class="com.mechanicalwood.bean.Bean6" scope="prototype">
<property name="strValue" value="MechanicalWood"/>
</bean>
</beans>
测试代码
import org
.springframework
.context
.ApplicationContext
;
import org
.springframework
.context
.support
.ClassPathXmlApplicationContext
;
import java
.util
.Arrays
;
public class BeanTest {
private ApplicationContext context
= null
;
@Before
public void initMethod(){
context
= new ClassPathXmlApplicationContext("applicationContext-beans.xml");
}
@Test
public void testSpringInject(){
Bean1 bean1
= (Bean1
) context
.getBean("bean1");
System
.out
.println("intValue = " + bean1
.getIntValue());
System
.out
.println("strValue = "+ bean1
.getStrValue());
System
.out
.println("strArrayValue = " + Arrays
.toString(bean1
.getStrArray()));
System
.out
.println("listValue = " + bean1
.getListValue());
System
.out
.println("mapValue = " + bean1
.getMapValue());
System
.out
.println("setValue = " + bean1
.getSetValue());
System
.out
.println("dateValue = " + bean1
.getDateValue());
}
@Test
public void testSpringInject2(){
Bean2 bean2
= (Bean2
) context
.getBean("bean2");
System
.out
.println("bean3.id = " + bean2
.getBean3().getId());
System
.out
.println("bean3.username = " + bean2
.getBean3().getUsername());
System
.out
.println("bean3.password = " + bean2
.getBean3().getPassword());
System
.out
.println("bean4.id = " + bean2
.getBean4().getId());
System
.out
.println("bean4.username = " + bean2
.getBean4().getUsername());
System
.out
.println("bean5.age = " + bean2
.getBean5().getAge());
}
@Test
public void testSpringInject3(){
Bean6 bean6_01
= (Bean6
) context
.getBean("bean6");
Bean6 bean6_02
= (Bean6
) context
.getBean("bean6");
if(bean6_01
.equals(bean6_02
)){
System
.out
.println("bean6_01 equals bean6_02");
}else{
System
.out
.println("bean6_01 not equals bean6_02");
}
}
}