當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
大数据WEB阶段Spring框架(二)简化配置的操作
生活随笔
收集整理的這篇文章主要介紹了
大数据WEB阶段Spring框架(二)简化配置的操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Spring簡化配置的操作
零、復習
一、Parent和abstract標簽
parent指定父類是誰 , 如果需要獲取父類中自動注入的值 , 必須加上此屬性 。
/*** 用來測試parent標簽* 簡化數據源的配置*/protected DataSource dataSource;public void setDataSource(DataSource dataSource) {this.dataSource = dataSource;}子類中的代碼 ProductDaoImpl public class ProductDaoImpl extends BaseDao implements ProductDao{/*private DataSource dataSource;public void setDataSource(DataSource dataSource) {this.dataSource = dataSource;}*/@Overridepublic void add() {dataSource.getConn();System.out.println("添加商品");}}public class UserDaoImpl extends BaseDao implements UserDao { /* private DataSource dataSource;public void setDataSource(DataSource dataSource) {this.dataSource = dataSource;}*/@Overridepublic void add() {dataSource.getConn();System.out.println("添加用戶");}} <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "><bean id="datasource" class="datasource.DataSource"></bean><bean id="baseDao" class="dao.BaseDao"><property name="dataSource" ref="datasource"></property></bean><!--parent配置的形式指明子父級關系 --><bean id="userDao" class="dao.UserDaoImpl" parent="baseDao"></bean><bean id="productDao" class="dao.ProductDaoImpl" parent="baseDao"></bean> </beans>二、autowire屬性
兩種方式:
byType: 通過類型進行自動注入 , 找到注入 , 找不到注入null ,找到多個報錯(注意 , bean的class是唯一的 , 否則系統不知道是哪個bean)
默認的自動注入
如果需要注入的配置比較多時 , 可以在beans標簽中同一配置默認的注入方式 beans中添加 default-autowire=“byName” <bean id="person" class="bean.Person" autowire="default"></bean>三、屬性注解
導入約束文件 后開啟注解功能
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"><!--啟用注解功能 --><context:annotation-config/> </beans>給屬性添加注解
@autowire
屬性自動注入 , 實體類中在屬性上加上@Autowire , 如果需要指定名稱則在再加一行@Qualifier(value=”XXX”)
public class Person{@Autowire@Qulifier(value="xxx")private Dog dog;}@Resource
四、類的注解
使用掃描機制
<!--使用類掃描器 包含了屬性注解所以寫一個就可以了 base-package="bean,dao,service"可以寫多個包中間用“,”號隔開 不能有空格 --> <context:component-scan base-package="bean,dao,service"/>@Component – 類注解
@Component 或@Component(value="person") public class Person{}五、關于注解了解內容
@Component(value="personService") @Scope(value="prototype") //改為多例 @Lazy //懶加載 public class PersonServiceImpl implements PersonService {@PostConstruct//使該方法作為這個bean生命周期中的初始化方法 , 在構造函數執行后執行 , 生命周期類型的函數 ,只在bean是單例時起作用 。 bean默認是單例的public void init(){System.out.println("這是一個初始化方法");} //聲生命周期中的銷毀方法 , 在Spring容器銷毀前執行 @PreDestory public void destory(){ }六、關于注解賦值
基本類型賦值
@Value(value="男") private String sex;讀取外部properties文件
${key}
Properties 文件內容
#key = value
name=\u5218\u6631\u6C5F
age=18
@Value(value=”${name}”)
private String name;
@Value(value=”${age}”)
private int age;
給set、map、list賦值
配置文件的頭
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"></beans>在beans標簽中添加
<util:list id="list"><value>2</value><value>3</value><value>4</value><value>5</value></util:list><util:set id="set"><value>222</value><value>333</value><value>444</value></util:set><util:map id="map"><entry key="1" value="包頭"></entry><entry key="2" value="北京"></entry></util:map> <util:properties id="properties"><prop key="name">李白</prop><prop key="age">18</prop></util:properties>實體類中獲取 注入屬性
#{@id}
@Value(value="${name}") private String name;@Value(value="${age}") private int age;@Value(value="#{@list}") private List<Integer> list;@Value(value="#{@map}") private Map<Integer,String> map;@Value(value="#{@set}") private Set<String> set;@Value(value="#{@properties}") private Properties properties;總結
以上是生活随笔為你收集整理的大数据WEB阶段Spring框架(二)简化配置的操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大数据WEB阶段Spring框架(一)I
- 下一篇: 计算机视觉之OpenCV教程 ---Ma