导入依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
spring官方文档地址:https://spring.io/projects/spring-framework
一. Container Overvie 容器概述
该org.springframework.context.ApplicationContext
接口代表Spring IoC容器,并负责实例化,配置和组装Bean。容器通过读取配置元数据来获取有关要实例化,配置和组装哪些对象的指令。配置元数据以XML,Java批注或Java代码表示。它使您能够表达组成应用程序的对象以及这些对象之间的丰富相互依赖关系。
二.HelloSpring
实例
package com.learn.spirng;
/**
* @author wb-zy215449
*/
public class Hello {
private String name;
public Hello() {
}
public Hello(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Hello{" +
"name='" + name + '\'' +
'}';
}
}
单元测试
@Test
public void testHello(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
Hello hello = applicationContext.getBean("hello", Hello.class);
System.out.println(hello);
}
实例初始化方法:
(1)无参构造方法
<?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="hello" class="com.learn.spirng.Hello"/>
</beans>
(2)有参构造(根据参数名)
<bean id="hello" class="com.learn.spirng.Hello">
<constructor-arg name="name" value="zhangsan"/>
</bean>
(3)有参构造(根据参数类型)
<bean id="hello" class="com.learn.spirng.Hello">
<constructor-arg type="java.lang.String" value="lisi"/>
</bean>
(4)根据参数下标
<bean id="hello" class="com.learn.spirng.Hello">
<constructor-arg index="0" value="wangwu"/>
</bean>
(5)属性注入
<bean id="hello" class="com.learn.spirng.Hello">
<property name="name" value="maliu"/>
</bean>
三. Bean的自动装配
自动装配是spring满足bean依赖的一种方式,spring会在上下文寻找并自动给bean装配属性
在spring中有三种装配的方式:
- 在xml中显示配置
- 在java中显示配置
- 隐式的自动装配bean【重要】
环境:
package com.learn.pojo;
public class Persion {
private Cat cat;
private Dog dog;
private String name;
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Persion{" + "cat=" + cat +", dog=" + dog +", name='" + name + '\'' + '}';
}
}
1.byName自动装配
<bean class="com.learn.pojo.Cat"/>
<bean class="com.learn.pojo.Dog"/>
<bean id="persion" class="com.learn.pojo.Persion" autowire="byName">
<property name="name" value="zhangsan"/>
</bean>
2.byType自动装配
<bean class="com.learn.pojo.Cat"/>
<bean class="com.learn.pojo.Dog"/>
<bean id="persion" class="com.learn.pojo.Persion" autowire="byType">
<property name="name" value="zhangsan"/>
</bean>
注 : byName的时侯,需保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致
byType的时候,需保证所有bean的class唯一,并且这个bean需要和自动注入的属性的类型一致
3.使用注解实现自动配置
jdk1.5 支持的注解 spring2.5 支持的注解
使用须知:
- 导入约束:context约束
- 配置注解的支持
<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/> </beans>
package com.learn.pojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class Persion {
// @Autowired(required = false)
@Autowired
private Cat cat;
@Autowired
@Qualifier(value = "dog222")
private Dog dog;
private String name;
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Persion{" + "cat=" + cat +", dog=" + dog +", name='" + name + '\'' + '}';
}
}
<context:annotation-config/>
<bean id="cat111" class="com.learn.pojo.Cat"/>
<bean id="dog222" class="com.learn.pojo.Dog"/>
<bean id="dog333" class="com.learn.pojo.Dog"/>
<bean id="persion" class="com.learn.pojo.Persion"/>
测试代码:
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
Persion persion = context.getBean("persion", Persion.class);
@Autowired
可以直接在属性上使用,也可以在set方式使用。 使用Autowired我们可以不用编写set方法,前提是自动装配的属性在IOC容器中存在,且名字符合byName
@Nullable 字段注解,说明这个字段可以为null @Autowired(required = false) 这个字段显示注解为false 表示该属性可以为null
如果@Autowired自动装配的环境复杂,自动装配无法通过一个Autowired完成时,我们可以使用@Qualifier(value = "***")配合Autiwired使用,指定一个唯一的bean注入。
四:使用注解开发
在spring4 之后,要使用注解开发,必须保证aop包导入
1.Bean
2.属性注入
package com.learn.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class User {
private String name;
public String getName() {
return name;
}
@Value("zhangsan")
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" + "name='" + name + '\'' + '}';
}
}
3.衍生的注解
@Component注解有几个衍生的注解,在web开发中,会按照mvc三层架构分层
- dao @Repository
- service @Service
- controller @Controller
这四个注解功能一致,都代表将某个类注入spring容器
4.自动装配置
- @Autowired 自动装配,通过类型,名字 (若Autowired不能唯一自动装配上属性,需配合@Qualifier(value="***"))
- @Nullable 说明自动可weinull
- Resoure 自动装配,通过名字,类型
5.作用域
package com.learn.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope(value = "prototype")
public class User {
private String name;
public String getName() {
return name;
}
@Value("zhangsan")
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" + "name='" + name + '