Spring-IOC容器中的常用注解與使用方法詳解
Spring是什么?
Spring是一個(gè)輕量級(jí)Java開(kāi)發(fā)框架,最早有Rod Johnson創(chuàng)建,目的是為了解決企業(yè)級(jí)應(yīng)用開(kāi)發(fā)的業(yè)務(wù)邏輯層和其他各層的耦合問(wèn)題。它是一個(gè)分層的JavaSE/JavaEE full-stack(一站式)輕量級(jí)開(kāi)源框架,為開(kāi)發(fā)Java應(yīng)用程序提供全面的基礎(chǔ)架構(gòu)支持。Spring負(fù)責(zé)基礎(chǔ)架構(gòu),因此Java開(kāi)發(fā)者可以專(zhuān)注于應(yīng)用程序的開(kāi)發(fā)。
體系結(jié)構(gòu)
核心容器(Core Container):Spring的核心容器是其他模塊建立的基礎(chǔ),有Spring-core、Spring-beans、Spring-context、Spring-context-support和Spring-expression(String表達(dá)式語(yǔ)言)等模塊組成
數(shù)據(jù)訪問(wèn)/集成(Data Access)層:數(shù)據(jù)訪問(wèn)/集成層由JDBC、ORM、OXM、JMS和事務(wù)模塊組成。
Web層:Web層由Spring-web、Spring-webmvc、Spring-websocket和Portlet模塊組成。
AOP(Aspect Oriented Programming)模塊:提供了一個(gè)符合AOP要求的面向切面的編程實(shí)現(xiàn),允許定義方法攔截器和切入點(diǎn),將代碼按照功能進(jìn)行分離,以便干凈地解耦。
植入(Instrumentation)模塊:提供了類(lèi)植入(Instrumentation)支持和類(lèi)加載器的實(shí)現(xiàn),可以在特定的應(yīng)用服務(wù)器中使用。
消息傳輸(Messaging):Spring4.0以后新增了消息(Spring-messaging)模塊,該模塊提供了對(duì)消息傳遞體系結(jié)構(gòu)和協(xié)議的支持。
測(cè)試(Test)模塊:Spring-test模塊支持使用JUnit或TestNG對(duì)Spring組件進(jìn)行單元測(cè)試和集成測(cè)試。
引入Jar包
<dependencies> <!--spring的jar包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.11.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.0.11.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.0.11.RELEASE</version> </dependency> </dependencies>
導(dǎo)入約束
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--spring的約束 --> <!--把對(duì)象的創(chuàng)建交給Spring來(lái)管理 --> <!--獲取容器中對(duì)象時(shí)使用id--> <!-- <bean id="accountServiceImpl" class="com.dynamic2.service.Impl.AccountServiceImpl"></bean> <bean id="accountDaoImpl" class="com.dynamic2.dao.Impl.AccountDaoImpl"></bean>--> <context:component-scan base-package="com.dynamic2"></context:component-scan> </beans>
常見(jiàn)注解
用于創(chuàng)建對(duì)象
@Component:把資源讓spring來(lái)管理。相當(dāng)于xml中配置一個(gè)bean。value:指定bean的id,如果不指定value屬性,默認(rèn)bean的id是當(dāng)前類(lèi)的類(lèi)名。首字母小寫(xiě)
@Controller:與@Component功能一樣,一般用在表現(xiàn)層,便于分層
@Service:與@Component功能一樣,一般用在業(yè)務(wù)層,便于分層
@Repository:與@Component功能一樣,一般用于持久層,便于分層
/** * @Author: Promsing * @Date: 2021/3/19 - 11:34 * @Description: 用于創(chuàng)建對(duì)象 * @version: 1.0 * XML配置 <bean id="accountServiceImpl" class="com.dynamic2.service.Impl.AccountServiceImpl"></bean> */ @Repository("accountDao ") public class AccountDaoImpl implements IAccountDao { ...... } @Service("accountService") public class AccountServiceImpl implements IAccountService { ...... } @Component("accountServiceImpl") @Scope("prototype")//多例 public class AccountServiceImpl2 implements IAccountService { ...... }
用于注入數(shù)據(jù)
@Autowired:自動(dòng)按照類(lèi)型注入。當(dāng)使用注解注入屬性時(shí),set方法可以省略。它只能注入其他bean類(lèi)型。當(dāng)有多個(gè)類(lèi)型匹配時(shí)。使用要注入的對(duì)象變量名稱(chēng)作為bean的id,在spring容器中查找,找到了注入成功,找不到就報(bào)錯(cuò)。
@Qualifier:在自動(dòng)按照類(lèi)型注入的基礎(chǔ)上,再按照Bean的id注入。它在給字段注入時(shí)不能單獨(dú)使用,必須和@Autowire一起使用;但是給方法參數(shù)注入時(shí),可以單獨(dú)使用。value屬性是指定Bean的id
@Resource:直接按照Bean的id注入。它也只能注入其他Bean類(lèi)型。name屬性是指定Bean的id
@Value:注入基本數(shù)據(jù)類(lèi)型和String類(lèi)型數(shù)據(jù)
/** * @Author: Promsing * @Date: 2021/3/19 - 11:34 * @Description: 用于創(chuàng)建對(duì)象 * @version: 1.0 * XML配置 <bean id="accountServiceImpl" class="com.dynamic2.service.Impl.AccountServiceImpl"></bean> */ @Component("accountServiceImpl") @Scope("prototype")//多例 public class AccountServiceImpl implements IAccountService { //注入成員變量 /* @Autowired 自動(dòng)按照類(lèi)型注入--尋找類(lèi)型 @Qualifier("accountDao2")*/ //尋找id //以上兩個(gè)注解相加的作用等于這個(gè) @Resource(name = "accountDao2") private IAccountDao accountDao2; @Override public void saveAccount() { accountDao2.saveAccount(); //System.out.println("service中的saveAccount執(zhí)行了~~"); } }
用于改變作用范圍
@Scope:指定Bean的作用范圍。value屬性指定范圍的值--singleton單例,prototype多例,request作用與web應(yīng)用的請(qǐng)求范圍,session作用與web應(yīng)用的會(huì)話范圍,global-session作用與集群環(huán)境中會(huì)話范圍
@Component("accountServiceImpl") @Scope("prototype")//多例 public class AccountServiceImpl implements IAccountService { ...... }
和生命周期相關(guān)(了解)
@PostConstruct:用于指定初始化方法
@PreDestroy:用于指定銷(xiāo)毀方法
Spring5
@Configuration:用于指定當(dāng)前類(lèi)是一個(gè)spring配置類(lèi),當(dāng)有容器時(shí)會(huì)從該類(lèi)上加載注解。獲取容器是使用AnnotationApplicationContext(有@Configuration注解的類(lèi).class)
@ComponentScan:用于指定spring在初始化容器時(shí)要掃描的包。作用和在spring的xml配置文件找那個(gè)的<context : component-sacn base-package="com.dynamic"/>
@Bean:該注解只用用在方法上,表明使用此方法創(chuàng)建一個(gè)對(duì)象,并且放入spring容器中
@Import:用于導(dǎo)入其他配置類(lèi),解耦合
/** * @Author: Promsing * @Date: 2021/3/28 - 0:36 * @Description: Spring配置類(lèi) * @version: 1.0 */ @Configuration//指定當(dāng)前類(lèi)是一個(gè)配置類(lèi) @ComponentScan("com.dynamic_transaction_anno")//用于指定spring在初始化容器時(shí)需要掃描的包 @Import({JdbcConfig.class,TransactionConfig.class})//導(dǎo)入其他配置類(lèi) @EnableTransactionManagement//開(kāi)啟spring注解事務(wù)的支持 public class SpringConfig { @Bean("jdbcTemplate") public JdbcTemplate createJdbcTemplate(DataSource ds){ return new JdbcTemplate(ds); } @Bean("dataSource") public DataSource createDataSource(){ DriverManagerDataSource dr=new DriverManagerDataSource(); dr.setDriverClassName("com.mysql.jdbc.Driver");//com.mysql.jdbc.Driver dr.setUrl("jdbc:mysql//localhost:330b/eesy"); dr.setUsername("root"); dr.setPassword("root"); return dr; } }
Spring整合Junit
@RunWith:替代原有的運(yùn)行器
@ContextConfiguration:指定配置文件的位置
@RunWith(SpringJUnit4ClassRunner.class)//替代原有運(yùn)行器 @ContextConfiguration(classes=SpringConfiguration.class)//指定配置類(lèi) public class AccountServiceTest { @Test public void testFindAll(){ //執(zhí)行測(cè)試方法 } }
從IOC容器中獲取對(duì)象
/** * @Author: Promsing * @Date: 2021/3/21 - 11:22 * @Description: 單元測(cè)試 * @version: 1.0 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes=SpringConfiguration.class) public class AccountServiceTest { @Resource(name = "accountServiceImpl") private IAccountService accountService; @Test //從容器中獲取對(duì)象 public void test(){ //一、獲取容器 //使用配置文件加載 ApplicationContext ac=new ClassPathXmlApplicationContext("bean3_1.xml"); //使用配置類(lèi)加載 /// ApplicationContext ac=new AnnotationConfigApplicationContext(SpringConfiguration.class); //二、獲取對(duì)象 accountService=(IAccountService)ac.getBean("accountServiceImpl",IAccountService.class); //三、執(zhí)行方法 List<Account> allAccounts = accountService.findAllAccount(); for (Account allAccount : allAccounts) { System.out.println(allAccount); } } }
到此這篇關(guān)于Spring-IOC容器中的常用注解與使用方法詳解的文章就介紹到這了,更多相關(guān)Spring-IOC注解使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java Hibernate多對(duì)多映射詳解及實(shí)例代碼
這篇文章主要介紹了java Hibernate多對(duì)多映射詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-01-01Java 判斷線程池所有任務(wù)是否執(zhí)行完畢的操作
這篇文章主要介紹了Java 判斷線程池所有任務(wù)是否執(zhí)行完畢的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08Java 數(shù)據(jù)結(jié)構(gòu)進(jìn)階二叉樹(shù)題集上
二叉樹(shù)可以簡(jiǎn)單理解為對(duì)于一個(gè)節(jié)點(diǎn)來(lái)說(shuō),最多擁有一個(gè)上級(jí)節(jié)點(diǎn),同時(shí)最多具備左右兩個(gè)下級(jí)節(jié)點(diǎn)的數(shù)據(jù)結(jié)構(gòu)。本文將帶你通過(guò)實(shí)際題目來(lái)熟練掌握2022-04-04JUnit5中的參數(shù)化測(cè)試實(shí)現(xiàn)
參數(shù)化測(cè)試使得我們可以使用不同的參數(shù)運(yùn)行同一個(gè)測(cè)試方法,從而減少我們編寫(xiě)測(cè)試用例的工作量,本文主要介紹了JUnit5中的參數(shù)化測(cè)試實(shí)現(xiàn),感興趣的可以了解一下2023-05-05為什么不推薦使用BeanUtils屬性轉(zhuǎn)換工具示例詳解
這篇文章主要介紹了為什么不推薦使用BeanUtils屬性轉(zhuǎn)換工具,本文通過(guò)示例代碼給大家詳細(xì)介紹,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07spring?@value無(wú)法取值多個(gè)properties文件的解決
這篇文章主要介紹了spring?@value無(wú)法取值多個(gè)properties文件的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Java concurrency集合之 CopyOnWriteArrayList_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java concurrency集合之 CopyOnWriteArrayList的相關(guān)資料,需要的朋友可以參考下2017-06-06