Spring配置Bean,为属性赋值
SayHello的实体类:
package com.langchao;
/**
* @ClassName: SayHello
* @description:
* @author: ZhangYawei
* @Date: 2019\2\23 15:59
**/
public class SayHello
{
private String name;
private int age;
private char sex;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public char getSex()
{
return sex;
}
public void setSex(char sex)
{
this.sex = sex;
}
public void hellowSpring()
{
System.out.println("HellowSpring" + name+" " +age+" "+sex);
}
@Override
public String toString()
{
final StringBuilder sb = new StringBuilder("SayHello{");
sb.append("name='").append(name).append('\'');
sb.append(", age=").append(age);
sb.append(", sex=").append(sex);
sb.append('}');
return sb.toString();
}
}
Spring配置文件applicationContext.xml里配置SayHello的Bean:
<!--创建Bean: class:类的全类名;通过反射在IOC容器里面创建Bean,所以必须有一个默认的无参构造 id:标识容器中的Bean,id唯一--> <bean id="sayHello" class="com.langchao.SayHello"> <property name="name" value="赵丽颖"></property> <property name="age" value="11"></property> <property name="sex" value="女"></property> </bean>
获取Bean:
ApplicationContext ctx= new ClassPathXmlApplicationContext("applicationContext.xml"); SayHello sayHello1 = (SayHello)ctx.getBean("sayHello"); sayHello1.hellowSpring();
打印结果:
Car的实体类:
package com.langchao; /** * @ClassName: Car * @description: 车辆信息的Bean * @author: ZhangYawei * @Date: 2019\2\24 10:26 **/ public class Car { private String brand; private String corp; private double prize; private int speed; public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getCorp() { return corp; } public void setCorp(String corp) { this.corp = corp; } public double getPrize() { return prize; } public void setPrize(double prize) { this.prize = prize; } public int getSpeed() { return speed; } public void setSpeed(int speed) { this.speed = speed; } public Car(String brand, String corp, double prize) { this.brand = brand; this.corp = corp; this.prize = prize; } public Car(String brand, String corp, int speed) { this.brand = brand; this.corp = corp; this.speed = speed; } public Car(String brand, String corp, double prize, int speed) { this.brand = brand; this.corp = corp; this.prize = prize; this.speed = speed; } public Car() { } @Override public String toString() { final StringBuilder sb = new StringBuilder("Car{"); sb.append("brand='").append(brand).append('\''); sb.append(", corp='").append(corp).append('\''); sb.append(", prize=").append(prize); sb.append(", speed=").append(speed); sb.append('}'); return sb.toString(); } }
Spring配置文件applicationContext.xml里配置Car的Bean:
<!--通过构造器的方式配置Bean:通过index下标-->
<bean id="car" class="com.langchao.Car">
<constructor-arg value="Baoma" index="0"></constructor-arg>
<constructor-arg value="Shanghai" index="1"></constructor-arg>
<constructor-arg value="4000000" index="2"></constructor-arg>
<constructor-arg value="250" index="3"></constructor-arg>
</bean>
获取Car:
Car car = (Car)ctx.getBean("car");
System.out.println(car);
打印结果:
通过name赋值(如果赋的值里面有特殊符号:用<![CDATA[]]>包裹起来):
<!--通过构造器配置Bean:通过name赋值-->
<bean id="car1" class="com.langchao.Car">
<constructor-arg value="Audi" name="brand"></constructor-arg>
<!--如果value值里面包含特殊符号:使用<![CDATA[]]>包裹起来-->
<constructor-arg name="corp">
<value><![CDATA[<上海热的很啊>]]></value>
</constructor-arg>
<constructor-arg value="240" name="speed"></constructor-arg>
</bean>
控制台:
通过idex和数据类型(type)赋值,走的是brand,corp,speed的有参构造:
<!--通过构造器配置Bean:通过下标和数据类型赋值-->
<bean id="car2" class="com.langchao.Car">
<constructor-arg value="FentTian" index="0"></constructor-arg>
<constructor-arg value="Shanghai" type="java.lang.String"></constructor-arg>
<constructor-arg value="22222" type="int"></constructor-arg>
</bean>
控制台:
Person的实体类:
package com.langchao; /** * @ClassName: Person * @description: 人的实体Bean * @author: ZhangYawei * @Date: 2019\2\24 11:01 **/ public class Person { private String name; private int age; private char sex; private Car car; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } @Override public String toString() { final StringBuilder sb = new StringBuilder("Person{"); sb.append("name='").append(name).append('\''); sb.append(", age=").append(age); sb.append(", sex=").append(sex); sb.append(", car=").append(car); sb.append('}'); return sb.toString(); } }
把Car引入到Person中:
<!--配置人的Bean:引用car的实体--> <bean id="person" class="com.langchao.Person"> <property name="name" value="Tomo"></property> <property name="age" value="21"></property> <property name="sex" value="男"></property> <property name="car" ref="car1"></property> <property name="car.prize" value="323232"></property> <!--内部Bean,不能被外部引用,只能在内部使用 <property name="car"> <bean id="car3" class="com.langchao.Car"> <property name="brand" value="Jery"></property> <property name="corp" value="Tesila"></property> <property name="prize" value="2122"> </property> <property name="speed" value="250"></property> </bean> </property>--> </bean>
控制台:
Map配置Bean的方法:
package com.langchao; import java.util.Map; /** * @ClassName: NewPerson * @description: 新人的实体Bean * @author: ZhangYawei * @Date: 2019\2\24 11:59 **/ public class NewPerson { private String name; private int age; private Map<String,Car> cars; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Map<String, Car> getCars() { return cars; } public void setCars(Map<String, Car> cars) { this.cars = cars; } @Override public String toString() { final StringBuilder sb = new StringBuilder("NewPerson{"); sb.append("name='").append(name).append('\''); sb.append(", age=").append(age); sb.append(", cars=").append(cars); sb.append('}'); return sb.toString(); } }
Spring配置文件applicationContext.xml里配置信息:
<bean id="newPerson" class="com.langchao.NewPerson"> <property name="name" value="大头儿子"></property> <property name="age" value="21"></property> <property name="cars"> <map> <entry key="AA" value-ref="car"></entry> <entry key="BB"> <bean class="com.langchao.Car"> <property name="brand" value="小头爸爸"></property> <property name="corp" value="Modole2"></property> <property name="prize" value="322222"></property> <property name="speed" value="250"></property> </bean> </entry> </map> </property> </bean>
控制台信息:
通过util:map(把map单独提取出来,可供多个Bean引用)命名空间和p命名空间配置,要导入util包和p命名空间的包:
<!--单独把map提取出来可供多个Bean引用-->
<util:map id="cars">
<entry key="AA" value-ref="car"/>
<entry key="BB" value-ref="car1"/>
<entry key="CC" value-ref="car2"/>
</util:map>
<!--p命名空间配置Bean:--> <bean id="person1" class="com.langchao.NewPerson" p:name="Rose" p:age="23" p:cars-ref="cars"></bean>
对应的car,car1,car2配置:
<!--通过构造器的方式配置Bean:通过index下标--> <bean id="car" class="com.langchao.Car"> <constructor-arg value="Baoma" index="0"></constructor-arg> <constructor-arg value="Shanghai" index="1"></constructor-arg> <constructor-arg value="4000000" index="2"></constructor-arg> <constructor-arg value="250" index="3"></constructor-arg> </bean> <!--通过构造器配置Bean:通过name赋值--> <bean id="car1" class="com.langchao.Car"> <constructor-arg value="Audi" name="brand"></constructor-arg> <!--如果value值里面包含特殊符号:使用<![CDATA[]]包裹起来--> <constructor-arg name="corp"> <value><![CDATA[<上海热的很啊>]]></value> </constructor-arg> <constructor-arg value="240" name="speed"></constructor-arg> </bean> <!--通过构造器配置Bean:通过下标和数据类型赋值--> <bean id="car2" class="com.langchao.Car"> <constructor-arg value="FentTian" index="0"></constructor-arg> <constructor-arg value="Shanghai" type="java.lang.String"></constructor-arg> <constructor-arg value="22222" type="int"></constructor-arg> </bean>
Person对象:
NewPerson newPerson = (NewPerson)cxt.getBean("person1");
System.out.println(newPerson);
控制台信息:
集合Bean的配置,HeyMan的实体类;
package com.langchao; import java.util.List; /** * @ClassName: HeyMan * @description: * @author: ZhangYawei * @Date: 2019\2\25 10:11 **/ public class HeyMan { private String name; private int age; private List<Car> carss; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public List<Car> getCarss() { return carss; } public void setCarss(List<Car> carss) { this.carss = carss; } @Override public String toString() { final StringBuilder sb = new StringBuilder("HeyMan{"); sb.append("name='").append(name).append('\''); sb.append(", age=").append(age); sb.append(", carss=").append(carss); sb.append('}'); return sb.toString(); } }
Spring配置文件applicationContext.xml里配置信息:
<!--单独把list提取出来可供多个Bean引用-->
<util:list id="carss">
<ref bean="car"/>
<ref bean="car1"/>
<ref bean="car2"/>
</util:list>
<!--p命名空间配置Bean:-->
<bean id="heyMan" class="com.langchao.HeyMan" p:name="倚天屠龙" p:age="24" p:carss-ref="carss"></bean>
控制台信息:
Properties配置属性值,DataSource实体类:
package com.langchao; import java.util.Properties; /** * @ClassName: DataSource * @description: * @author: ZhangYawei * @Date: 2019\2\25 10:18 **/ public class DataSource { private Properties properties; public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } @Override public String toString() { final StringBuilder sb = new StringBuilder("DataSource{"); sb.append("properties=").append(properties); sb.append('}'); return sb.toString(); } }
Spring配置文件applicationContext.xml里配置信息:
<!--对Properties配置的Bean,进行赋值:-->
<bean id="dataSource" class="com.langchao.DataSource">
<property name="properties">
<props>
<prop key="url">www.baidu.com</prop>
<prop key="username">root</prop>
<prop key="password">root</prop>
<prop key="driver">mysql:dreiver.com</prop>
</props>
</property>
</bean>
控制台信息: