目前都是用springboot來解決一些日常需求,有一段時間沒用springmvc了,在這裡總結一下,當作回顧。同時,springmvc也是java背景基礎,值得複習。
bean中的一些配置
scope配置應用情境:
1,action對象:必須是多例;
2,service對象:單例;
3,dao對象:單例
對象生命週期方法:
1,init-method: 初始化方法
2,destroy-method: 銷毀方法
springioc容器的依賴注入:
1,構造方法的注入 用constructor-arg
2,setter方法注入 用property
3,p名稱空間注入 配置xmlms:p="http://www.springframework.org/schema/p" p:customerDao-ref="customerDao" / p:name="eric" 但是前提是必須寫好set方法
4,spEL運算式:springEL運算式,文法變成#{} 例如<property name="customerDao" value="#{customerDao}"/>
注入不同資料類型:
1,普通資料類型 <value> 或者value屬性
2,javaBean對象:<ref>或者ref屬性
3,數組:<array>
4,List集合:<list>
5:map集合:<map>標籤
6,Properties:<props>
如果是用ioc註解方式的話,需要在applicationContext.xml 中開啟下ioc註解功能,需要加入新的名稱空間,比如在beans中加入
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframwork.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
這樣就引入了context的名稱空間
然後配置下context的組件掃描
<!-- base-package: IOC註解的類所在的包 -->
<context:component-scan base-package="cn.z"/>
這時候只需要在類上加上@Component就能被ioc感知到,預設的類名是小寫字母開頭的。
當然也可以改那個名字,用name屬性就好。
ioc相關的註解:
@Component:建立對象(用於普通的spring項目)
@Repository:和@Component的作用是一樣的,都是用來建立對象(分層項目,Dao層)
@Service:和@Component的作用是一樣的,都是用來建立對象(分層項目,Service層)
@Controller:和@Component的作用是一樣的,都是用來建立對象(分層項目,web層)
@Scope:單例和多例,如果想要多例,可以在類上加@Scope("prototype")
//下面兩個方法要起作用,需要把類設計為單例
@PostConstruct:代替上文配置方式中的init-method
@PreDestroy:代表銷毀的方法
×註解方式可以代替xml方式進行ioc工廠。
依賴注入相關的註解:
@Value:注入普通資料類型,value的話很少直接寫,都是通過載入設定檔的方式,在applicationContext.xml 中配一下<context:property-placeholder location="classpath:jdbc.properties"/> 就好,然後在實際代碼中@Value("{url}")這樣引入外部的變數就好。
@Autowired:自動根據類型注入,autowired是注入一個對象,比如:
@Autowired
private CunstomerDao customerDao; // 直接就注入了,是直接賦值給變數,(無需提供構造方法或setter方法),底層直接走反射,賦值給customerDao
通俗來說就是他會去找誰實現了customerDao這個介面,然後看那些impl是不是配置了@Controller這樣的註解,如果配置了,就認為是夾在到ioc容器中了,然後就直接去取就行了。
注意:如果spring環境中沒有任何一個CustomerDao的類型,那麼會注入失敗。
如果spring環境中出現了多個impl實現了customerDao,比如第一個是@Contoller(value="customerDao1") 第二個是@Controller(value="customerDao2").....兩個impl ,將會失敗。
如果確實出現了這種情況怎麼辦。 這時候可以使用qulifer,他可以配合autowired使用名稱
@Qulifer:指定注入的對象名稱(也就是id)
使用:@Quelifier(value="customerDao2")
private CustomerDao customerDao;
@Resource:既能夠根據類型自動注入,也可以根據對象名稱自動注入
用法:如果有兩個實作類別
@Resource(name="customerDao1")
private CustomerDao customerDao;
@Autowired和@Resource的區別:
1,注入的方式不一樣,autowired只能根據類型注入,如果需要根據名稱進行注入需要@Qualifier註解的配合,而@Resource既能夠根據類型自動注入,也可以根據對象名稱注入
2,所屬的標準不同
@Autowired來自於spring架構
@Resource來自javaee標準
所以說@Resource會比@Autowired更加標準
所以一般都是用resource註解來注入對象。
spring新註解(零配置註解)
用了這套註解,整個spring開發過程中不需要有任何xml檔案。
@Configuration
作用:配置類,用於代替applicationContext.xml 檔案
例如寫一個配置類,上面寫@Configurable
@ComponentScan
作用:代替<context:component-scan/>標籤
例如@ComponentScan(basePackages={"cn.sm1234"}
這時候在啟動程式中就需要ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfig.class);
@PropertySource
作用:代替<context:property-placeholder/>配置,載入properties設定檔
用的時候可以在SpringConfig配置類前寫@PropertySource(value="classpath:jdbc.properties")
然後在需要用的service類中用@Value("${url}")類引用
@import
如果有很多配置類,那麼可以在主配置類中引用其他配置類,@Import(value={JdbcProperties.class})
@Bean
作用:把一個對象放入springioc容器
用法:比如我在service層要get一個Dao,但是這時候這個dao是不在ioc容器中的。
這時候可以這麼做:
@Bean(name="custDao")
public CustomerDao getDao(){
return new CustomerDaoImpl(();
}
這樣@Bean就有兩個作用,一個是可以執行該方法的代碼,第二個是把該方法的返回對象放入Spring的IOC容器中。
注意@Bean必須是用在方法的頂部,不能用在類上面
@Bean和@Resource或@Autowired的區別
後者是從springioc容器中擷取一個指定類型或者指定名稱的對象。前者是執行某個方法,把方法的返回的對象,放入spring的ioc容器。
spring可以整合junit進行簡化測試類別編寫。
倒入spring-test包
有設定檔的方式
在測試類別上寫:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
然後在測試類別中想拿dao的一個對象直接這樣寫就好了
@Resource
private CustomerDao customerDao;
這樣就已經注入了Dao
這一junit版本不要太低。。不然回報錯。要是用eclipse開發,就替換eclipse的低版本。
零配置的方式
同樣也是在測試類別上寫:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={SpringConfig.class})
然後注入是和之前是一樣的
@Resource(name="customerDao1")
private CustomerDao customerDao;