<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.3.12version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
dependencies>
优点:
1.Spring是一个开源的免费的框架(容器)!
2.Spring是一个轻量级、非入侵式的框架!
3.控制反转(IOC)、面向切面编程(AOP)!
4.支持事物的处理,对框架整合的支持!
总结:Spring就是一个轻量级的控制反转(IOC)和面向切面编程(AOP)的框架!
Spring Boot:
Spring Cloud:
现在大多数公司都在用Spring Boot 进行快速开发,学习Spring Boot的前提需要,掌握Spring及SpringMVC
弊端:发展太久,违背了设计理念,配置设计繁琐,人称:配置地狱
1.
<bean id="user" class="com.xing.pojo.User">
<constructor-arg index="0" value="kexing">constructor-arg>
bean>
<alias name="user" alias="asdavasvasvas">alias>
5.
<bean id="userT" class="com.xing.pojo.User" name="user2">
这个import,一般用于团队开发使用,他可以将多个配置文件,导入合并伟一个假设,现在项目有多个人开发,这三个人复制不同的类开发,不同的类需要注册在不桐的bean中 用import将所有人的beans.xml合并成一个总的
使用的时候,直接使用总的配置就可以了
<import resource="beans.xml">import>
<import resource="beans2.xml">import>
<import resource="beans3.xml">import>
前面已经说过了
↑IOC创建对象方法
依赖注入:Set注入!
依赖:bean对象的创建依赖于容器!
注入:bean对象中的所有属性,由容器来注入!
【环境依赖】
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
2.真实测试环境
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private Properties info;
private String wife;
<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="student" class="com.xing.pojo.Student">
<property name="name" value="abc">property>
bean>
beans>
4.测试类
public class MyTests {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext ("beans.xml");
Student student =(Student) context.getBean("student");
System.out.println(student.getName());
}
}
5.完善信息注入
<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="student" class="com.xing.pojo.Student">
<property name="name" value="abc">property>
<property name="address" ref="address">
property>
<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="12312312321">entry>
<entry key="银行卡" value="45645645645">entry>
map>
property>
<property name="games">
<set>
<value>lolvalue>
<value>cocvalue>
<value>bobvalue>
set>
property>
<property name="wife">
<null>null>
property>
<property name="info">
<props>
<prop key="root">rootprop>
<prop key="url">urlprop>
<prop key="password">passwordprop>
props>
property>
bean>
<bean id="address" class="com.xing.pojo.Address">
<property name="address" value="北京">property>
bean>
beans>
<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">
<bean id="user" class="com.xing.pojo.User" p:name="kobe" p:age="26">bean>
<bean id="user2" class="com.xing.pojo.User" c:age="18" c:name="curry">bean>
beans>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rWPAV8Gd-1636552159843)(C:\Users\Star\AppData\Roaming\Typora\typora-user-images\image-20211110165818080.png)]
1.单例模式(Spring默认机制)
<bean id="user2" class="com.xing.pojo.User" c:age="18" c:name="curry" scope="singleton">bean>
2.原型模式:每次从容器中get的时候,都会产生一个新对象!
<bean id="user2" class="com.xing.pojo.User" c:age="18" c:name="curry" scope="prototype">bean>
3.其余的request、session、applicaton、这些个只能在web开发中使用到!
在Spring中有三种装配的方式
<bean id="people" class="com.xing.po.People" autowire="byName">
<property name="name" value="人">property>
bean>
<bean id="people" class="com.xing.po.People" autowire="byType">
<property name="name" value="人">property>
bean>
小结:
jdk1.5支持的注解,Spring2.5就支持注解了!!!
要使用注解须知:
导入约束
<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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<context:annotation-config>context:annotation-config>
beans>
@Autowired
直接在类上的属性注释即可,也可以在set方式上使用!
使用Autowirde我们可以不用编写Set方法了,前提是你这个自动装配的属性在IOC(Spring)中存在,且符合名字ByName!
科普:
@Nullable 字段标记了这个注释,说明这个字段可以为Null
@Documented
public @interface Autowired {
boolean required() default true;
}
测试代码
public class People {
//如果显示定义Autowired的required属性为false,说明这个对象可以为Null,否则不可以为空
@Autowired(required = false)
private Cat cat;
@Autowired
private Dog dog;
private String name;
如果@Autowired自动装配环境比较复杂,自动装配注解在无法通过一个注解【@Autowired】来实现时,我们可以通过@Qualifier(value=“xxx”) 来配合@Autowired的使用,指定一个唯一的bean对象注入
public class People {
//如果显示定义Autowired的required属性为false,说明这个对象可以为Null,否则不可以为空
@Autowired
private Cat cat;
@Autowired
@Qualifier(value = "doggg")
private Dog dog;
private String name;
@Resource注解
import javax.annotation.Resource;
public class People {
@Resource
private Cat cat;
@Resource
private Dog dog;
private String name;
@Autowired和@Resource的区别:
在Spring4之后,要使用注解开发,必须要保证aop的包导入了
使用注解需要导入context约束,增加注解的支持
bean
属性如何注解
@Component
public class User {
//相当于
@Value("星哥" )
public String name ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
衍生的注解
@Component 有几个衍生注解,我们在web开发中,会按照mvc三层架构分层!
dao【@Repository】
service【@Service】
controller【@Controller】
这四个注解的功能都是一样的,都是代表将某个类注册到Spring中,装配Bean
自动装配置
- @Autowirde:自动装配通过ByType,ByName
- @Resource:自动装配通过ByName,ByType
- @Nullable:允许属性为null
- @Component:组件,放在类上,说明这个类被Spring管理了,就是bean!
作用域
@Component
@Scope("prototype")
public class User {
//相当于
@Value("星哥" )
public String name ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
小结:
xml与注解
@Component:组件写在类上面相当于Spring管理了整个类~=bean,取值为类名的小写
xml更加万能,适用任何场景的开发,维护简单方便
注解 不是自己类使用不了,维护相对复杂
xml与注解最佳实践:
xml用来管理bean;
注解只负责完成属性的注入;
我们在使用的过程中,只需要注意一个问题:必须让注解生效,就需要开启注解的支持
<context:component-scan base-package="com.xing.ko">context:component-scan>
<context:annotation-config>context:annotation-config>