可以将 Spring 其当做一个 容器 ,可以随意的存储对象。
@Component
@Controller
@Service
@Repository
这四个是常用的注解,用于声明 Bean 的注解,可以在后面跟括号为起 value 属性传值,代表依赖注入的 id,若不设置,则默认为 类名首字母小写
这四个注解本质都是同一个注解 @Component,只是名称不同,可以用于三层架构中不同的层
写在需要注入依赖的类上
上面是写在类上的注解
接下来是写在属性或者方法上的注解
@Value ()
给简单属性直接赋括号里的值
@AutoWired
非简单类型注入,默认根据类型装配,byType
@Autowired+@Qualifier 连用可以根据名字装配,byName
使用方法,在被注入的类上要加 @Compoment 注解
在注入类的非简单属性类型上直接写上 @Autowired 即可自动工具类型装配
当被注入的对象有多个,就无法通过类型直接注入,需要使用名字:
@Autowired
@Qualifier("被注入依赖的id")
两个注解联合使用
Autowired 注解可以使用在,属性,构造方法,构造方法的传参等需要注入依赖处
@Resource
未指定 name 属性则直接根据当前属性名作为名字
该方法与 @Autowired+@Qualifier 连用 的效果一致
区别:不能出现在构造方法上
使用前,需要注入依赖,配置 sping.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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Compomen</artifactId>
<version>1.0-SNAPSHOT</version>
<!--仓库-->
<repositories>
<!-- spring里程碑版本的仓库-->
<repository>
<id>repository.spring.milestone</id>
<name>Spring Milestone Repository</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<dependencies>
<!-- AutoWired 需要的依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.0-M2</version>
</dependency>
<!-- 单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- Resource 需要的依赖-->
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<version>2.1.1</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
</project>
需要对要用到的包进行扫描,在 spring.xml 中声明 (在 resource 目录下创建)
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
">
<context:component-scan base-package="breeze"/>
</beans>
需要注入对象的类:
@Component
public class Compomen {
@Value("18")
int age;
@Autowired
Auto auto;
@Resource(name="reso")
Reso reso;
public void printValue(){
System.out.println(age);
}
public void autoIsNullOrNot(){
System.out.println(auto == null ? "fail":"succeed");
}
public void printReso(){
reso.ResoText();
}
}
被注入的两个类:
@Component("reso")
public class Reso {
public void ResoText(){
System.out.println("Reso这个类中的方法被调用");
}
}
@Component
public class Auto {
Auto(){
System.out.println("Auto类中的构造方法被调用...");
}
}
测试程序
@Test
public void text(){
//拿到配置文件中的容器
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
//拿到容器中的类
Compomen compomen = context.getBean("compomen", Compomen.class);
compomen.printValue();
compomen.autoIsNullOrNot();
compomen.printReso();
}
运行结果:
Auto类中的构造方法被调用...
18
succeed
Reso这个类中的方法被调用Process finished with exit code 0