由spring管理的对象可以有生命周期方法,这些生命周期方法是开发人员自定义的,方法名自定义,无参数
由spring管理的对象的类可以有初始化和销毁这两种生命周期方法,按需设计即可。
public class Person{
private String name;
private int age;
//person类的初始化方法
public void init(){
System.out.println("初始化方法");
}
public void destory(){
System.out.println("销毁方法");
}
}
<!-- init-method : 定义初始化方法,直接写上方法名称即可 destroy-method: 定义销毁方法,直接写上方法名即可 --> <beanid=""class="cn.tedu.spring.beans.Person"init-method="init"destroy-method="destory"></bean>
default-init-method
指定初始化方法和添加 default-destroy-method
指定销毁方法 <beansdefault-init-method="init"default-destroy-method="destroy"> <beanid="person"class="cn.tedu.spring.bean.Person"></bean> </bean>
set
方法 public class Person{
private String name;
private int age;
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;
}
}
<beanid="person"class="cn.tedu.spring.beans.Person"> <!-- 直接使用property配置参数 name:指定属性的字段,这个是set方法后面的单词首字母小写的值,比如SetUsername(),那么此时的name值为username value: 指定属性的值 ref : 指定前面定义的bean的id,用于设置引用类型的参数值 --> <propertyname="name"value="陈加兵"></property> <propertyname="age"value="22"></property> </bean>
setAge
,那么其中的name属性的值就是 age
。所以这个name属性的值并不是java代码中的属性名称,只不过通常在java代码中,基于代码规范,这里的name属性值也是java代码中的属性名称。 public class Address{
private String city; //城市
private String pro ; //省份
public String getCity(){
return city;
}
public void setCity(String city){
this.city = city;
}
public String getPro(){
return pro;
}
public void setPro(String pro){
this.pro = pro;
}
}
public class Person{
private String name;
private int age;
private Address address; // Address的对象作为成员变量
public Address getAddress(){
return address;
}
public void setAddress(Address address){
this.address = address;
}
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;
}
}
<!-- 创建一个Address的实例 --> <beanid="address"class="cn.tedu.spring.beans.Address"> <propertyname="city"value="无锡"></property> <propertyname="pro"value="江苏"></property> </bean> <beanid="person"class="cn.tedu.spring.beans.Person"> <propertyname="name"value="陈加兵"></property> <propertyname="age"value="22"></property> <!-- 这里的ref引用的是上面配置的Address的实例中的id值 --> <propertyname="address"ref="address"></property> </bean>
<bean id="" class="">
即可 public class Person{
private String name;
private int age;
//构造方法
public Person(String name,int age){
this.name = name;
this.age = age;
}
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;
}
}
<beanid="person"class="cn.tedu.spring.beans.Person"> <!-- name: 这个相当于index,也是指定参数列表的属性,不过这里是直接使用参数列表中的变量名 value: 为参数设置的值 index: 构造方法的参数列表的索引,从0开始 ref:引用类型的值,这里的值应该是上面已经定义好的bean的id值 --> <constructor-argindex="0"value="陈加兵"></constructor-arg> <constructor-argindex="1"value="22"></constructor-arg> </bean>
引用类型的注入
就是基本型的注入,这里不再详细的讲述 List
, Set
, Map
, Properties
set
方法 public class Message{
private List<String> cities; //城市 。List集合
private Set<String> friend; //Set集合
private Map<Integer,String> bookes; //Map集合
private Properties properties; //Properties集合
public List<String> getCities(){
return cities;
}
public void setCities(List<String> cities){
this.cities = cities;
}
public Set<String> getFriend(){
return friend;
}
public void setFriend(Set<String> friend){
this.friend = friend;
}
public Map<Integer, String> getBookes(){
return bookes;
}
public void setBookes(Map<Integer, String> bookes){
this.bookes = bookes;
}
public Properties getProperties(){
return properties;
}
public void setProperties(Properties properties){
this.properties = properties;
}
}
<beanid="message"class="cn.tedu.spring.beans.Message"> <!-- List集合的注入 --> <propertyname="cities"> <list> <value>徐州</value> <value>无锡</value> <value>常州</value> </list> </property> <!-- Set集合的注入 --> <propertyname="friend"> <set> <value>Jack</value> <value>Tom</value> <value>陈加兵</value> </set> </property> <!-- Map集合的注入 --> <propertyname="bookes"> <map> <entrykey="1001"value="java编程基础"></entry> <entrykey="1002"value="java编程思想"></entry> </map> </property> <!-- properties的集合的注入 --> <propertyname="properties"> <props> <propkey="username">root</prop> <propkey="password">root</prop> </props> </property> </bean>
@Test
public void test2(){
// spring的配置文件
String conf = "applicationContext.xml";
// 获取spring容器
AbstractApplicationContext context = new ClassPathXmlApplicationContext(
conf);
Message message=(Message) context.getBean("message");
List<String> cities=message.getCities();
Set<String> friends=message.getFriend();
Map<Integer, String> bookes=message.getBookes();
Properties properties=message.getProperties();
System.out.println(cities);
System.out.println(friends);
System.out.println(bookes);
System.out.println(properties);
context.close();
}
<util:>
) ref
指定的是配置集合的 id
使用的还是上面的 Message
类
在 resource
中需要新建一个 jdbc.properties
,我们便可以在spring的配置文件中使用 <util:properties id="" location="classpath:jdbc.properties">
自动的读取其中的值
root=root password=root url=jdbc:mysql://localhost:3306/hirbernate?useUnicode=true&characterEncoding=UTF-8 driver=com.mysql.jdbc.Driver
<util:listid="cities"> <value>徐州</value> <value>无锡</value> <value>常州</value> </util:list> <util:setid="friends"> <value>Jack</value> <value>Tom</value> <value>陈加兵</value> </util:set> <util:mapid="bookes"> <entrykey="1001"value="java编程基础"></entry> <entrykey="1002"value="java编程思想"></entry> </util:map> <!-- 引入外部的Properties文件,location指定的就是位置 --> <util:propertiesid="properties"location="classpath:jdbc.properties"></util:properties> <beanid="message"class="cn.tedu.spring.beans.Message"> <!-- List集合的注入 ref指定的上面定义的List的id --> <propertyname="cities"ref="cities"></property> <!-- Set集合的注入 --> <propertyname="friend"ref="friends"></property> <!-- Map集合的注入 --> <propertyname="bookes"ref="bookes"></property> <!-- properties的集合的注入 --> <propertyname="properties"ref="properties"></property> </bean>
private String[] names;
public String[] getNames() {
return names;
}
public void setNames(String[] names){
this.names = names;
}
<!-- 为数组赋值 --> <property name="names"> <array> <value>Alex</value> <value>Billy</value> </array> </property>
<List>
和 <array>
可以随意挑选使用,即为 List
类型的数据注入值时,既可以使用 <List>
节点,也可以使用 <array>
节点,反之为数组类型的数据值也是一样 value
直接为 ""
<propertyname="xxx"> <null/> </property>
<propertyname="xxx"> <valuetype="数据类型">值</value> </property>