本篇就简单了聊一下spring中Bean管理和操作。
首先说一下spring中管理操有两种方式:
现在就聊一下xml配置的文件的不同配置方式的不同效果。
其实一直说bean,简单的说就是java对象。
因为后面很多时候会使用配置文件,格式:
<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">
beans>
首先来一个无参创建对象,这个很简单。
这个时候简单的时候创建一个类:
package com.xzd.test;
public class Person {
public void play(){
System.out.println("我是喜欢运动小仙女,所以才玩飞盘的");
}
}
然后下xml中进行配置信息:
然后体验一下:
public class test {
public static void main(String[] args) {
ApplicationContext applicationContext= new ClassPathXmlApplicationContext("personbean.xml");
Person person1= (Person) applicationContext.getBean("person");// 这里会增加一个强制转换或者如下
Person person2= applicationContext.getBean("person",Person.class);// 这里可以通过参数来确定这个spring创建的对象是什么类型
person1.play();
person2.play();
}
}
补充XML标签解释:
但是这种创建的对象,可以看出创建的对象其没有属性的或者是无参构造对象。
演示一下:
package com.xzd.test;
public class Person {
String personname;
int age;
public Person(String personname, int age) {
this.personname = personname;
this.age = age;
}
@Override
public String toString() {
return "Person={\"name\":"+this.personname+",\"age\":"+this.age+"}";
}
}
然后配置xml
<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="person" class="com.xzd.test.Person">
<constructor-arg name="personname" value="我是张三啊">constructor-arg>
<constructor-arg name="age" value="18">constructor-arg>
bean>
beans>
然后调用:
public class test {
public static void main(String[] args) {
ApplicationContext applicationContext= new ClassPathXmlApplicationContext("personbean.xml");
Person person= applicationContext.getBean("person",Person.class);// 这里可以通过参数来确定这个spring创建的对象是什么类型
System.out.println(person.toString());
}
}
解释补充:
对于属性,前面在创建对象的时候,通过构造参数进行属性,赋值,但是在前面也是可以同set进行属性赋值没说白了就是:
public class Person {
String personname;
int age;
public void setAge(int age) {
this.age = age;
}
public void setPersonname(String personname) {
this.personname = personname;
}
@Override
public String toString() {
return "Person={\"name\":"+this.personname+",\"age\":"+this.age+"}";
}
}
但是既然聊spring,自然就不能使用创建对象后使用setPersonname,现在演示:
<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="person" class="com.xzd.test.Person">
<property name="personname">
<value>我是张三啊value>
property>
<property name="age" value="18">property>
bean>
beans>
然后试一下:
public class test {
public static void main(String[] args) {
ApplicationContext applicationContext= new ClassPathXmlApplicationContext("personbean.xml");
Person person= applicationContext.getBean("person",Person.class);// 这里可以通过参数来确定这个spring创建的对象是什么类型
System.out.println(person.toString());
}
}
标签补充:
property : 这个是对属性的进行配置,但是这种配置还有一前提,那就是需要在对象中记得些setter方法,不然ide会提示。
property的属性(简单列出两个,其实有很多在后面补充):
对于set的注入属性,还可以更加简单的,进行配置,现在只需要将配置文件增加p标签即可:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="person" class="com.xzd.test.Person" p:personname="我是张三啊" p:age="18">
bean>
beans>
然后看一下这样也可以注解的也可以完成这个配置。
有时候创建对象的时候,会赋值属性的时候会是特殊符号,当然spring也可以注入属性为特殊符号。
<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="person" class="com.xzd.test.Person">
<property name="personname">
<null/>
property>
<property name="age" value="18">property>
bean>
beans>
**
特殊符号
<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="person" class="com.xzd.test.Person">
<property name="personname">
<value>value>
property>
<property name="age" value="18">property>
bean>
beans>
如何使用****
其实属性除了是简单字符串或者数字,但是有些对象的属性,本身就是一个对象,比如这样
package com.xzd.test;
public class Person {
String personname;
int age;
public void setPersonname(String personname) {
this.personname = personname;
}
public void setAge(int age) {
this.age = age;
}
}
package com.xzd.test;
public class Student {
String school;
Person person;
// 两
public Student(String school, Person person) {
this.school = school;
this.person = person;
}
@Override
public String toString() {
return "Student={\"name\":" + person.personname + ",\"age\":" + person.age + ",\"school\":"+this.school+"}";
}
}
<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="student" class="com.xzd.test.Student">
<constructor-arg name="school" value="游戏学院">constructor-arg>
<constructor-arg name="person">
<bean id="person" class="com.xzd.test.Person">
<property name="personname" value="张三">property>
<property name="age" value="18">property>
bean>
constructor-arg>
bean>
beans>
public class test {
public static void main(String[] args) {
ApplicationContext applicationContext= new ClassPathXmlApplicationContext("personbean.xml");
Student student= applicationContext.getBean("student", Student.class);//
System.out.println(student.toString());
}
}
比如还有就是业务中service中引用dao的方法一样。一句话来说就是在一个对象类中注入一到一个对象类中 ,前面内部注入,在bean标签中套用一个bean标签,但是外部注入式两个bean标签没套用,而通过属性进行引入,所以这个是外部注入。如下演示:
package com.xzd.test.service;
import com.xzd.test.dao.SaveDao;
public class SaveService {
SaveDao saveDao;
public void setSaveDao(SaveDao saveDao) {
this.saveDao = saveDao;
}
public void save() {
System.out.println("service调用dao");
saveDao.save();
}
}
package com.xzd.test.dao;
public class SaveDao {
public void save(){
System.out.println("dao添加到数据库中了");
}
}
<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="SaveService" class="com.xzd.test.service.SaveService">
<property name="saveDao" ref="saveDao">property>
bean>
<bean id="saveDao" class="com.xzd.test.dao.SaveDao">
bean>
beans>
public class test {
public static void main(String[] args) {
ApplicationContext applicationContext= new ClassPathXmlApplicationContext("servicetest.xml");
SaveService saveService= applicationContext.getBean("SaveService", SaveService.class);//
saveService.save();
}
}
可以看结果
其实可以看出在property标签中,通过rel为name赋值,而rel的值就是创建的要引入的bean中id即可。
毕竟有些bean的属性值,还有可能是集合,比如数字,list,map等
package com.xzd.test;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class CollectionTest {
String arr[];
Map map;
Set set;
List list;
public void setArr(String[] arr) {
this.arr = arr;
}
public void setMap(Map map) {
this.map = map;
}
public void setSet(Set set) {
this.set = set;
}
public void setList(List list) {
this.list = list;
}
public void testToString(){
System.out.println("arr: "+this.arr);
System.out.println("set: "+this.set);
System.out.println("map: "+this.map);
System.out.println("list: "+this.list);
}
}
<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="collectiontest" class="com.xzd.test.CollectionTest">
<property name="arr">
<array>
<value>"a"value>
<value>"b"value>
<value>"c"value>
array>
property>
<property name="list">
<list>
<value>1value>
<value>2value>
<value>3value>
list>
property>
<property name="map">
<map>
<entry key="name" value="张三">entry>
<entry key="age" value="18">entry>
map>
property>
<property name="set">
<set>
<value>"A"value>
<value>"B"value>
set>
property>
bean>
beans>
public class test {
public static void main(String[] args) {
ApplicationContext applicationContext= new ClassPathXmlApplicationContext("collectiontest.xml");
CollectionTest collectionTest= applicationContext.getBean("collectiontest", CollectionTest.class);//
collectionTest.testToString();
}
}
package com.xzd.test;
import java.util.List;
public class CollectionTest {
List<String> list;
public void setList(List<String> list) {
this.list = list;
}
@Override
public String toString() {
return "CollectionTest{" +
"list=" + list +
'}';
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="collectiontest" class="com.xzd.test.CollectionTest">
<property name="list" ref="s_list">property>
bean>
<util:list id="s_list">
<value>老头环value>
<value>战神value>
<value>曙光2value>
util:list>
beans>
public class test {
public static void main(String[] args) {
ApplicationContext applicationContext= new ClassPathXmlApplicationContext("collectiontest.xml");
CollectionTest collectiontest= applicationContext.getBean("collectiontest", CollectionTest.class);
System.out.println(collectiontest.toString());
}
}
可以看出可以如下设置,通过util标签进行设置。
package com.xzd.test;
public class Course {
String courseName;
String teacherName;
public Course(String courseName, String teacherName) {
this.courseName = courseName;
this.teacherName = teacherName;
}
@Override
public String toString() {
return "{name:"+this.courseName+" teachername: "+this.teacherName+"}";
}
}
public class Student {
String name;
List<Course> listcourse;
public void setName(String name) {
this.name = name;
}
public void setListcourse(List<Course> listcourse) {
this.listcourse = listcourse;
}
public void testToString(){
System.out.println("name: "+this.name+"list: "+this.listcourse);
}
}
<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="student" class="com.xzd.test.Student">
<property name="name" value="张三">property>
<property name="listcourse" >
<list>
<ref bean="course1">ref>
<ref bean="course2">ref>
list>
property>
bean>
<bean id="course1" class="com.xzd.test.Course">
<constructor-arg name="courseName" value="老头环无伤通关">constructor-arg>
<constructor-arg name="teacherName" value="宫崎老贼">constructor-arg>
bean>
<bean id="course2" class="com.xzd.test.Course">
<constructor-arg name="courseName" value="v社不会数学尤其是三">constructor-arg>
<constructor-arg name="teacherName" value="亏本的v胖">constructor-arg>
bean>
beans>
public class test {
public static void main(String[] args) {
ApplicationContext applicationContext= new ClassPathXmlApplicationContext("student.xml");
Student student= applicationContext.getBean("student", Student.class);
student.testToString();
}
}