1.添加类注解存储Bean对象(五大类)
@Controller:控制器,验证用户请求数据的正确性;(安保系统)
@Service:服务,编排和调度具体执行方法的;(客服中心)
@Repository:持久层,和数据库进行交互的;(执行者)= DAO(Data Access Object)
@Component:组件,工具类
@Configuration:配置项,项目中的一些配置
使用注解之前的前置工作:
<?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:content="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">
<content:component-scan base-package="com.java.demo"></content:component-scan>
</beans>
conten是否可以和bean混用? 可以
五大类注解可以不在component-scan包下码? 不可以
在component-scan下的所有子包的类(包不同名),只要加了五大类注解,就能存储到Spring中;
即使在component-scan包下,没有加五大类注解,也不可以运行;
五大类注解之间的关系:其他四个都是Component的子类,都是Component的扩展;
为什么需要五大类注解:
就是为了让用户看到注解后,望文生义,知道当前类的用途;
bean命名规则
2.方法注解@Bean