spring-ioc的注解 理解-1
简单对象注入的理解 用到了两个对象 Student 、Wife ,一个xml配置(在idea编译器的resource文件下),主要是为让spring去扫描注解,一个测试类,一个pom.xml,导入需要的依赖。
1.Student
import lombok.Data; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Data /** * 创建对象的注解 一下注解只能加在类上 * @Component 哪一层都可以 * @Controller 加在web层 * @Service 加在service层 也叫业务层 * @Repository 加在dao层 */ //使用注解的方式 //该注解的作用:当项目启动时,spring会去扫描注解(spring要想去扫描注解,那么就要通过配置文件去配置),一旦扫描到该注解,就会给该注解所 //注解的类创建对象,并放入容器中 该容器中对象的唯一标识是类名首字母小写,当然也可以手动 //定义一个唯一标识 @Component("stu") @Component public class Student { //给属性赋初值 使用注解的方式@Value @Value("1") private int id; @Value("renzhe") private String name; @Value("18") private int age; //当容器中有且只有一个wife对象时 用@Autowired注解进行对象的注入,他是根据类型自动注入的 //相当于Wife w = app.getBean("Wife.class"); //Wife wife = w; @Autowired private Wife wife; /** * 当有多个对象时,要想注入对象,有两种方法 * 1.@Autowired * @Qualifier(value="名字") * 这两个注解联合使用,@Autowired是根据类型 @Qualifier是根据类型和名字 * 2.@Resource(name="名字") 他相当于@Autowired和@Qualifier联合使用 */
@Autowired
private Date date;
}
2.Wife
import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Data @Component public class Wife { @Value("1") private int id; @Value("yu") private String name; @Value("18") private int age; }
3.applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!-- 要想扫描注解必须用到context命名空间--> <!-- context:component-scan扫描的包以及他的子包--> <context:component-scan base-package="com.briup.spring.bean"></context:component-scan>
<bean id="now" class="java.util.Date"></bean>
</beans>
4.SpringAnnTest
import com.briup.spring.bean.Student; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringAnnTest { @Test public void test1(){ ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); /*一旦加载这个配置文件 就会去扫描所配置的包以及他的子包 那么就会扫描到Student类上的注解Component, 就会给Student类创建一个student对象(也叫bean对象) 并且放入到容器中。 而默认的bean对象的唯一标识是类名首字母小写 也就是student */ Student student = (Student) app.getBean("student"); System.out.println(student); } }
5.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"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>spring-ioc-ann</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.11.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.11.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.11.0</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.2.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.2.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.2.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>5.2.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.2.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.7.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> </project>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义