Spring第一步,先定义一个bean
新建一个java web项目。
1.下载spring4.2的jar包,地址:http://repo.spring.io/libs-release-local/org/springframework/spring/
2.将其中的.JAR包全部导入进项目中;
3.在类根目录下新建一个bean.xml文件
4.在类目录下新建一个com.hl.demo.Chinese对象。
package com.hl.demo;
public class Chinese {
public void eat() {
System.out.println("中国人对吃很有一套");
}
public void walk() {
System.out.println("中国人行如飞");
}
}
5.新建一个main方法运行对象:
package com.hl.demo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ClientTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("/bean.xml");
Chinese chinese = (Chinese) ctx.getBean("CHINESE");
chinese.eat();
chinese.walk();
}
}
6.运行会有一个报错为:
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
7.这是因为缺少commons.logging.jar包的缘故。
去官网下载:http://commons.apache.org/proper/commons-logging/download_logging.cgi
8.运行即可。
9.对新建一个Person类,其中对于类的初始化用set方法。
package com.hl.demo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
/**
* @author Administrator
* 个人基本信息
* */
public class Person{
private String name ="";//名字
private String sex = "";//性别
private String id = ""; //身份证
private int age = -1; //年龄
private List address = new ArrayList(); //联系地址
private List telphone = new ArrayList();//常用电话
private Set sets=new HashSet();
private Properties properties=new Properties();
private Map maps=new HashMap();
public Set getSets() {
return sets;
}
public void setSets(Set sets) {
this.sets = sets;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public Map getMaps() {
return maps;
}
public void setMaps(Map maps) {
this.maps = maps;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List getAddress() {
return address;
}
public void setAddress(List address) {
this.address = address;
}
public List getTelphone() {
return telphone;
}
public void setTelphone(List telphone) {
this.telphone = telphone;
}
}
10.在spring bean中初始化以上类属性,对于set方式的初始化在bean中采用
深圳
珠海
15898430298
15888888888
第一个set
第二个set
第三个set
value1
value2
value3
11.在com.hl.demo.Chinese中添加一个com.hl.demo.Person对象。红色标记之。
package com.hl.demo;
public class Chinese implements Human{
private Person personBseInfo = null;
public Person getPersonBseInfo() {
return personBseInfo;
}
public void setPersonBseInfo(Person personBseInfo) {
this.personBseInfo = personBseInfo;
}
@Override
public void eat() {
System.out.println("中国人对吃很有一套");
}
@Override
public void walk() {
System.out.println("中国人行如飞");
}
@Override
public void sleep() {
System.out.println("中国人勤于工作极少睡觉...");
}
}
深圳
珠海
15898430298
15888888888
第一个set
第二个set
第三个set
value1
value2
value3
13.采用构造函数初始化bean对象,有无set方式没有关系,新建一个com.hl.demo.Constructor类,如下:
package com.hl.demo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Constructor {
private String str = "";
private int i = -1;
private List list = new ArrayList();
private Set sets=new HashSet();
private Properties properties=new Properties();
private Map maps=new HashMap();
public Constructor(String str,int i,List list,Set set, Properties prop , Map maps){
this.str = str;
this.i = i;
this.list = list;
this.sets = set;
this.properties = prop;
this.maps = maps;
}
public void setStr(String str) {
this.str = str;
}
public void setList(List list) {
this.list = list;
}
public void setSets(Set sets) {
this.sets = sets;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public void setMaps(Map maps) {
this.maps = maps;
}
public void setI(int i) {
this.i = i;
}
public String getStr() {
return str;
}
public int getI() {
return i;
}
public List getList() {
return list;
}
public Set getSets() {
return sets;
}
public Properties getProperties() {
return properties;
}
public Map getMaps() {
return maps;
}
}
14、bean.xml中初始化bean
深圳...
珠海...
第一个set
第二个set
第三个set
value1
value2
value3
15,在bean中初始化类时,无论是否有,
如果重写了构造函数,则必须用
没有构造函数则可以使用