说明:
(1)本篇博客主要演示:【@Repository】、【@Service】、【@Controller】、【@Component】这四种【组件类型】注解;
(2)本篇博客是【Spring IoC容器与Bean管理20:使用注解方式实现Spring IoC一:Spring注解的好处;四种【组件类型】注解简介;】的延续;
(3)【使用注解方式开发Spring IoC的,applicationContext.xml的schema约束】和【前面使用XML方式开发Spring IoC的,applicationContext.xml的schema约束】是不同的;
(4)本篇博客仅仅涉及【@Repository】、【@Service】、【@Controller】、【@Component】这四种【组件类型】注解的最简单使用;即本篇博客仅仅涉及到【得到一个最简单的对象】,而没有涉及【对象的注入等对象初始化的工作】;
目录
4.一个简单的例子:主要目的是走一遍这个流程:@Repository注解
然后,编写SpringApplication入口列,去观察效果:
5.剩余补充:@Service注解、@Controller注解、@Component注解
1.为了演示,创建一个Maven项目s08;
2.在pom.xml中引入Spring的依赖:
<?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>com.imooc.spring</groupId> <artifactId>ioc</artifactId> <version>1.0-SNAPSHOT</version> <repositories> <repository> <id>aliyun</id> <name>aliyun</name> <url>https://maven.aliyun.com/repository/public</url> </repository> </repositories> <dependencies> <dependency> <groupId>org.springframew