本文对应B站遇见狂神说的idea Spring教程
目录
1. Spring 介绍
1.1 简介
Spring官方文档地址:https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html(个人需要VPN才能打开,不代表普遍情况)
全版本文档下载地址:https://repo.spring.io/release/org/springframework/spring
GitHub地址:https://github.com/spring-projects/spring-framework
使用maven导入的包:spring-webmvc和spring-jdbc(使用maven+报名搜索最新版导入)
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
在日后的使用中,我们还会用到junit包,在这个地方也一起导入
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
PS:在导入包时,经常遇到包名爆红的问题
1.通常是因为网络原因,导致本地仓库中下载的包下载了一般就停了,在C:\Users\个人用户名\.m2\repository中去查看自己包的下载情况,若存在LastUpdated文件,则需要删除,然后再idea中的maven管理点击刷新按钮,即reimport all maven projects
2.若查看本地仓库的包是下载全的,可以将pom.xml文件中将爆红的包名先注释掉,在maven中进行clean命令,再将包名解除注释,理论上即可解决问题
1.2 优点
- 开源的,免费的容器
- 轻量级的,非入侵式的框架(非入侵即导入不会对原来的项目产生影响)
- 控制反转(IOP),面向切面编程(AOP)
- 支持事务的处理,对框架整合的支持
1.3 组成
(图片来自百度百科)
1.4 扩展
-
Spring Boot
一个快速开发的脚手架
基于SpingBoot可以快速开发单个微服务
约定大于配置
-
Spring Cloud
基于SpringBoot实现
弊端:发展至今,时间太久,配置十分繁琐
2.IoC理论
IoC即Inversion of Control,意为控制反转,接下来将推导IoC的形成原因和作用
2.1 IoC理论推导
1.UserDao接口
package com.tyrion.dao;
public interface UserDao{
void getUser();
}
2.UserDaoImpl实现类
package com.tyrion.dao;
public class UserDaoImpl implements UserDao{
void getUser(){
System.out.println("默认获取用户的数据");
}
}
3.UserService业务接口
package com.tyrion.service;
public interface UserService{
void getUser();
}
4.UserServiceImpl业务实现类
package com.tyrion.service;
import com.tyrion.dao.UserDao;
import com.tyrion.dao.UserDaoImpl;
public class UserServiceImpl implements UserService{
//业务层完成的工作:调用dao层的实现
private UserDao = new UserDaoImpl;
public void getUser(){
UserDao.getUser();
}
}
5.测试类MyTest.java
import com.tyrion.service.UserService;
import com.tyrion.service.UserServiceImpl;
public class MyTest{
public static void main(String[] args){
//用户实际调用的是业务层,dao层不需要用户接触
UserService userService = new UserService();
userService.getUser();
}
}
这是原本的实现方式,但此时,若需求更新,要增加一个Mysql的实现类和一个Oracle的实现类
UserDaoMysqlImpl.java
package com.tyrion.dao;
public class UserDaoMysqlImpl implements UserDao{
void getUser(){
System.out.println("Mysql获取用户的数据");
}
}
UserDaoOracleImpl.java
package com.tyrion.dao;
public class UserDaoOraclesqlImpl implements UserDao{
void getUser(){
System.out.println("Oracle获取用户的数据");
}
}
这样一来,若用户需要使用Mysql服务,必须在UserServiceImpl.java中做出改变
//使用默认服务
private UserDao = new UserDaoImpl;
//若使用Mysql的服务,需要改变成为
private UserDao = new UserDaoMysqlImpl();
//若使用Oracle的服务,需要改变为
private UserDao = new UserDaoOracleImpl();
这样的实现方式,需要根据用户的不同需求去改变源代码,当程序的代码量很大时,需要修改大量的代码
UserServiceImpl中,在可以利用set进行动态实现的注入:
package com.tyrion.service;
import com.tyrion.dao.UserDao;
import com.tyrion.dao.UserDaoImpl;
public class UserServiceImpl implements UserService{
//使用set方法
private UserDao userDao;
publice void serUserDao(UserDao userDao){
this.userDao = userDao;
}
public void getUser(){
UserDao.getUser();
}
}
这样一来,在测试类中调用接口时,可以灵活的根据需求注入实现方式
Mytest.java
import com.tyrion.service.UserService;
import com.tyrion.service.UserServiceImpl;
public class MyTest{
public static void main(String[] args){
//用户实际调用的是业务层,dao层不需要用户接触
UserService userService = new UserService();
((UserServiceImpl) userService).setUserDao(new UserDaoMysqlImpl);
userService.getUser();
}
}
- 之前,业务层主动创建对象,用户通过业务层调用不同的服务,因此用户改变时程序员需要改变业务层的代码
- 使用set方式注入之后,业务层不再拥有主动性,而是变成了被动接受对象,用户需要什么实现,自己就创建什么类传入即可
这种思想,从本质上解决了问题,程序员不用去管理对象的创建,系统的耦合性大大降低,可以更加专注在业务的实现上,这就是IoC的原型
2.2 IoC本质
IoC是一种设计思想,DI(依赖注入)是IoC的一种实现方式。
没有IoC的程序中,我们是用面向对象编程,对象的创建和对象之间的关系完全硬编码在程序之中,对象的创建由程序自己控制,而控制反转之后将对象的创建交给了第三方,即获取依赖对象的方式反转了
IoC是Spring框架的核心内容,Spring使用了多种方式实现IoC可以使用XML配置,可以使用注解,新版本的Spring也可以零配置实现IoC
Spring容器会在初始化时读取配置文件 ,根据配置文件或元数据创建与组织对象存入容器中,程序使用时,再用IoC容器中取出对象
3. HelloSpring项目
1.pom.xml文件配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-test</artifactId>
<groupId>com.test</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>HelloSpring</artifactId>
</project>
2.Hello类的创建
package com.tyrion.pojo;
public class Hello {
private String str;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
@Override
public String toString() {
return "Hello{" +
"str='" + str + '\'' +
'}';
}
}
3.在resource中创建beans.xml,进行spring文件的配置
<?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.tyrion.pojo.Hello">
<property name="str" value="spring"></property>
</bean>
</beans>
4.在MyTest.java中进行测试
import com.tyrion.pojo.Hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
//获取spring的容器
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//对象在spring中管理,使用时直接取出
Hello hello=(Hello) context.getBean("hello");
System.out.println(hello.toString());
}
}
在HelloSpring项目中,我们java的对象在beans.xml中创建,这些对象在spring中称为bean,
<!-- 使用spring创建对象时,对象统称为bean
java中创建对象:
Hello hello = new Hello();
spring的配置文件(XML)创建对象:
id = 变量名,bean的唯一标识符
class = new 的对象类型
name:别名,和alias相似,但是可以取多个别名,name="hello1,hello2,hello3",并且分隔符可以用空格,逗号,分号等
property:给对象的属性赋值(spring使用set方法负值,所以必须要有set方法)
ref:引用spring中已经创建好的对象(上面的bean中id值)
value:具体的值,基本数据类型
在加载配置文件的时候,对象已经创建,无论是否使用context.getbean()取出
-->
4. IoC创建对象的方式
4.1 无参构造
User.java
package com.tyrion.pojo;
public class User{
private String name;
public User(){
System.out.println("User的无参构造");
}
public String getUser(){
return name;
}
public void setUser(String name){
this.name = name;
}
public void show(){
System.out.println("name"+name);
}
}
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="user" class="com.tyrion.pojo.User">
<property name="name" value="李华"></property>
</bean>
</beans>
MyTest.java
import com.tyrion.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
//获取spring的容器
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//对象在spring中管理,使用时直接取出
//输出 User的无参构造(对象在读取配置文件时已经创建)
User user=(User) context.getBean("user");
System.out.println( );
//输出 name=李华
}
}
4.2 有参构造
User.java
package com.tyrion.pojo;
public class User{
private String name;
public User(String name){
this.name=name;
System.out.println("User的有参构造");
}
public String getUser(){
return name;
}
public void setUser(String name){
this.name = name;
}
public void show(){
System.out.println("name"+name);
}
}
4.2.1 通过下标
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="user" class="com.tyrion.pojo.User">
<constructor-arg index="0" value="李华"/>
</bean>
</beans>
4.2.2通过类型
<?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="user" class="com.tyrion.pojo.User">
<constructor-arg type="Java.lang.String" value="李华"/>
</bean>
</beans>
但假设存在两个参数,都是String,就不能使用
4.2.3 通过参数名
<?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="user" class="com.tyrion.pojo.User">
<constructor-arg name="name" value="李华"/>
</bean>
</beans>
5. Spring的配置
5.1别名
<!-- 别名,alias:如果添加了别名,getbean()也可以使用别名来获取对象-->
<alias name="hello" alias="new_alias"></alias>
5.2bean的配置
<!--
id = 变量名,bean的唯一标识符
class = new 的对象类型
name:别名,和alias相似,但是可以取多个别名,name="hello1,hello2,hello3",并且分隔符可以用空格,逗号,分号等
-->
5.3 import
假设现在项目有三个人进行开发,不同的类注册在三个人各自的bean中,我们可以利用import将所有人的beans.xml进行合并成为一个,使用的时候用总的
<!-- 一般用于团队开发,可以将多个配置文件导入合并为一个-->
<import resource="beans1.xml"/>
<import resource="beans2.xml"/>
<import resource="beans3.xml"/>
6. DI(Dependency Injection)
DI即依赖注入
6.1 构造器注入
上文使用的方法(constructor-arg)
6.2 set方式注入【重点】
- 依赖:bean对象的创建依赖于容器
- 注入:bean对象中所有的属性,由容器来注入
【环境搭建】
- 复杂类型
- 真实测试对象
1.Address.java类
package com.tyrion.pojo;
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
- Student.java类
package com.tyrion.pojo;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Properties;
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbiess;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobbiess() {
return hobbiess;
}
public void setHobbiess(List<String> hobbiess) {
this.hobbiess = hobbiess;
}
public Map<String, String> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address.toString() +
", books=" + Arrays.toString(books) +
", hobbiess=" + hobbiess +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
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="address" class="com.tyrion.pojo.Address">
<property name="address" value="新华大街"/>
</bean>
<bean id="student" class="com.tyrion.pojo.Student">
<!-- 普通注入-->
<property name="name" value="tyrion"/>
<!-- bean注入,使用ref注入-->
<property name="address" ref="address"/>
<!-- 数组注入-->
<property name="books">
<array>
<value>红楼梦</value>
<value>西游记</value>
<value>水浒传</value>
</array>
</property>
<!-- list注入-->
<property name="hobbiess">
<list>
<value>听歌</value>
<value>看电影</value>
</list>
</property>
<!-- map注入-->
<property name="card">
<map>
<entry key="身份证" value="1234546"/>
<entry key="银行卡" value="6211111"/>
</map>
</property>
<!-- set注入-->
<property name="games">
<set>
<value>LOL</value>
<value>CSGO</value>
</set>
</property>
<!-- null值注入-->
<property name="wife">
<null/>
</property>
<!-- property注入-->
<property name="info">
<props>
<prop key="学号">201600000</prop>
<prop key="性别">男性</prop>
<prop key="姓名">李华</prop>
</props>
</property>
</bean>
</beans>
4.测试类
import com.tyrion.pojo.Student;
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");
Student student=(Student)context.getBean("student");
System.out.println(student.toString());
}
}
6.3 其他方式注入
6.3.1 P命名空间
P命名空间注入对应着set注入的属性注入
以下是官方文档的p-namespace注入实例,注意和以前不同的是在开头导入了
xmlns:p="http://www.springframework.org/schema/p"
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- traditional declaration with optional argument names -->
<bean name="classic" class="com.example.ExampleBean">
<property name="email" value="someone@somewhere.com"/>
</bean>
<!-- c-namespace declaration with argument names -->
<bean name="p-namespace" class="com.example.ExampleBean"
p:email="someone@somewhere.com"/>
</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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="john-classic" class="com.example.Person">
<property name="name" value="John Doe"/>
<property name="spouse" ref="jane"/>
</bean>
<bean name="john-modern"
class="com.example.Person"
p:name="John Doe"
p:spouse-ref="jane"/>
<bean name="jane" class="com.example.Person">
<property name="name" value="Jane Doe"/>
</bean>
</beans>
6.3.2 C命名空间
C命名空间对应着constructor,即构造器注入
以下是官方文档的c-namespace注入实例,注意,在开头导入了
xmlns:c="http://www.springframework.org/schema/c"
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<bean id="beanTwo" class="x.y.ThingTwo"/>
<bean id="beanThree" class="x.y.ThingThree"/>
<!-- traditional declaration with optional argument names -->
<bean id="beanOne" class="x.y.ThingOne">
<constructor-arg name="thingTwo" ref="beanTwo"/>
<constructor-arg name="thingThree" ref="beanThree"/>
<constructor-arg name="email" value="something@somewhere.com"/>
</bean>
<!-- c-namespace declaration with argument names -->
<bean id="beanOne" class="x.y.ThingOne" c:thingTwo-ref="beanTwo"
c:thingThree-ref="beanThree" c:email="something@somewhere.com"/>
</beans>
6.4 bean的作用域
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4QqOV1Re-1596977169434)(C:\Users\Tyrion\AppData\Roaming\Typora\typora-user-images\image-20200808162221762.png)]
<!-- bean的scope标签
scope="singleton"(默认):单例模式,getbean共享一个对象,使用==测试时返回true
scope="prototype":原型模式,每getbean一个都是新的对象,使用==测试时会false
其他:request,session,application,websocket只在web开发中使用
-->
7. Bean的自动装配
自动装配是Spring满足bean依赖的一种方式,Spring会在上下文中自动寻找,并自动给bean装配属性!
在Spring中有三种装配方式:
1.在xml中显式配置
2.在java中显式配置
3.隐式 的自动装配【重要】
7.1 测试环境
搭建测试环境
1.Cat类
package com.tyrion.pojo;
public class Cat {
public void shout(){
System.out.println("喵");
}
}
2.Dog类
package com.tyrion.pojo;
public class Dog {
public void shout(){
System.out.println("汪");
}
}
3.People类
package com.tyrion.pojo;
public class People {
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 "People{" +
"cat=" + cat +
", dog=" + dog +
", name='" + name + '\'' +
'}';
}
}
4.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="cat" class="com.tyrion.pojo.Cat"/>
<bean id="dog" class="com.tyrion.pojo.Dog"/>
<!-- 自动装配:autowire
byName:在容器中上下文自动寻找,和自己对象set方法后面值对应的bean id(即:bean的id和set方法的形参名字相同,区分大小写)
byType:在容器上下文自动寻找,和自己对象属性类型相同的bean(弊端:每个类只能有一个,存在多个会报错 好处:甚至可以不写名字,简洁)
-->
<bean id="people" class="com.tyrion.pojo.People" autowire="byName">
<property name="name" value="李华"/>
</bean>
</beans>
5.测试类Mytest
import com.tyrion.pojo.People;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Mytest {
@Test
public void test1(){
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
People people=context.getBean("people",People.class);//传入第二个参数People.class这样前面不用进行强制转换
people.getCat().shout();
people.getDog().shout();
}
}
7.2使用byName和byType实现自动装配
总结:
byName:保证所有bean的id唯一,并且这个bean和需要自动注入的属性的set方法的值一致
byType:保证所有bean的class唯一,并且这个bean和需要自动注入的属性类型一致
7.3使用注解进行自动装配
jdk1.5支持注解,Spring从2.5支持注解
要使用注解:
1.导入约束 context约束
2.配置注解的支持 context:annotation-config/
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/>
<bean id="dog" class="com.tyrion.pojo.Dog"/>
<bean id="cat" class="com.tyrion.pojo.Cat"/>
<bean id="people" class="com.tyrion.pojo.People"/>
</beans>
People.java需要修改
package com.tyrion.pojo;
import org.springframework.beans.factory.annotation.Autowired;
public class People {
@Autowired
private Cat cat;
@Autowired
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 "People{" +
"cat=" + cat +
", dog=" + dog +
", name='" + name + '\'' +
'}';
}
}
即 在类的属性上加一个@Autowire即可(在属性的set方法上加入也可以)
并且,如果在属性上面加的话,下面的set方法可以去掉(@Autowire通过反射来实现),前提是自动装配的属性在IOC容器中存在,且符合类型,
(视频课程中讲的是byName,但实际是byType和byType的整合,先name找,再type找)
PS:
@Nullable
//1.@Nullable 字段标识了这个注解,说明这个字段可以为空值,例如
public People(@Nullable String name){
this.name=name;
}
//这样如果name为空,这个方法不会报错
属性required
//2.@Autowired可以跟一个属性required,默认是true
public @interface Aotuwired{
boolean required() default true;
}
//如果显式标注,说明这个对象可以为空,否则不允许为空
@Autowired(required = false)
@Qualifier
//3.使用@Autowired自动装配时,若name和type都不能确定唯一,可以使用@Qualifier指定一个id
@Autowired
@Qualifier(value = "dog_id")
private Dog dog;
@Resource
//4.也可以使用java原生注解@Resource实现(同样是name和type的集合),但是用这样的注解,idea没有Spring绿色叶子的提示
@Resource
private Dog dog;
//相似的,也可以使用name参数指定唯一bean对象
@Resource(name = "dog_id")
//最后,@Resource和@Autowired具体通过type还是name,结合方法,先后顺序,个人没有测试,课程内容和弹幕也存在矛盾,具体使用还需要尝试
8. 使用注解开发
在Spring4之后,使用注解开发需要导入aop的依赖(在maven下导入webmvc的包之后会自动导入aop的包)
使用注解需要导入context约束,增加对注解的支持
<?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.tyrion.pojo.Dog"/>
<bean id="cat" class="com.tyrion.pojo.Cat"/>
<bean id="people" class="com.tyrion.pojo.People"/>
</beans>
1.bean
@Component
//5.等价于 <bean id="user" class="com.tyrion.pojo.User"/>,放在类上,说明这个类被Spring管理了
@Component
public class User {
public String name="李华";
}
2.属性注入
@Value
有两种实现,一种是在属性上面加
@Component
public class User {
//相当于<property name="name" value="李华">
@Value("李华")
public String name;
}
另一种是写在set方法上面
@Component
public class User {
public String name;
@Value("李华")
public void setName(String name) {
this.name = name;
}
}
3.衍生注解
@Component有几个衍生注解,在web开发中,会按照MVC分层
-
dao【@Repository】
-
service【@Service】
-
controller【@Controller】
这四个注解的功能都是一样的,都是代表将某个类注册到Spring中,装配bean
4.自动装配
@Autowired:自动装配
使用@Autowired自动装配时,若name和type都不能确定唯一,可以使用@Qualifier指定一个id
@Nullable:字段标记了这个注解,说明字段值可以为空
@Resource:自动装配
5.作用域
@Scope("singleton"):单例
@Scope("prototype"):原型
6.小结
xml与注解:
- xml更加万能,,适用于任何场合,维护更加方便
- 注解 不是自己的类不能使用,维护相对复杂
最佳实现:
- xml用来管理bean
- 注解负责属性的注入
- 使用注解需要注意:要让注解生效
<!-- 指定要扫描的包,这个包下的注解就会生效-->
<context:component-scan base-package="com.tyrion"/>
<context:annotation-config/>
9. 使用Java的方式配置Spring
完全不使用Spring的xml的配置,全权交给Java来做
JavaConfig是Spring的一个子项目,在Spring4之后,成为一个核心功能
使用java的配置就是使用**@Configuration**便签创建一个配置类来装配bean
1.User类
package com.tyrion.pojo;
import org.springframework.beans.factory.annotation.Value;
public class User {
@Value("李华") //属性注入
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
2.Config类
package com.tyrion.config;
import com.tyrion.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration //代表这是一个配置类,和之前beans.xml是一样的
public class Config {
//相当于xml中注册一个bean
//方法名 = id
//返回值 = class
@Bean
public User user(){
return new User();
}
}
3.测试类
import com.tyrion.config.Config;
import com.tyrion.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Mytest {
public static void main(String[] args) {
//如果完全通过配置类,我们需要使用AnnotationConfigApplicationContext获取容器,通过配置类的class对象加载
ApplicationContext context=new AnnotationConfigApplicationContext(Config.class);
User user=context.getBean("user",User.class);
System.out.println(user.getName());
}
}