参考文献:http://blog.csdn.net/lzm18064126848/article/details/48196513
Spring实现了输入不同的字符串返回不同的新对象的功能,而且这些映射写在xml文件中
1.自动配置方式
没有addSpringCapabilities?
右击web项目——properties——Myeclipse-----ProjectFacets——勾选上Spring----ok
然后
变成了
项目图标的J变成了S,这说明我的项目已经支持Spring了么?
代码与手动配置的代码是一致的的
2.手动配置方式
2.1 添加Spring的jar包
将Spring-framwork压缩包libs目录下的所有jar包和
Struts压缩包lib下的commoms-logging包添加到myeclipse
2.2配置文件和代码
①applicationContext.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<beanid="northMan"class="demo.Northperson"></bean>
<beanid="southMan"class="demo.SouthPerson"></bean>
</beans>
②Person.java
package demo;
public interfacePerson {
void eat();//定义抽象的吃方法
voiddrink();//定义抽象的喝方法
}
③Northperson.java
package demo;
public classNorthperson implements Person{
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("北方人喜欢吃面食");
}
@Override
public void drink() {
// TODO Auto-generated method stub
System.out.println("北方人喜欢喝酒");
}
}
④SouthPerson
package demo;
public classSouthPerson implements Person{
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("南方人喜欢吃饭");
}
@Override
public void drink() {
// TODO Auto-generated method stub
System.out.println("南方人喜欢喝茶");
}
}
⑤Test
package demo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public classTest {
publicstaticvoidmain(String[]args){
ApplicationContext ac=newFileSystemXmlApplicationContext("src/applicationContext.xml");//利用文件系统查询applicationContext.xml配置文件
Northperson n=(Northperson)ac.getBean("northMan");
n.eat();
n.drink();
SouthPerson s=(SouthPerson)ac.getBean("southMan");
s.eat();
s.drink();
}
}