国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

Spring純注解配置實現(xiàn)代碼示例解析

瀏覽:104日期:2023-08-22 09:34:59

問題

我們發(fā)現(xiàn),之所以我們現(xiàn)在離不開 xml 配置文件,是因為我們有一句很關鍵的配置:<!-- 告知spring框架在,讀取配置文件,創(chuàng)建容器時,掃描注解,依據(jù)注解創(chuàng)建對象,并存入容器中 --><context:component-scan base-package='com.itheima'></context:component-scan>如果他要也能用注解配置,那么我們就離脫離 xml 文件又進了一步。另外,數(shù)據(jù)源和 JdbcTemplate 的配置也需要靠注解來實現(xiàn)。<!-- 配置 dbAssit --><bean class='com.itheima.dbassit.DBAssit'><property name='dataSource' ref='dataSource'></property></bean><!-- 配置數(shù)據(jù)源 --><bean class='com.mchange.v2.c3p0.ComboPooledDataSource'><property name='driverClass' value='com.mysql.jdbc.Driver'></property><property name='jdbcUrl' value='jdbc:mysql:///spring_day02'></property><property name='user' value='root'></property><property name='password' value='1234'></property></bean>

新注解說明

@Configuration

作用:

用于指定當前類是一個 spring 配置類,當創(chuàng)建容器時會從該類上加載注解。獲取容器時需要使用AnnotationApplicationContext(有@Configuration 注解的類.class)。

屬性:

value:用于指定配置類的字節(jié)碼

實例:

示例代碼:

/*** spring 的配置類,相當于 bean.xml 文件* @author * @Company * @Version 1.0*/@Configurationpublic class SpringConfiguration {}

注意:

我們已經(jīng)把配置文件用類來代替了,但是如何配置創(chuàng)建容器時要掃描的包呢?

請看下一個注解。

@ComponentScan

作用:

用于指定 spring 在初始化容器時要掃描的包。作用和在 spring 的 xml 配置文件中的:

<context:component-scan base-package='com.itheima'/>是一樣的。

屬性:

basePackages:用于指定要掃描的包。和該注解中的 value 屬性作用一樣。

示例代碼:

/*** spring 的配置類,相當于 bean.xml 文件* @author * @Company * @Version 1.0*/@Configuration@ComponentScan('com.itheima')public class SpringConfiguration {}

注意:

我們已經(jīng)配置好了要掃描的包,但是數(shù)據(jù)源和 JdbcTemplate 對象如何從配置文件中移除呢?請看下一個注解。

@Bean

作用:

該注解只能寫在方法上,表明使用此方法創(chuàng)建一個對象,并且放入 spring 容器。

屬性:

name:給當前@Bean 注解方法創(chuàng)建的對象指定一個名稱(即 bean 的 id)。

/*** 連接數(shù)據(jù)庫的配置類* @author * @Company * @Version 1.0*/public class JdbcConfig {/*** 創(chuàng)建一個數(shù)據(jù)源,并存入 spring 容器中* @return*/@Bean(name='dataSource')public DataSource createDataSource() {try {ComboPooledDataSource ds = new ComboPooledDataSource();ds.setUser('root');ds.setPassword('1234');ds.setDriverClass('com.mysql.jdbc.Driver');ds.setJdbcUrl('jdbc:mysql:///spring_day02');return ds;} catch (Exception e) {throw new RuntimeException(e);}}/*** 創(chuàng)建一個 DBAssit,并且也存入 spring 容器中* @param dataSource* @return*/@Bean(name='runner')@Scope('prototype') //配置QueryRunner為多列對象 防止多個dao調(diào)用使線程混亂public QueryRunner createDBAssit(DataSource dataSource) {return new DBAssit(dataSource);}}

注意:

我們已經(jīng)把數(shù)據(jù)源和 DBAssit 從配置文件中移除了,此時可以刪除 bean.xml 了。

但是由于沒有了配置文件,創(chuàng)建數(shù)據(jù)源的配置又都寫死在類中了。如何把它們配置出來呢?

請看下一個注解。

@PropertySource

用于加載.properties 文件中的配置。例如我們配置數(shù)據(jù)源時,可以把連接數(shù)據(jù)庫的信息寫到properties 配置文件中,就可以使用此注解指定 properties 配置文件的位置。

屬性:

value[]:用于指定 properties 文件位置。如果是在類路徑下,需要寫上 classpath:

配置:

/*** 連接數(shù)據(jù)庫的配置類* @author 黑馬程序員* @Company http://www.ithiema.com* @Version 1.0*/public class JdbcConfig {@Value('${jdbc.driver}')private String driver;@Value('${jdbc.url}')private String url;@Value('${jdbc.username}')private String username;@Value('${jdbc.password}')private String password;/*** 創(chuàng)建一個數(shù)據(jù)源,并存入 spring 容器中* @return*/@Bean(name='dataSource')public DataSource createDataSource() {try { ComboPooledDataSource ds = new ComboPooledDataSource(); ds.setDriverClass(driver); ds.setJdbcUrl(url); ds.setUser(username); ds.setPassword(password); return ds; } catch (Exception e) { throw new RuntimeException(e); }}}jdbc.properties 文件:jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/day44_ee247_springjdbc.username=rootjdbc.password=1234

注意:

此時我們已經(jīng)有了兩個配置類,但是他們還沒有關系。如何建立他們的關系呢?請看下一個注解。

@Import

作用:

用于導入其他配置類,在引入其他配置類時,可以不用再寫@Configuration 注解。當然,寫上也沒問題。

屬性:

value[]:用于指定其他配置類的字節(jié)碼。

示例代碼:

@Configuration@ComponentScan(basePackages = 'com.itheima.spring')@Import({ JdbcConfig.class})public class SpringConfiguration {}@Configuration@PropertySource('classpath:jdbc.properties')public class JdbcConfig{}

通過注解獲取容器:

ApplicationContext ac =new AnnotationConfigApplicationContext(SpringConfiguration.class);

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: Spring
相關文章:
主站蜘蛛池模板: 北流市| 娄底市| 宁化县| 砀山县| 大理市| 越西县| 奉贤区| 天峻县| 同仁县| 碌曲县| 崇阳县| 兴安县| 罗甸县| 敖汉旗| 吴江市| 霍州市| 亚东县| 晋州市| 阿合奇县| 澳门| 株洲县| 江阴市| 郸城县| 苏州市| 青岛市| 富民县| 齐河县| 仲巴县| 儋州市| 淅川县| 龙川县| 枣阳市| 新邵县| 新和县| 磴口县| 响水县| 曲靖市| 岐山县| 萝北县| 上林县| 兴隆县|