首先我们要有一个实体类,属性都以包装类命名(int写成Integer),设置注入必有set\get访问器,构造注入必有构造方法,
public class Student {
Integer id;
String name;
Integer age;
Boolean sex;
Date Brithday;
public Student() {
super();
}
public Student(Integer id, String name, Integer age, Boolean sex, Date brithday) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
Brithday = brithday;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
//其他属性构造方法......
------------------设置注入--------------------------
{
<bean id="stu" class="emtity.Student">
<property name="id" value="15"></property>
<property name="name" value="小明"></property>
<property name="age" value="19"></property>
<property name="brithday" value="1999-12-12"></property>
</bean>//基本类型赋值
<bean id="userdao" class="dao.UserDaolmpl"> </bean>
<bean id="userservice" class="service.UserServicelmpl">
<property name="student" ref="stu"></property>
</bean>//把上面id="stu"赋值后的对象赋值给 id="userservice"类内为name="student"属性
}
------------------构造注入--------------------------
<bean id="stu2" class="emtity.Student">
<constructor-arg index="0" value="1"/>
<constructor-arg index="1" value="小明"/>
<constructor-arg index="2" value="15"/>
<constructor-arg index="3" value="true"/>
<constructor-arg index="4" value="1999-12-12"/>
</bean>
一个constructor-arg 相当于一个属性 index为参数下标 value为值 type可以设置类型(用的不多)
------------------p命名空间注入--------------------------
<bean id="stu3" class="emtity.Student" p:id="2" p:name="小张" p:age="20"></bean>
<bean id="userservice" class="service.UserServicelmpl" p:dao-ref="stu3"></bean>
xmlns:p="http://www.springframework.org/schema/p" //在头部添加一句话
------------------集合赋值--------------------------
list\set\map\Properties\Arrays赋值方法
<bean id="stu4" class="emtity.Student">
<property name="list">
<list>
<value>List1</value>
<value>List2</value>
<ref bean="stu"/>
</list>
</property>
<property name="set">
<set>
<value>set1</value>
<value>set2</value>
<ref bean="stu"/>
</set>
</property>
<property name="str">
<array>
<value>Array1</value>
<value>Array2</value>
</array>
</property>
<property name="map">
<map>
<entry key="001" value="张三"></entry>
<entry key="002" value="张四"></entry>
<entry key="003" value-ref="stu"></entry>
</map>
</property>
<property name="props">
<props>
<prop key="004">张五</prop>
<prop key="005">张六</prop>
</props>
</property>
</bean>
------------------单例模式--------------------------
在Spring框架内创建的对象默认为单例模式,运行过程中只要是同一id的对象 操作的都是一对象
在bean标签内添加 scope=“prototype”,解放单例模式,每次拿到的对象都是不同的
<bean id="stu" class="emtity.Student">
Student stu=(Student)factory.getBean("stu");
Student stu2=(Student)factory.getBean("stu2");
System.out.println(stu==stu2);//结果为true
<bean id="stu" class="emtity.Student" scope="prototype">
Student stu=(Student)factory.getBean("stu");
Student stu2=(Student)factory.getBean("stu2");
System.out.println(stu==stu2);//结果为false
------------------对象声明周期--------------------------
对象创建后不会主动的销毁,想在创建调用后销毁则在bean标签内的init-method="" destroy-method=""为创建前执行,销毁前执行,ConfigurableApplicationContext类创建对象,最后使用registerShutdownHook();方法销毁
----- entity.Student类
public void init() {
System.out.println("init");
}
public void destroy() {
System.out.println("destroy");
}
----- ApplicationContext.xml
<bean id="stu" class="emtity.Student" init-method="init" destroy-method="destroy">
<property name="id" value="15"></property>
<property name="name" value="小明"></property>
<property name="age" value="19"></property>
<property name="brithday" value="1999-12-12"></property>
</bean>
-----test测试类
BeanFactory factory=new ClassPathXmlApplicationContext("ApplicationContext.xml");
Student stu=(Student)factory.getBean("stu");
System.out.println(stu.getName());
输出 init > 小明
-----销毁对象使用
ConfigurableApplicationContext factory=new ClassPathXmlApplicationContext("ApplicationContext.xml");
Student stu=(Student)factory.getBean("stu");
System.out.println(stu.getName());
factory.registerShutdownHook();
输出 init > 小明 > destroy
------------------反射方式复制对象--------------------------
Student s = new Student(131, "小张", 13,false, new Date());
Student s2=(Student)copyForObject(s);
private static Object copyForObject(Object obj) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
//获得原有对象的class信息
Class clazz=obj.getClass();
//获得原有对象的无参构造方法
Constructor constructor=clazz.getConstructor(new Class[] {});//参数列表给一个空数组
//通过无参构造方法创建新对象 target为返回对象
Object target = constructor.newInstance(new Object[] {});
//获取原对象的属性
Field[] fields=clazz.getDeclaredFields();
//每个属性有set\get两个方法,并且有一定规律,
for(Field f:fields) {
//获得set\get方法名称=获取属性名.字符串截取(截取从一位开始前开始到第二位前结束,包前不包后)
String setmethodName="set"+f.getName().substring(0,1).toUpperCase()+f.getName().substring(1);
String getmethodName="get"+f.getName().substring(0,1).toUpperCase()+f.getName().substring(1);
//查询get 属性名 参数(为空)
Method getMethod = clazz.getDeclaredMethod(getmethodName, new Class[] {});
//查询set 属性名 参数类型
Method setMethod = clazz.getDeclaredMethod(setmethodName,f.getType());
//调用原有对象的get方法 赋给新对象的set方法
Object result=getMethod.invoke(obj, new Object[] {});
//将属性通过set方法赋值给新对象
setMethod.invoke(target, result);
}
return target;
}