Spring的对象注入
写一个类
public class Circle {
@Override
public String toString() {
return "Circle [radius=" + radius + "]";
}
private double radius;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public Circle() {
super();
}
}
再创建一个bean.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">
<!-- //此行在调用加载时会创建一个Circle 一个bean节点对应一个对象为单例,下次再调用时依然还是以前创建的对象。执行的为为无参的构造方法 -->
<bean id="circle " class="com.bean.Circle" />
<!-- 也可以给对象属性设值 在类中一定要写set方法-->
<bean id="circle2" class="com.bean.Circle">
<property name="radius" value="2.0"></property>
</bean>
<!-- 可以使用上个对象的值 不过要先加载上个对象 -->
<bean id="circle3" class="com.bean.Circle">
<property name="radius" value="#{circle2.radius+1}"></property>
</bean>
</beans>
在代码中如何实现
@Test
public void CircleTest(){
Circle circle= (Circle) new ClassPathXmlApplicationContext("bean.xml").getBean("circle");
circle.setRadius(12);
System.out.println(circle);
Circle circle2= (Circle) new ClassPathXmlApplicationContext("bean.xml").getBean("circle2");
System.out.println(circle2);
Circle circle3= (Circle) new ClassPathXmlApplicationContext("bean.xml").getBean("circle3");
System.out.println(circle3);
}
当类中的属性有引用类型
<?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">
<!-- 当属性是引用类型时 用值用 ref -->
<bean id="xiaoming" class="com.bean.Student">
<property name="id" value="1"></property>
<property name="name" value="xiaoming"></property>
<property name="score" value="78"></property>
<property name="birthday" ref="birthday"></property>
</bean>
<bean id="birthday" class="com.bean.BirthDay">
<property name="year" value="1994"></property>
<property name="month" value="03"></property>
<property name="day" value="15"></property>
</bean>
</beans>
特殊属性如List Set 等
<bean id="myColl" class="com.phone1000.je1702.sp03.MyCollection">
<property name="array">
<array>
<value>zhangsan</value>
<value>zhaoziqiang</value>
<value>xiazenghao</value>
<value>hejiangtao</value>
<value>wucaibing</value>
<value>chenjianjian</value>
<value>hushengli</value>
</array>
</property>
<property name="list">
<list>
<value>wangjishen</value>
<value>hexiangyi</value>
<value>wanghailin</value>
<value>lizhiyang</value>
</list>
</property>
<property name="set">
<set>
<value>zhangruixuan</value>
<value>yangyubing</value>
<value>changzhongbo</value>
<value>zhoujunfeng</value>
</set>
</property>
<property name="map">
<map>
<entry key="banzhang" value="zhangjiandong"></entry>
<entry key="xuewei" value="changzhongbo"></entry>
<entry key="laowei" value="hushengli"></entry>
<entry key="kongwei" value="xujinxin"></entry>
</map>
</property>
<property name="props">
<props>
<prop key="driver">mysqldriver</prop>
<prop key="url">mysqlurl</prop>
<prop key="username">root</prop>
<prop key="password">root</prop>
</props>
</property>
</bean>
有参构造
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- <bean id="emp" class="com.sp05.Employee">
<constructor-arg name="sex" value="boy" />
<constructor-arg name="dept" ref="dept" />
<constructor-arg name="name" value="zhang"/>
<constructor-arg name="salary" value="10000" />
</bean> -->
<!-- <bean id="emp" class="com.sp05.Employee">
<constructor-arg name="name" value="zhang"/>
<constructor-arg name="salary" value="10000" />
<constructor-arg name="sex" value="boy" />
<constructor-arg name="dept" ref="dept" />
<property name="dept" ref="dept"></property>
</bean> -->
<!-- <bean id="emp" class="com.sp05.Employee">
<constructor-arg index="0" value="zhang"/>
<constructor-arg index="1" value="10000" />
<constructor-arg index="2" value="boy" />
<constructor-arg index="3" ref="dept" />
</bean> -->
<!-- <bean id="emp" class="com.sp05.Employee">
<constructor-arg type="java.lang.String" value="zhang"/>
<constructor-arg type="double" value="10000" />
<constructor-arg type="java.lang.String" value="boy" />
<constructor-arg type="com.sp05.Dept" ref="dept" />
</bean> -->
<bean id="emp" class="com.sp05.Employee">
<constructor-arg value="zhang"/>
<constructor-arg value="10000" />
<constructor-arg value="boy" />
<constructor-arg ref="dept" />
</bean>
<bean id="dept" class="com.sp05.Dept">
<property name="did" value="101" />
<property name="dname" value="java ee" />
</bean>
</beans>