spring框架是由于软件开发的复杂性而创建的,spring是一个轻量级的控制反转(LoC)和面向切面(AoP)的容器框架。
使用时先导入此包,这样会连带着导入其他包。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
使用方式,将实体类托管于spring容器,实体类对象的实例化交给容器来做,可动态注入对应属性的值。如:
实体类:
public class Hello {
private String str;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
@Override
public String toString() {
return "Hello{" +
"str='" + str + '\'' +
'}';
}
}
xml文件框架
<?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">
</beans>
beans.xml中创建对象:
<?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">
<bean id="hello" class="com.lijian.pojo.Hello">
<property name="str" value="spring"/>
</bean>
</beans>
通过beans.xml中的id获取对象并进行输出测试:
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Hello hello = (Hello) context.getBean("hello");
依赖注入,注入对象对应的属性值
<?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">
<bean id="address" class="com.lijian.pojo.Address">
<property name="address" value="成都"/>
</bean>
<bean id="student" class="com.lijian.pojo.Student">
<property name="name" value="李"/>
<property name="address" ref="address"/>
<property name="books">
<array>
<value>红楼梦</value>
<value>西游记</value>
<value>水浒传</value>
<value>三国演义</value>
</array>
</property>
<property name="hobbys">
<list>
<value>打球</value>
<value>写代码</value>
<value>看电影</value>
</list>
</property>
<property name="card">
<map>
<entry key="学号" value="192.168.03565"/>
<entry key="id" value="1641646464"/>
</map>
</property>
<property name="games">
<set>
<value>王者</value>
<value>lol</value>
</set>
</property>
<property name="wife">
<null/>
</property>
<property name="info">
<props>
<prop key="学号">111111</prop>
<prop key="id">2222222</prop>
</props>
</property>
</bean>
</beans>
测试
import com.lijian.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Mytest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
}
}
总结
1、 控制反转的思想是:以往的对象的创建由程序本身来控制,而反转之后,对象的创建权交给用户。
2、spring实现对象的创建及属性注入的原理。spring通过构造器创建对象,配置xml文件时,对象就已经存在且唯一。属性值的注入方式有多种,主要通过set方法注入。