Spring
什么是Spring
Spring是一个轻量级控制反转(IoC)和面向切面(AOP)的容器框架。
控制反转——Spring通过一种称作控制反转(IoC)的技术促进了松耦合。当应用了IoC,一个对象依赖的其它对象会通过被动的方式传递进来,而不是这个对象自己创建或者查找依赖对象。你可以认为IoC与JNDI相反——不是对象从容器中查找依赖,而是容器在对象初始化时不等对象请求就主动将依赖传递给它。
面向切面——Spring提供了面向切面编程的丰富支持,允许通过分离应用的业务逻辑与系统级服务(例如审计(auditing)和事务(transaction)管理)进行内聚性的开发。应用对象只实现它们应该做的——完成业务逻辑——仅此而已。它们并不负责(甚至是意识)其它的系统级关注点,例如日志或事务支持。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5y4gE9z1-1571317342938)(C:\Users\84067\AppData\Roaming\Typora\typora-user-images\1571020127464.png)]
控制反转
什么是控制反转?
控制反转就是把创建对象的权利给了第三方,以前创建对象的权利在自己手中,现在创建对象的权利交给了Spring。
控制反转是一种通过描述(XML或注解)并通过第三方去生产或获取特定对象的方式。在Spring中实现控制反转的是IoC容器,其实现方法是依赖注入(Dependency Injection,DI)。
QuickStart
接下来我们使用Spring来创建一个简单 的Helloworld对象。
- 编写一个实体类
HelloWorld
public class HelloWorld {
private String name;
public HelloWorld(String name) {
this.name = name;
}
public HelloWorld() {
}
public void test(){
System.out.println(name);
}
}
- 在Spring的配置文件中注册Bean 这里我们文件是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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd"
>
<!--在xml中注册Bean
class是类的包名加类名
id是变量名-->
<bean class="HelloWorld" id="helloWorld"></bean>
</beans>
- 测试代码
public class MyTest {
public static void main(String[] args) {
// context就是代表Spring的容器
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
// 从容器中获取到Spring为我们创建的bean
HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");
helloWorld.test();
}
}
从这里我们看到使用了Spring之后,我们不需要在new一个对象了 ,我们获取一个对象是从容器中获取的。
Spring创建对象的方式
构造函数创建
-
默认是无参构造函数构造
-
也可以使用有参函数构造,有参函数构造主要有下面三种方式:
<!--在Spring配置里面配置beans--> <!--第一种方式,通过参数名字来构建--> <bean id="hello" class="Hello"> <constructor-arg name="name" value="echoqian"></constructor-arg> </bean> <!--第二种方式,通过下标来设置--> <bean id="hello" class="Hello"> <constructor-arg index="0" value="echoqian"></constructor-arg> </bean> <!--第三种方式 按照类型来创建--> <bean id="hello" class="Hello"> <constructor-arg type="java.lang.String" value="echoqian"></constructor-arg> </bean>
依赖注入
构造器注入
前面已经说过了
set方式注入
复杂对象的注入:
Student.java
import java.util.*;
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbies;
private Map<String, String> cards;
private Set<String> games;
private String wife;
private Properties info;
public void setName(String name) {
this.name = name;
}
public void setAddress(Address address) {
this.address = address;
}
public void setBooks(String[] books) {
this.books = books;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
public void setCards(Map<String, String> cards) {
this.cards = cards;
}
public void setGames(Set<String> games) {
this.games = games;
}
public void setWife(String wife) {
this.wife = wife;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbies=" + hobbies +
", cards=" + cards +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
Address.java
public class Address {
private String address;
public Address() {
}
public Address(String address) {
this.address = address;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="Address">
<property name="address" value="西安"></property>
</bean>
<bean id="student" class="Student" scope="prototype">
<property name="name" value="echoqian"></property>
<property name="address" ref="address"></property>
<property name="books" >
<array>
<value>book1</value>
</array>
</property>
<property name="cards">
<map>
<entry key="学生证" value="2170520028"></entry>
</map>
</property>
<property name="games">
<set>
<value>LOL</value>
<value>LOL</value>
<value>LOL</value>
</set>
</property>
<property name="hobbies">
<list>
<value>codeing</value>
<value>dancing</value>
<value>singing</value>
</list>
</property>
<property name="info">
<props>
<prop key="姓名">echoqian</prop>
<prop key="username">echoqian</prop>
<prop key="pwd">qpy939773</prop>
</props>
</property>
<property name="wife">
<null></null>
</property>
</bean>
</beans>
测试
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
}
}
注入的时候要注意一点
第一 set方法是存在的 。
第二 依赖对象必须是Spring创建好的 必须存在。
自动装配
前面我们已经看了依赖注入可以在xml中配置注入,但其实我们可以通过注解
的方式来实现自动装配。
@Autowired
使用了@Autowired
注解之后,我们不需要Set方法了,Spring会自动帮我们装配依赖,但是前提是这个依赖是Spring已经创建好的。
自动装配场景
一个人拥有一只猫和一只狗
People.java
package com.echoqian;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
//Component注解 组件 Spring会自动创建
@Component
public class People {
// 自动装配, Spring会按照ByType的方式来自动装配
@Autowired
private Cat cat;
@Autowired
private Dog dog;
public void test(){
cat.test();
dog.test();
}
}
Cat.java
package com.echoqian;
import org.springframework.stereotype.Component;
@Component
public class Cat {
private String name;
public void test(){
System.out.println(" i am a cat");
}
}
Dog.java
package com.echoqian;
import org.springframework.stereotype.Component;
@Component
public class Dog {
private String name;
public void test(){
System.out.println(" i am a dog");
}
}
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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--开启对注解的支持-->
<context:annotation-config/>
<!--扫描component组件-->
<context:component-scan base-package="com.echoqian"></context:component-scan>
</beans>
测试
import com.echoqian.People;
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");
People people = context.getBean("people", People.class);
people.test();
}
}
使用纯java的方式来配置Spring
前面我们学习到的都是基于xml来配置的Spring,现在我们可以用纯java类来配置Spring。
MyConfiguration.xml
package com.echoqian;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
//标识这是一个纯java的Spring配置文件
@Configuration
//扫描这个包下面的注解会被识别
@ComponentScan("com.echoqian")
public class MyConfiguration {
//注册bean
@Bean
public User User(){
return new User();
}
}
User.java
package com.echoqian;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class User {
@Value("echoqian")
private String name;
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
测试
import com.echoqian.MyConfiguration;
import com.echoqian.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Mytest {
public static void main(String[] args){
ApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration.class);
User user = context.getBean("user", User.class);
System.out.println(user.toString());
}
}
text.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Mytest {
public static void main(String[] args){
ApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration.class);
User user = context.getBean("user", User.class);
System.out.println(user.toString());
}
}