07-XML 文件注解开发
目錄
- 注解
- 一、@Configuration
- @Import
- 二、@ComponentScan
- 三、@bean
- 1.Config完整代碼
- 2.測試類
不論是 xml 開發或者注解開發都有一個問題是,我們還是不得不寫 bean.xml 文件,這次解決這個問題
創建 spring06 項目
將上一個項目導入
注解
@Configuration
- 作用:指定當前類是一個配置類,它的作用和 bean.xml 是一樣的
- 細節:當配置類作為 AnnotationConfigApplicationContext 對象創建的參數時,該注解可以不寫
@ComponentScan
作用:用于通過注解指定 spring 在創建容器時要掃描的包。
屬性:
- value:
- 她和 basePackages 的作用是一樣的,都是用于指定創建容器要掃描的包。
- 我們使用此注解就等同于在 xml 中配置了:
源碼中,參數有value、basePackages,上面的注解表示,這兩個相等
- value:
@bean
- 作用:
- 用于把當前方法的返回值作為 bean 對象存入 spring 的 ioc 容器中
- 屬性:
- name:
- 用于指定 bean 的 id,當不寫時,默認值是當前方法的名稱
- name:
- 細節:
- 當我們使用注解配置方法時,如果方法有參數,spring 框架會去容器中查找有沒有可用的 bean 對象。
- 查找的方式和 Autowired 的注解作用是一樣的
@import
- 作用:用于導入其他配置類
- 屬性:
- value:用于指定其他配置類的字節碼
- 當我們使用Import 的注解之后,有 Import 注解的類就父類配置類,而導入的都是子類配置類
@PropertySource
- 作用:用于指定properties文件的位置
- 屬性:
- value:指定文件的名稱和路徑
- 關鍵字:classpath,表示類路勁下
- value:指定文件的名稱和路徑
一、@Configuration
作用:指定當前類是一個配置類
細節:當配置類作為 AnnotationConfigApplicationContext 對象創建的參數時,該注解可以不寫
何時可以不寫?
在測試類中注入可不寫
new AnnotationConfigApplicationContext(SpringConfiguration.class);何時必須寫?
如果我們把里面的配置信息轉移到 JdbcConfig 類中,JdbcConfig 類還在掃描的包中
SpringConfiguration 里面內容為空,JdbcConfig 類就必須寫@Configuration,或者也實行注入,實行注入的話,SpringConfiguration 與 JdbcConfig 同級
ac=new AnnotationConfigApplicationContext(SpringConfiguration.class, JdbcConfig.class);@Import
使用 Import 標簽,可以不用寫@Configuration 與注入
@Configuration @Import(JdbcConfig.class) @ComponentScan(basePackages = {"com"}) public class SpringConfiguration {二、@ComponentScan
作用:用于通過注解指定 spring 在創建容器時要掃描的包。
屬性:
? value:她和 basePackages 的作用是一樣的,都是用于指定創建容器要掃描的包。
? 我們使用此注解就等同于在 xml 中配置了:
<context:component-scan base-package="com"/>源碼中,參數有value、basePackages,上面的注解表示,這兩個相等
而且這兩個屬性是數組
@AliasFor("basePackages")String[] value() default {};@AliasFor("value")String[] basePackages() default {};當下面代碼出場了,那么 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
/*** 創建數據源對象* @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;}細節:
- 當我們使用注解配置方法時,如果方法有參數,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;/*** 描述:* 〈該類是一個配置類,他的作用和 bean.xml 是一樣的〉** @author zuiren* @create 2019/8/28* @since 1.0.0*/ @Configuration @ComponentScan(basePackages = {"com"}) public class SpringConfiguration {/*** 用于創建一個 QueryRunner 對象* @param dataSource* @return*/@Bean(name = "runner")//設置為多例對象@Scope(value = "prototype")public QueryRunner createQueryRunner(DataSource dataSource){return new QueryRunner(dataSource);}/*** 創建數據源對象* @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.得到業務層對象IAccountService as=null;@Beforepublic void init(){//1.獲取容器ac=new AnnotationConfigApplicationContext(SpringConfiguration.class);//2.得到業務層對象as=ac.getBean("accountService",IAccountService.class);}@Testpublic void testFindAllAccount(){//3.執行方法List<Account> accounts = as.findAllAccount();for (Account account:accounts){System.out.println(account);}}@Testpublic void testFindAccountById(){//3.執行方法Account account=as.findAccountById(1);System.out.println(account);}@Testpublic void testSaveAccount(){//3.執行方法Account account=new Account();account.setName("卡茲克");account.setMoney(300);as.saveAccount(account);}@Testpublic void testUpdateAccount(){}@Testpublic void testDeleteAccount(){} }轉載于:https://www.cnblogs.com/zuiren/p/11437511.html
總結
以上是生活随笔為你收集整理的07-XML 文件注解开发的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 06-基于 XML 和注解 的 IOC
- 下一篇: 基于ASP.Net Core开发的一套通