日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

[Spring5]IOC容器_Bean管理注解方式_创建对象

發布時間:2023/12/4 javascript 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Spring5]IOC容器_Bean管理注解方式_创建对象 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

IOC操心Bean管理(基于注解方式)

1.什么是注解

(1)注解是代碼特殊標記,格式:@注解名稱(屬性名稱=屬性值,屬性名稱=屬性值…)

(2)使用注解,注解作用在類上面,方法上面,屬性上面

(3)使用注解目的:簡化xml配置

2.Spring針對Bean管理中創建對象提供注解

(1)@Component

(2)@Service

(3)@Controller

(4)@Repository

上面四個注解功能是一樣的,都可以用來創建bean實例

3.基于注解方式實現對象創建

第一步 引入依賴

spring-aop-5.2.6.RELEASE.jar

或者

<dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>5.3.15</version> </dependency>

第二步 開啟組件掃描

<!--1.如果掃描多個包,多個包使用逗號隔開--> <!--2.掃描包上層目錄--> <?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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!--開啟組件掃描--><!--1.如果掃描多個包,多個包使用逗號隔開--><!--2.掃描包上層目錄--><context:component-scan base-package="com.atguigu.spring.testdemo,com.atguigu.spring.service"></context:component-scan></beans>

第三步 創建類,在類上面添加創建對象注解

package com.atguigu.spring.service;import org.springframework.stereotype.Component;//在注解里面value屬性值可以省略不寫 //默認值是類名稱,首字母小寫 @Component(value = "userService")//<bean id = "" class = ""/> public class UserService {public void add(){System.out.println("service add");} }

測試:

package com.atguigu.spring.testdemo;import com.atguigu.spring.service.UserService; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestSpring5Demo1 {@Testpublic void testService(){ApplicationContext context = new ClassPathXmlApplicationContext("bean6.xml");UserService userService = context.getBean("userService", UserService.class);System.out.println(userService);userService.add();} }

總結

以上是生活随笔為你收集整理的[Spring5]IOC容器_Bean管理注解方式_创建对象的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。