1.maven依赖.pom.xml文件
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.9.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
2.Spring IoC容器和Bean简介
IoC也称为依赖注入(DI)。在此过程中,对象仅通过构造函数参数,工厂方法的参数或在构造或从工厂方法返回后在对象实例上设置的属性来定义其依赖项(即,与它们一起使用的其他对象) 。然后,容器在创建bean时注入那些依赖项。该过程从根本上讲是通过使用类的直接构造或诸如服务定位器模式之类的机制来控制其依赖关系的实例化或位置的bean本身的逆过程(因此,其名称为Control Inversion)。
3.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="hello" class="com.kuang.pojo.Hello">
<property name="str" value="Spring"></property>
</bean>
</beans>
<!--使用Spring来创建对象,Spring这些都称为Bean
类型 变量名 =new 类型();
Hello hello=new Hello();
id = 变量名
class =new 的对象
property 相当于给对象中的属性设置一个值
-->
测试类
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
4.有参数构造函数创建
1.下标赋值-索引
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg index="0" value="7500000"/>
<constructor-arg index="1" value="42"/>
</bean>
2.通过类型创建
//不建议使用
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg type="int" value="7500000"/>
<constructor-arg type="java.lang.String" value="42"/>
</bean>
3.参数名赋值
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg ref="name" value="name"/>
<constructor-arg ref="sex" value="sex"/>
</bean>
5.Spring配置
别名
<!-- 别名,如果添加了别名也可以通过别名获取到这个对象-->
<alias name="mysql" alias="daoMysql"></alias>
Bean的配置
<!-- id:bean的唯一标识符,也就是相当于我们学的对象名-->
<!-- class:bean对象所对应全限定名:包名+类型-->
<!-- name: 也是别名,可以同时多个取名-->
<bean id="mysql" class="com.kuang.dao.UserDaoMysql" name="m1 m2,m3;m4"></bean>
<bean id="UserServiceImpl" class="com.kuang.service.UserServiceImpl">
<property name="userDao" ref="m1"></property>
</bean>
import
一般用于团队开发,他可以将多个配置文件,导入合并为一个 假设这个项目由多人开发,这几个人负责不同的类开发,不同的类需要注册在不同的bean中,我们可以利用import将所有的bena.xml合并为一个总的
-
applicationContext.xml
<import resource="beans02.xml"/>
<import resource="beans.xml"/>
使用时只需要使用总的配置就行了,也就是 applicationContext.xml这个总文件
6.DI注入依赖
1.构造方法注入
2.set注入依赖
<?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="com.kuang.pojo.Address">
<property name="address" value="西安"></property>
</bean>
<bean id="student" class="com.kuang.pojo.Student">
<!-- 普通值注入 value-->
<property name="name" value="张三"></property>
<!-- Bean注入-->
<property name="address" ref="address"></property>
<!-- 数组注入-->
<property name="books" >
<array>
<value>西游记</value>
<value>水浒传</value>
<value>红楼梦</value>
<value>三国演义</value>
</array>
</property>
<!-- list注入 -->
<property name="hobbys">
<list>
<value>听歌</value>
<value>看电影</value>
</list>
</property>
<!-- map注入-->
<property name="card">
<map>
<entry key="身份证" value="234568888777666655"></entry>
<entry key="银行卡" value="46778794647587876"></entry>
</map>
</property>
<!-- set注入-->
<property name="games">
<set>
<value>LOL</value>
<value>王者荣耀</value>
</set>
</property>
<!-- null注入-->
<property name="wife">
<null></null>
</property>
<!-- Properties注入 -->
<property name="infor">
<props>
<prop key="学号">2019220341</prop>
<prop key="姓名">张三</prop>
<prop key="性别">男</prop>
</props>
</property>
</bean>
</beans>
测试
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties infor;
//System.out.println(student.toString());
//测试结果
// Student{name='张三',
// address=Address{address='西安'},
// books=[西游记, 水浒传, 红楼梦, 三国演义],
// hobbys=[听歌, 看电影],
// card={身份证=234568888777666655,
// 银行卡=46778794647587876},
// games=[LOL, 王者荣耀],
// wife='null',
// infor={学号=2019220341, 性别=男, 姓名=张三}}
3.其他注入方法
我们可以使用c命名空间和p命名空间注入 解释:
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- p命名空间注入,可以直接注入属性值:property-->
<bean id="user" class="com.kuang.pojo.User" p:name="张三" p:age="18"></bean>
<!-- c命名空间注入,通过构造器注入:construct-args-->
<bean id="user1" class="com.kuang.pojo.User" c:name="李四" c:age="18"></bean>
</beans>
测试:
public class UserTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
User user = (User) context.getBean("user1");
System.out.println(user);
}
}
注意点:p命名空间和c命名空间不能直接使用,需要导入约束!
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
4.Bean的作用域
1.单例模式(Spring默认机制)
<bean id="accountService" class="com.something.DefaultAccountService" scope="singleton"/>
2.原型模式:每次从容器中get的时候,都会产生一个新对象!
<bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>
3.其余request、session、application、这些个只能在web开发中使用到
5.Bean的自动装配
-
自动装配是Spring满足bean依赖的一种方式
-
Spring会在上下文中自动寻找,并自动给bean装配属性 在Spring中三种装配方式 1.在xml中显示配置 2.在Java中显示配置 3.阴式的自动装配bean【重点】
测试
public class Mytest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
People people = (People) context.getBean("people");
people.getDog().shout();
people.getCat().shout();
}
}
环境搭配:一个人有2个宠物 原方法
byName自动装配
byType自动装配
小结: byName自动装配的时候,需要保证所有的bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致! byType自动装配的时候,需要保证所有的bean的class唯一,并且这个bean需要和自动注入的属性的类型一致!
6.注解实现自动装配
要使用注解须知: 1.导入约束:context约束 2.配置注解支持:context:annotation-config/
<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>
@Autowired
直接在属性上使用即可!也可以在set方法上使用 使用 @Autowired我们可以不用编写set方法了,前提是你这个自动装配的属性在IOC(Spring)容器中存在,且符合名字byName!
public class People {
@Autowired
private Cat cat;
@Autowired
private Dog dog;
private String name;
}
<?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/>
<bean id="dog" class="com.kuang.pojo.Dog"/>
<bean id="cat" class="com.kuang.pojo.Cat"/>
<bean id="people" class="com.kuang.pojo.People"/>
</beans>
科普:
@Nullable 字段标记了这个注解,说明这个字段可以为NULL;
如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解【@Autowired】完成的时候,我们可以使用@Qualifier(value = "xxx")去配置@Autowired的使用,指定一个唯一的bean对象注入
public class People {
@Autowired
@Qualifier(value = "cat1")
private Cat cat;
@Autowired
@Qualifier(value = "dog1")
private Dog dog;
private String name;
}
<context:annotation-config/>
<bean id="dog" class="com.kuang.pojo.Dog"/>
<bean id="dog1" class="com.kuang.pojo.Dog"/>
<bean id="cat" class="com.kuang.pojo.Cat"/>
<bean id="cat1" class="com.kuang.pojo.Cat"/>
<bean id="people" class="com.kuang.pojo.People"/>
@Resource注解
public class People {
@Resource(name = "cat1")
private Cat cat;
@Resource
private Dog dog;
private String name;
}
bean.xml同上
小结: @Resource和@Autowired的区别:
-
@Autowired:自动装配通过类型。名字
-
通过byType的方式实现,而且必须要求对象存在!【常用】 如果不能唯一自动装配上属性,则需要通过@Qualifier(value = "xxx")去配置
-
@Nullable 字段标记了这个注解,说明字段可以为null
-
@Resource自动装配名字,类型。 默认通过byName方式实现,如果找不到名字,则通过byType方式实现!如果都找不到就报错【常用】
7.使用注解开发
在Spring4之后使用注解开发必须导入apo包 使用注解导入context约束
-
bean
-
属性如何注入 @Component 注解开发 加入指定的包
<!--指定扫描的包,这个包的注解就会生效--> <context:component-scan base-package="com.kuang.pojo"></context:component-scan> //这个注解相当于<bean id="user" class="com.kuang.pojo.User"></bean> @Component public class User { public String name="张三"; }
@Value
@Component
public class User {
@Value("李四")
public String name;
} 相当于
<bean id="user" class="com.kuang.pojo.User">
<property name="name" value="李四"></property>
</bean>
-
衍生的注解 @Component有几个衍生的注解,在web开发中,会按照mvc三层架构分层 dao :[@Repository] service:[@Service] controller:[@Controller] 这四个功能都是一样的,代表将类注册到Spring中,注册bean
-
自动装配
@Autowired:自动装配通过类型。名字 @Nullable 字段标记了这个注解,说明字段可以为null @Resource自动装配名字,类型。
-
作用域 @Scope 表类型
-
小结 xml与注解 : xml更加万能,适用于任何场所!维护更加简单 : 注解不是自己的类使用不了,维护相对复杂 xml与注解最佳实践 : xml用来管理bean : 注解只负责完成属性注入 : 在使用过程中,想要注解生效,就要开启注解支持
<!--指定扫描的包,这个包的注解就会生效--> <context:component-scan base-package="com.kuang"></context:component-scan> <context:annotation-config/>