1.Spring相关简介
2.Spring核心:IOC AOP
IOC:控制反转:就是对对象控制权的转移,从程序代码本身
反转到了外部容器中,通过外部容器实现对象的创建,属
性的赋值,依赖的管理。
IOC的具体实现
依赖注入(DI)
1.创建项目,导入jar包
2.定义类
3.创建Spring配置文件,编写bean
4.在测试类中测试
创建项目
导入jar包
编写Company类和Founder类
Company.java
package cn.lexed.pojo;
public class Company {
private String name;
private String product;
private int order;
private Founder fo;//name age descent
public Company() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
pu服务器托管网blic String getProduct() {
return product;
}
public void setProduct(String product) {
this.product = product;
}
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
public Founder getFo() {
return fo;
}
public void setFo(Founder fo) {
this.fo = fo;
}
@Override
public String toString() {
return "Company [name=" + name + ", product=" + product + ", order=" + order + ", fo=" + fo + "]";
}
}
Founder.java
package cn.lexed.pojo;
public class Founder {
private String name;
private int age;
private String descent;
public Founder() {
super();
}
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;
}
public String getDescent() {
return descent;
}
public void setDescent(String descent) {
this.descent = descent;
}
@Override
public String toString() {
return "Founder [name=" + name + ", age=" + age + ", descent=" + descent + "]";
}
}
编写配置文件
applicationContext.xml
编写测试类
TestSpring.java
package cn.lexed.test;
import static org.junit.Assert.*;
import org.jun服务器托管网it.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.lexed.pojo.Company;
public class TestSpring {
@Test
public void test() {
//1.创建Spring容器的对象
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
//2.使用getBean方法,getBean(配置文件中bean的id值)
Company c=(Company)ac.getBean("com");
System.out.println(c);
}
}
运行结果
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net
相关推荐: Eclipse+WTP+Tomcat开发的几点注意事项
作者fbysss关键字:eclipse,wtp,tomcat new 一个server之后,会自动建立一个目录workspace/Servers/Tomcat v5.5 Server @ localhost-config,里面的几个文件,来自tomcat的co…