Spring的简单介绍
- Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson创建。简单来说,Spring是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架,Spring 框架是一个分层架构,由 7 个定义良好的模块组成。Spring模块构建在核心容器之上,核心容器定义了创建、配置和管理 bean 的方式。
- Spring框架即以 interface21 框架为基础。
- Spring框架:
3.1 轻量级
3.2 面向切面
3.3 控制翻转 (IOC)【inversion of Control】:类的实例由容器的ClassPathXmlApplicationContext创建。
3.4 Spring 包含并管理对象的创建和生命周期,在意义上,它是一个容器。
3.5 框架:在Spring中,应用对象被声明式地组合,典型地是在一个XML文件里。Spring也提供了很多基础功能(事务管理、持久化框架集成等等),将应用逻辑的开发留给了你。 - Spring 优点:
4.1.低侵入式设计,代码污染极低。
4.2.独立于各种应用服务器,基于Spring框架的应用,可以真正实现Write Once,Run Anywhere的承诺。
4.3.Spring的DI机制降低了业务对象替换的复杂性,提高了组件之间的解耦。
4.4.Spring的AOP支持允许将一些通用任务如安全、事务、日志等进行集中式管理,从而提供了更好的复用。
4.5.Spring的ORM和DAO提供了与第三方持久层框架的良好整合,并简化了底层的数据库访问。
4.6.Spring并不强制应用完全依赖于Spring,开发者可自由选用Spring框架的部分或全部。
Spring的简单使用
- 首先在maven中找到Spring的依赖:https://mvnrepository.com/artifact/org.springframework/spring-webmvc
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
- 实体类
package com.baidu.pojo;
public class Hello {
private int age;
private String sex;
private String name;
public Hello(String name) {
System.out.println("有参构造只有name的");
this.name = name;
}
public Hello(int age, String sex, String name) {
System.out.println("有参构造全部内容");
this.age = age;
this.sex = sex;
this.name = name;
}
public Hello(){
System.out.println("无参构造");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
System.out.println("通过set方法设置name");
}
@Override
public String toString() {
return "Hello{" +
"age=" + age +
", sex='" + sex + '\'' +
", name='" + name + '\'' +
'}';
}
}
- 创建对应的bean.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"
xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--通过无参构造和set方法创建对象-->
<bean id="hello1" class="com.baidu.pojo.Hello">
<property name="name" value="Spring"></property>
</bean>
<!--通过有参构造创建对象,最后通过set重新设置name属性-->
<bean id="hello2" class="com.baidu.pojo.Hello">
<constructor-arg index="0" value="18"/>
<constructor-arg index="2" value="girl"/>
<constructor-arg index="1" value="郝一凡1"/>
<property name="name" value="郝一凡6"/>
</bean>
<!--通过有参构造创建对象-->
<bean id="hello3" class="com.baidu.pojo.Hello">
<constructor-arg value="18"/>
<constructor-arg value="girl2"/>
<constructor-arg value="郝一凡3"/>
</bean>
<!--通过有参构造创建对象-->
<bean id="hello4" class="com.baidu.pojo.Hello">
<constructor-arg name="age" value="19"/>
<constructor-arg name="name" value="郝一凡4"/>
<constructor-arg name="sex" value="gril4"/>
</bean>
<!--通过参数的数据类型创建对象-->
<bean id="hello5" class="com.baidu.pojo.Hello">
<constructor-arg type="java.lang.String" value="郝一凡5"/>
</bean>
<!--给创建的对象起别名-->
<alias name="hello5" alias="hellohello"/>
</beans>
- 测试类
package com.baidu.pojo;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloTest {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
Hello hello1 = (Hello) context.getBean("hello1");
System.out.println(hello1.toString());
}
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
Hello hello1 = (Hello) context.getBean("hello2");
System.out.println(hello1.toString());
}
@Test
public void test3(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
Hello hello1 = (Hello) context.getBean("hello3");
System.out.println(hello1.toString());
}
@Test
public void test4(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
Hello hello1 = (Hello) context.getBean("hello4");
System.out.println(hello1.toString());
}
@Test
public void test5(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
Hello hello1 = (Hello) context.getBean("hellohello");
System.out.println(hello1.toString());
}
}
我这里测试的是test5
6. bean.xml 中的import标签
<!--
classpath*:会在所有文件中去找,效率低,不建议使用
classpath:在此项目中找寻,效率高,建议使用
file:需要找到文件的绝对路径,不推荐使用
http:需要找到文件的网络路径,不推荐使用
-->
<import resource="classpath*:"/>
<import resource="classpath:"/>
<import resource="file:"/>
<import resource="http:"/>
依赖注入DI
- 创建实体类
package com.baidu.pojo;
import java.util.*;
public class User {
private String name;
private int age;
private Address address;
private String[] hobbys;
private Set<String> books;
private List<String> names;
private Map<String,String> users;
private Properties properties;
private User girlFriend;
public User() {
}
public User(String name, int age, Address address, String[] hobbys, Set<String> books, List<String> names, Map<String, String> users, Properties properties, User girlFriend) {
this.name = name;
this.age = age;
this.address = address;
this.hobbys = hobbys;
this.books = books;
this.names = names;
this.users = users;
this.properties = properties;
this.girlFriend = girlFriend;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", address=" + address +
", hobbys=" + Arrays.toString(hobbys) +
", books=" + books +
", names=" + names +
", users=" + users +
", properties=" + properties +
", girlFriend=" + girlFriend +
'}';
}
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 Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getHobbys() {
return hobbys;
}
public void setHobbys(String[] hobbys) {
this.hobbys = hobbys;
}
public Set<String> getBooks() {
return books;
}
public void setBooks(Set<String> books) {
this.books = books;
}
public List<String> getNames() {
return names;
}
public void setNames(List<String> names) {
this.names = names;
}
public Map<String, String> getUsers() {
return users;
}
public void setUsers(Map<String, String> users) {
this.users = users;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public User getGirlFriend() {
return girlFriend;
}
public void setGirlFriend(User girlFriend) {
this.girlFriend = girlFriend;
}
}
package com.baidu.pojo;
public class Address {
private String name;
public Address() {
}
public Address(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Address{" +
"name='" + name + '\'' +
'}';
}
}
- 配置对应的bean.xml文件
<bean id="user" class="com.baidu.pojo.User">
<!--普通注入-->
<property name="name" value="郝一凡"/>
<!--引入注入-->
<property name="address" ref="address"/>
<property name="age" value="18"/>
<!--set注入-->
<property name="books">
<set>
<value>西游记</value>
<value>三国演义</value>
<value>红楼梦</value>
<value>水浒传</value>
</set>
</property>
<!--空值注入-->
<property name="girlFriend">
<null/>
</property>
<!--数组注入-->
<property name="hobbys">
<array>
<value>电影</value>
<value>音乐</value>
<value>游戏</value>
</array>
</property>
<!--list注入-->
<property name="names">
<list>
<value>王者荣耀</value>
<value>刺激战场</value>
<value>绝地求生</value>
</list>
</property>
<!--map注入-->
<property name="users">
<map>
<entry key="name" value="郝一龙"/>
<entry key="age" value="14"/>
<entry key="sex" value="boy"/>
</map>
</property>
<!--properties注入-->
<property name="properties">
<props>
<prop key="时间">2019-07-26</prop>
</props>
</property>
</bean>
c命名空间注入和p命名空间注入
这个的使用要在bean.xml文件的头目录里面加上两行语句
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
<bean id="address1" class="com.baidu.pojo.Address" p:name="陕西省"/>
<bean id="address2" class="com.baidu.pojo.Address" c:name="宝鸡市"/>