好记性不如烂笔头
相信从事java后台开发的朋友们对这个框架都不陌生吧。
Spring的核心是控制反转(IOC)和面向切面(AOP),本片博客主要是讲IOC的应用
什么是IOC,就我现在的理解就是将对象的生命周期交给了容器【如对象的创建…】
开始搭建环境【IDEA】
如图,new project的时候勾上Spring选项,然后下一步,填写好项目名称,然后确定就可以了
会自动帮你把jar下载好的
场景:创建一个User类,user包含id、name、age, 测试spring的容器是否会帮我们生成User的实例
public class User {
public int id ;
public String name ;
public int age ;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
spring-config.xml [配置文件]
property 非必须的
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--scope="prototype" 表示使用原型模式【就是非单例模式,默认的是单例模式】-->
<bean id="UserBean" class="User" scope="prototype">
<!-- 注意 name="id" value="1" 是通过类的set方法设置进去的-->
<property name="id" value="1"></property>
<property name="name" value="Spring"></property>
<property name="age" value="10"></property>
</bean>
</beans>
测试代码
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args){
//创建一个spring的IOC容器对象
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
//从IOC容器中获取Bean的实例
User user = (User) context.getBean("UserBean");
System.out.println(user.toString());
}
}
输出 User{id=1, name='Spring', age=10}
此时增加一个user增加一个Phone的类
public class Phone {
public int phoneNumber ;
public String phoneName ;
public long phonePay ;
public int getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(int phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getPhoneName() {
return phoneName;
}
public void setPhoneName(String phoneName) {
this.phoneName = phoneName;
}
public long getPhonePay() {
return phonePay;
}
public void setPhonePay(long phonePay) {
this.phonePay = phonePay;
}
@Override
public String toString() {
return "Phone{" +
"phoneNumber=" + phoneNumber +
", phoneName='" + phoneName + '\'' +
", phonePay=" + phonePay +
'}';
}
}
public class User {
public int id ;
public String name ;
public int age ;
public Phone phone ;
public Phone getPhone() {
return phone;
}
public void setPhone(Phone phone) {
this.phone = phone;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", phone=" + phone +
'}';
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--scope="prototype" 表示使用原型模式【就是非单例模式,默认的是单例模式】-->
<bean id="UserBean" class="User" scope="prototype">
<!-- 注意 name="id" value="1" 是通过类的set方法设置进去的-->
<property name="id" value="1"></property>
<property name="name" value="Spring"></property>
<property name="age" value="10"></property>
<!--注入一个Phone-->
<property name="phone" ref="PhoneBean"/>
</bean>
<bean id="PhoneBean" class="Phone">
<property name="phoneNumber" value="110"/>
<property name="phoneName" value="小米"/>
<property name="phonePay" value="2600"/>
</bean>
</beans>
测试
User{id=1, name='Spring', age=10, phone=Phone{phoneNumber=110, phoneName='小米', phonePay=2600}}
~完~