Spring的概念
Spring是开源的轻量级框架
1.核心:
AOP:面向切面编程,扩展功能不需要修改源代码实现
IOC:控制反转
2.一站式框架
在JavaWeb三层框架中每一层都有相对应的解决技术
Web层:SpringMVC
Service:IOC
Dao层:MyBatis
版本:4.1.3
- IOC的操作(控制反转—通过Spring管理类的创建)
-IOC入门案例
1.导入jar包
2.创建类,在类里面创建方法
建一个Person类
package pojo;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
/**
*
*这个注解相当于<bean id ="person" class = "pojo.Person"/>
*通过注解配置IOC
*/
public class Person {
/**
* @Value赋值
*
*/
@Value(value="John")
private String name;
@Value(value="20")
private int age;
private String[] hobby;
private List<String> hobby1;
public Person(String name, int age, String[] hobby, List<String> hobby1) {
super();
this.name = name;
this.age = age;
this.hobby = hobby;
this.hobby1 = hobby1;
}
public List<String> getHobby1() {
return hobby1;
}
public void setHobby1(List<String> hobby1) {
this.hobby1 = hobby1;
}
public Person(String name, int age, String[] hobby) {
super();
this.name = name;
this.age = age;
this.hobby = hobby;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public Person() {
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", hobby=" + Arrays.toString(hobby) + ", hobby1=" + hobby1
+ "]";
}
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 void run(){
System.out.println("run......");
}
}
3.创建Spring配置文件,配置要创建对象相应的类
(1).Spring核心配置文件名称和文职是不固定的,建议放在src下,名称官方建议applicationContext。xml
(2).引入schema约束
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>
(3).需要配置创建对象相应的类
在<beans></beans>标签内配置
<bean id="person" class="pojo.Person" scope="prototype"></bean>
4.测试(代码仅用于测试)
创建一个JUnit测试
代码:
package test;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.Car;
import pojo.Person;
public class IOCTest {
@Test
public void test() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Person p = (Person) context.getBean("person");
p.run();
}
}