spring学习笔记3
IOC操作Bean对象,基于注解方式
1.什么是注解
(1)注解是代码的特殊标记。格式:@注解名称(属性名称=属性值,属性名称=属性值。。。)
(2)使用注解,注解作用再累上面,方法上面,属性上面。
(3)使用注解的目的,简化xml配置
2.Spring针对Bean管理中创建对象提供注解
(1)@Component
(2)@Service
(3)@Controller
(4)@Respository
*上面四个注解功能是一样的,都可以用来创建bena实例
3.基于注解方式实现对象创建
第一步 引入依赖
第二步 开启组件扫描
<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" 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 http://www.springframework.org/schema/context/spring-context.xsd"> <!--开启组件扫描 如果扫描多个包,则用逗号隔开,或者扫描包的上层目录--> <context:component-scan base-package="com.atguigu.spring5.test,com.atguigu.spring5.service"></context:component-scan> </beans>
第三步创建类,在类上添加注解
//在注解中的value属性值可以省略不写 //默认值是类的名称,首字母小写 @Component(value = "userService") public class UserService { public void add(){ System.out.println("service add...."); } }
测试代码
@Test public void testService(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml"); UserService userService = context.getBean("userService",UserService.class); System.out.println(userService); userService.add(); }
4、开启组件扫描中的细节配置
<!--实例1 use-default-filter=false 表示现在不适用默认的filter,自己配置filter context:include-filter,设置扫描哪些内容 contect:exclude-filter为设置不扫描哪些内容--> <context:component-scan base-package="com.atguigu" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
5.基于注解方式实现属性注入
(1) @Autowired:根据属性类型自动装配
第一步 把service和dao对象创建,在service和dao类添加创建对象注解
第二步 在service注入dao对象,在service类添加dao类型属性,在属性上面使用注解
@Component(value = "userService") public class UserService { @Autowired private UserDao userDao; public void add(){ System.out.println("service add...."); userDao.add(); } }
(2) @Qualifier:根据属性名称进行注入
这个@Qualifler注解的使用,和上面的@Autowired一起使用
@Component(value = "userService") public class UserService { @Autowired @Qualifier(value = "userDaoImpl") private UserDao userDao; public void add(){ System.out.println("service add...."); userDao.add(); } }
(3) @Resource:可以根据类型注入,可以根据名称注入
@Value(value = "abc") private String sname;
6.完全注解开发
(1)创建配置类,替代xml配置文件
@Configuration//作为配置类,替代xml配置文件 @ComponentScan(basePackages = "com.atguigu") public class SpringConfig { }
(2)编写测试类
@Test public void testService2(){ ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); UserService userService = context.getBean("userService",UserService.class); System.out.println(userService); userService.add(); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!