07-XML 文件注解开发
目錄
- 注解
- 一、@Configuration
- @Import
- 二、@ComponentScan
- 三、@bean
- 1.Config完整代碼
- 2.測試類
不論是 xml 開發(fā)或者注解開發(fā)都有一個(gè)問題是,我們還是不得不寫 bean.xml 文件,這次解決這個(gè)問題
創(chuàng)建 spring06 項(xiàng)目
將上一個(gè)項(xiàng)目導(dǎo)入
注解
@Configuration
- 作用:指定當(dāng)前類是一個(gè)配置類,它的作用和 bean.xml 是一樣的
- 細(xì)節(jié):當(dāng)配置類作為 AnnotationConfigApplicationContext 對象創(chuàng)建的參數(shù)時(shí),該注解可以不寫
@ComponentScan
作用:用于通過注解指定 spring 在創(chuàng)建容器時(shí)要掃描的包。
屬性:
- value:
- 她和 basePackages 的作用是一樣的,都是用于指定創(chuàng)建容器要掃描的包。
- 我們使用此注解就等同于在 xml 中配置了:
源碼中,參數(shù)有value、basePackages,上面的注解表示,這兩個(gè)相等
- value:
@bean
- 作用:
- 用于把當(dāng)前方法的返回值作為 bean 對象存入 spring 的 ioc 容器中
- 屬性:
- name:
- 用于指定 bean 的 id,當(dāng)不寫時(shí),默認(rèn)值是當(dāng)前方法的名稱
- name:
- 細(xì)節(jié):
- 當(dāng)我們使用注解配置方法時(shí),如果方法有參數(shù),spring 框架會去容器中查找有沒有可用的 bean 對象。
- 查找的方式和 Autowired 的注解作用是一樣的
@import
- 作用:用于導(dǎo)入其他配置類
- 屬性:
- value:用于指定其他配置類的字節(jié)碼
- 當(dāng)我們使用Import 的注解之后,有 Import 注解的類就父類配置類,而導(dǎo)入的都是子類配置類
@PropertySource
- 作用:用于指定properties文件的位置
- 屬性:
- value:指定文件的名稱和路徑
- 關(guān)鍵字:classpath,表示類路勁下
- value:指定文件的名稱和路徑
一、@Configuration
作用:指定當(dāng)前類是一個(gè)配置類
細(xì)節(jié):當(dāng)配置類作為 AnnotationConfigApplicationContext 對象創(chuàng)建的參數(shù)時(shí),該注解可以不寫
何時(shí)可以不寫?
在測試類中注入可不寫
new AnnotationConfigApplicationContext(SpringConfiguration.class);何時(shí)必須寫?
如果我們把里面的配置信息轉(zhuǎn)移到 JdbcConfig 類中,JdbcConfig 類還在掃描的包中
SpringConfiguration 里面內(nèi)容為空,JdbcConfig 類就必須寫@Configuration,或者也實(shí)行注入,實(shí)行注入的話,SpringConfiguration 與 JdbcConfig 同級
ac=new AnnotationConfigApplicationContext(SpringConfiguration.class, JdbcConfig.class);@Import
使用 Import 標(biāo)簽,可以不用寫@Configuration 與注入
@Configuration @Import(JdbcConfig.class) @ComponentScan(basePackages = {"com"}) public class SpringConfiguration {二、@ComponentScan
作用:用于通過注解指定 spring 在創(chuàng)建容器時(shí)要掃描的包。
屬性:
? value:她和 basePackages 的作用是一樣的,都是用于指定創(chuàng)建容器要掃描的包。
? 我們使用此注解就等同于在 xml 中配置了:
<context:component-scan base-package="com"/>源碼中,參數(shù)有value、basePackages,上面的注解表示,這兩個(gè)相等
而且這兩個(gè)屬性是數(shù)組
@AliasFor("basePackages")String[] value() default {};@AliasFor("value")String[] basePackages() default {};當(dāng)下面代碼出場了,那么 bean.xml 中 可以被拿掉了
package com.config;import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration;@Configuration @ComponentScan(basePackages = {"com"}) public class SpringConfiguration {}三、@bean
/*** 創(chuàng)建數(shù)據(jù)源對象* @return*/@Bean(name = "dataSource")public DataSource createDataSource(){ComboPooledDataSource ds=new ComboPooledDataSource();try {ds.setDriverClass("com.mysql.cj.jdbc.Driver");ds.setJdbcUrl("jdbc:mysql://localhost:3306/test");ds.setUser("root");ds.setPassword("root");}catch (PropertyVetoException e){e.printStackTrace();}return ds;}細(xì)節(jié):
- 當(dāng)我們使用注解配置方法時(shí),如果方法有參數(shù),spring 框架會去容器中查找有沒有可用的 bean 對象。
- 查找的方式和 Autowired 的注解作用是一樣的,
1.Config完整代碼
package com.config;import com.mchange.v2.c3p0.ComboPooledDataSource; import org.apache.commons.dbutils.QueryRunner; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration;import javax.sql.DataSource; import java.beans.PropertyVetoException;/*** 描述:* 〈該類是一個(gè)配置類,他的作用和 bean.xml 是一樣的〉** @author zuiren* @create 2019/8/28* @since 1.0.0*/ @Configuration @ComponentScan(basePackages = {"com"}) public class SpringConfiguration {/*** 用于創(chuàng)建一個(gè) QueryRunner 對象* @param dataSource* @return*/@Bean(name = "runner")//設(shè)置為多例對象@Scope(value = "prototype")public QueryRunner createQueryRunner(DataSource dataSource){return new QueryRunner(dataSource);}/*** 創(chuàng)建數(shù)據(jù)源對象* @return*/@Bean(name = "dataSource")public DataSource createDataSource(){ComboPooledDataSource ds=new ComboPooledDataSource();try {ds.setDriverClass("com.mysql.cj.jdbc.Driver");//?useSSL=false&serverTimezone=GMT 我這里不加會出錯ds.setJdbcUrl("jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=GMT");ds.setUser("root");ds.setPassword("root");}catch (PropertyVetoException e){e.printStackTrace();}return ds;} }2.測試類
package com.test;import com.config.SpringConfiguration; import com.domain.Account; import com.service.IAccountService; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;import java.util.List;/*** 描述:* 〈使用 junit 單元測試:測試我們配置〉** @author zuiren* @create 2019/8/28* @since 1.0.0*/ public class AccountServiceTest {//1.獲取容器ApplicationContext ac=null;//2.得到業(yè)務(wù)層對象IAccountService as=null;@Beforepublic void init(){//1.獲取容器ac=new AnnotationConfigApplicationContext(SpringConfiguration.class);//2.得到業(yè)務(wù)層對象as=ac.getBean("accountService",IAccountService.class);}@Testpublic void testFindAllAccount(){//3.執(zhí)行方法List<Account> accounts = as.findAllAccount();for (Account account:accounts){System.out.println(account);}}@Testpublic void testFindAccountById(){//3.執(zhí)行方法Account account=as.findAccountById(1);System.out.println(account);}@Testpublic void testSaveAccount(){//3.執(zhí)行方法Account account=new Account();account.setName("卡茲克");account.setMoney(300);as.saveAccount(account);}@Testpublic void testUpdateAccount(){}@Testpublic void testDeleteAccount(){} }轉(zhuǎn)載于:https://www.cnblogs.com/zuiren/p/11437511.html
總結(jié)
以上是生活随笔為你收集整理的07-XML 文件注解开发的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 06-基于 XML 和注解 的 IOC
- 下一篇: asp.net ajax控件工具集 Au