Spring-介绍
一、三层架构中spring位置
二、spring一站式框架
spring框架属于容器性质,容器中装什么对象就有什么功能,所以是一站式。它不仅不排斥其他框架,还能帮其他框架管理对象。(aop支持,ioc思想,spring jdbc,aop事务,junit测试支持等)
spring搭建
一、导包
1、4个spring包
2、日志包
com.springsource.org.apache.logging-1.1.1.jar
com.springsource.org.apache.log4j-1.2.15.jar 可选
二、创建一个对象
public class User {
public User() {
System.out.println("User对象空参构造方法!!!!");
}
private String name;
private Integer age;
private Car car;
public User(String name, Car car) {
System.out.println("User(String name, Car car)!!");
this.name = name;
this.car = car;
}
public User(Car car,String name) {
System.out.println("User(Car car,String name)!!");
this.name = name;
this.car = car;
}
public User(Integer name, Car car) {
System.out.println("User(Integer name, Car car)!!");
this.name = name+"";
this.car = car;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public void init(){
System.out.println("我是初始化方法!");
}
public void destory(){
System.out.println("我是销毁方法!");
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", car=" + car + "]";
}
}
三、书写配置注册对象到容器
1、位置任意(建议放到src下),配置文件名任意(建议applicationContext.xml)
2、导入约束
<?xml version="1.0" encoding="UTF-8"?>
<beans xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd " xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- 将User对象交给spring容器管理 -->
<bean class="cn.itcast.bean.User" name="user"/></beans>
四、代码测试
public class Demo {
@Test
public void fun(){
//1 创建容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
/*//2 向容器"要"user对象
User u = (User) ac.getBean("user");
//3 打印user对象
System.out.println(u);*/
}
}