目录
spring管理bean 通过bean给对象赋值
语法格式<bean id=“” class=>(id自己选择 class要包名到类名)
通过property给对象赋值(通过set方法)
里面的属性 id:自己设置 name:属性名 value:属性值
先建立一个类person 属性有id name
public class Person { private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + "]"; } public Person(Integer id, String name) { super(); this.id = id; this.name = name; } public Person() { // TODO Auto-generated constructor stub } }
建立xml文件配置bean
<bean id="personOne" class="com.atguigu.spring.mod.Person"> <!-- <property>:为对象的某个属性赋值 name:属性名 value:属性值 --> <property name="id" value="1111"></property> <property name="name" value="小明"></property> </bean>
测试类
public static void main(String[] args) { ConfigurableApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); Person person=(Person)ac.getBean("personONe", Person.class);//第一个参数是xml的id名 第二个是类的class System.out.print(person); }
通过constuctor-arg给对象赋值
语法:<consturctor-arg>
<bean id="personThree" class="com.atguigu.spring.mod.Person"> <constructor-arg value="2222"> </constructor-arg> <constructor-arg value="小潘"></constructor-arg> </bean>
如果在实体类里面有多个构造器,
student类
ublic class Student { private Integer id; private String name; private Integer age; private String sex; private Double score; private Teacher teacher; public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } public Double getScore() { return score; } public void setScore(Double score) { this.score = score; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + ", score=" + score + ", teacher=" + teacher + "]"; } public Student() { super(); // TODO Auto-generated constructor stub } public Student(Integer id, String name, Integer age, String sex) { super(); this.id = id; this.name = name; this.age = age; this.sex = sex; } public Student(Integer id, String name, Double score, String sex) { this.id = id; this.name = name; this.score = score; this.sex = sex; } }
1.通过索引 constructor里面有一个属性index
<bean id="s3" class="com.atguigu.spring.di.Student"> <constructor-arg value="10022"></constructor-arg> <constructor-arg value="王五"></constructor-arg> <constructor-arg value="90" index="2" type="java.lang.Double"></constructor-arg>代表第二个参数是给double类型赋值的 <constructor-arg value="女"></constructor-arg> </bean>
通过p名称给属性赋值
<bean id="s4" class="com.atguigu.spring.di.Student" p:id="10033" p:name="赵六" p:age="26" p:sex="男" ></bean>
属性ref
当给属性赋的值是一个对象时 用ref 语法格式:ref="对象的id"(也就是说 ref后面时配置文件中某一bean的id)
<bean id="s5" class="com.atguigu.spring.di.Student"> <property name="teacher" ref="teacher"></property> </bean> <bean id="teacher" class="com.atguigu.spring.di.Teacher"> <property name="tid" value="10000"></property> <property name="tbane" value="小明"></property> </bean> //如果用P来给该属性赋值 语法格式:p:teacher-ref="" <bean id="s4" class="com.atguigu.spring.di.Student" p:id="10033" p:name="赵六" p:age="26" p:sex="男" p:teacher-ref="teacher"></bean>
级联赋值
例如给student的teacher属性赋值 teacher里面又有name属性
<bean id="s5" class="com.atguigu.spring.di.Student"> <property name="id" value="10055"></property> <property name="name" value="张三三"></property> <property name="age" value="23"></property> <property name="sex" value="男"></property> <property name="teacher" ref="teacher"></property> <property name="teacher.tname" value="小红"></property>/// </bean>
内部bean
例如给student的teacher属性赋值 但是teacher也是一个类 就可以建一个内部bean class放的是teacher
定义在某个bean的内部bean 只能在该bean使用
<bean id="s6" class="com.atguigu.spring.di.Student"> <property name="id" value="10066"></property> <property name="name" value="崔八"></property> <property name="age" value="18"></property> <property name="sex" value="男"></property> <property name="teacher">//下面这个bean是在这个property里面的 <bean id="tt" class="com.atguigu.spring.di.Teacher"> <property name="tid" value="1"></property> <property name="tname" value="谢娜"></property> </bean> </property> </bean>
给集合属性赋值
例如给teacher的cls班级赋值 班级是list类型 就用
<llist>
<value></value>
<list>
<bean> <property name="tid" value="2"></property> <property name="tname" value="薛之谦"></property> <property name="cls"> <list> <value>1</value> <value>2</value> <value>3</value> </list> </property> </bean>
如果list里面放的是引用数据类型
例如teacher里面放的list是student类型(List<studemt>)
需要用<ref/ bean=""> bean里面放的是之前定义过的学生类型
<bean id="t2" class="com.atguigu.spring.di.Teacher"> <property name="tid" value="3"></property> <property name="tname" value="张杰"></property> <property name="students"> <list> <ref bean="s1"/> <ref bean="s2"/> <ref bean="s3"/> </list> </property> </bean>
如果该集合是map类型
语法格式
<property name="p">
放的是key 放的是value
</property>
例如teacher里面有个map类型的(private Map<String, String> bossMap;
<bean id="t3" class="com.atguigu.spring.di.Teacher"> <property name="tid" value="10003"></property> <property name="tname" value="admin"></property> <property name="bossMap"> <map> <entry> <key> <value>10011</value> </key> <value>教导主任</value> </entry> <entry> <key> <value>10012</value> </key> <value>校长</value> </entry> </map> </property> </bean>
工厂bean 通过继承FactoryBean 来对属性赋值
public class MyFactory implements FactoryBean<Car> { @Override public Car getObject() throws Exception { Car car = new Car(); car.setBrand("奥迪"); car.setPrice(200000.0); return car; } @Override public Class<?> getObjectType() { // TODO Auto-generated method stub return Car.class; } @Override public boolean isSingleton() { // TODO Auto-generated method stub return false; } }
<bean id="factory" class="com.atguigu.spring.factorybean.MyFactory"></bean>
测试
public class Test { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("factory-bean.xml"); Object object = ac.getBean("factory"); System.out.println(object); } }