java项目功能详情介绍,JAVAEE—spring的详细介绍
一、spring介紹
1.三層架構中spring位置
2.spring一站式框架
正是因為spring框架性質是屬于容器性質的.
容器中裝什么對象就有什么功能.所以可以一站式.
不僅不排斥其他框架,還能幫其他框架管理對象.
aop支持、ioc思想、spring jdbc、aop 事務、junit 測試支持
二、spring搭建
1.導包
日志包:com.springsource.org.apache.commons.logging-1.1.1.jar
可選:com.springsource.org.apache.log4j-1.2.15.jar(老版本要導入的,導入可以保證一定能運行)
2.創建一個對象
public class User {private String name;private Integer age; public String getName() {return name;
}public void setName(String name) {this.name = name;
}public Integer getAge() {return age;
}public void setAge(Integer age) {this.age = age;
}
}
3.書寫配置注冊對象到容器
位置任意(建議放到src下)
配置文件名任意(建議applicationContext.xml)
導入約束:
然后編輯applicationContext.xml
進入編輯后點擊add,導入xsi
添加完xsi后,再次點擊add,指定一個新的命名空間
然后選擇剛剛導入的xsd
點擊OK,回到剛剛的頁面,設置命名空間的名字(可以直接復制location Hint的前半段),prefix空著即可
點擊OK,顯示為下面的界面,就說明導入成功了。
書寫applicationContext.xml:
4.代碼測試
@Testpublic void fun1(){ //1 創建容器對象ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");//2 向容器"要"user對象User u = (User) ac.getBean("user");//3 打印user對象 System.out.println(u);
}
三、spring概念
1.思想
1.1 ioc
1.2 di
2.applicationContext&BeanFactory
2.1 BeanFactory接口
spring原始接口.針對原始接口的實現類功能較為單一;
BeanFactory接口實現類的容器.特點是每次在獲得對象時才會創建對象。
2.2 ApplicationContext
每次容器啟動時就會創建容器中配置的所有對象.并提供更多功能。
從類路徑下加載配置文件:ClassPathXmlApplicationContext
2.3 結論
結論:web開發中,使用applicationContext. 在資源匱乏的環境可以使用BeanFactory.
四、spring配置詳解
1.Bean元素
2.Bean元素進階
2.1 scope屬性
singleton(默認值):單例對象.被標識為單例的對象在spring容器中只會存在一個實例
prototype:多例原型.被標識為多例的對象,每次再獲得才會創建.每次創建都是新的對象.整合struts2時,ActionBean必須配置為多例的.
request:web環境下.對象與request生命周期一致.
session:web環境下,對象與session生命周期一致.
2.2 生命周期屬性
init-method:配置一個方法作為生命周期初始化方法.spring會在對象創建之后立即調用.
destory-method:配置一個方法作為生命周期的銷毀方法.spring容器在關閉并銷毀所有容器中的對象之前調用.
3.spring創建對象的方式
class="cn.itcast.b_create.UserFactory"
factory-method="createUser" >
factory-bean="userFactory"factory-method="createUser2" >
class="cn.itcast.b_create.UserFactory" >
4.spring的分模塊配置
五、spring屬性注入
1.注入方式
1.1 set方法注入(重中之重)
1.2 構造函數注入(重點)
1.3 p名稱空間注入
1.4 spel注入
2.復雜類型注入
2.1 數組
tomjerry
2.2 List
jackrose
2.3 Map
2.4 Properties
com.jdbc.mysql.Driverroot1234
六、練習:將spring容器應用到struts-crm項目
管理Service對象以及Dao對象
1.導包(4+2),再加1
再加1指的是:spring-web-4.2.4.RELEASE.jar(因為要用到web的監聽)
2.將Service對象以及Dao對象配置到spring容器
3.在Action中獲得容器中的Service對象
3.1 web.xml中配置容器隨項目啟動
org.springframework.web.context.ContextLoaderListener
contextConfigLocationclasspath:applicationContext.xml
3.2 在Action中獲得容器
//獲得spring容器=>從Application域獲得即可 //1 獲得servletContext對象ServletContext sc = ServletActionContext.getServletContext();//2.從Sc中獲得ac容器WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);//3.從容器中獲得CustomerServiceUserService us = (UserService) ac.getBean("userService");
4.管理容器在項目中的生命周期
下面錯誤的示范.導致每次請求都創建新的容器
//創建容器對象ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");//獲得cs(customerService對象)CustomerService cs = (CustomerService) ac.getBean("customerService");
總結
以上是生活随笔為你收集整理的java项目功能详情介绍,JAVAEE—spring的详细介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html5在线api,HTML5+ AP
- 下一篇: Qt中Tcp通信的简单使用二