Spring學習筆記_day01_ioc

本文爲博主辛苦總結,但願本身之後返回來看的時候理解更深入,也但願能夠起到幫助初學者的做用.

轉載請註明 出自 : luogg的博客園 謝謝配合!

Spring_day01

spring是一站式的框架,對EE的三層有每一層的解決方案,Web層,業務層,數據訪問層.Web層:SpringMVC , 持久層:JDBC Template , 業務層 : Spring的Bean管理java

IOC(Inverse of Control) : 反轉控制,將對象的建立交由Spring來完成,反射+配置文件實現.web

AOP(Aspect Oriented Programming) : 面向切面編程.spring

IOC思想 : 工廠+反射+配置文件,底層原理就是提供一個工廠Bean,而後提供一個配置文件,把一些類全都配置在配置文件中,經過xml解析得到類的全路徑,從而反射得到類的實例.編程

spring優勢

方便解耦,簡化開發服務器

  • Spring就是一個大工廠,能夠將全部對象建立和依賴關係維護,交給Spring管理

AOP編程的支持session

  • Spring提供面向切面編程,能夠方便的實現對程序進行權限攔截、運行監控等功能

聲明式事務的支持app

  • 只須要經過配置就能夠完成對事務的管理,而無需手動編程
    方便程序的測試框架

  • Spring對Junit4支持,能夠經過註解方便的測試Spring程序
    方便集成各類優秀框架eclipse

  • Spring不排斥各類優秀的開源框架,其內部提供了對各類優秀框架(如:Struts、Hibernate、MyBatis、Quartz等)的直接支持
    下降JavaEE API的使用難度分佈式

  • Spring 對JavaEE開發中很是難用的一些API(JDBC、JavaMail、遠程調用等),都提供了封裝,使這些API應用難度大大下降

IOC和DI區別

IOC:控制反轉,將對象的建立權交給Spring處理.

DI:依賴注入,在Spring建立對象過程當中,把對象依賴屬性注入到類中.經過property標籤.

Eclipse配置XML提示

window->搜索xml catlog->add 找到schame的位置,將複製的路徑copy指定位置,選擇schame location.

Bean的3中實現方式

  • 1.默認狀況經過無參構造方法實現
  • 2.經過靜態方法實例化
  • 3.經過實例工廠實例化

Bean標籤的其餘配置

id和name的區別

id遵照xml的id的約束,保證這個屬性值是惟一的,且必須以字母開頭,name沒有這些要求,

若是bean標籤上沒有id,那麼name能夠做爲id.

scope屬性

scope屬性 :

  • singleton :單例的.(默認的值.)
  • prototype :多例的.
  • request :web開發中.建立了一個對象,將這個對象存入request範圍,request.setAttribute();
  • session :web開發中.建立了一個對象,將這個對象存入session範圍,session.setAttribute();
  • globalSession :通常用於Porlet應用環境.指的是分佈式開發.不是porlet環境,globalSession等同於session;

實際開發中主要使用singleton,prototype

Bean屬性的注入方式

  • 1.經過構造方法注入
  • 2.經過setter方法注入,最常使用,用property標籤,name,value表示普通屬性,ref表示引用其餘的對象.

若經過構造方法注入,那麼bean標籤中須要使用

<constructor-arg name="name" value="奔馳S400"></constructor-arg>
<constructor-arg name="price" value="1200000"></constructor-arg>

其中,name也能夠換成index,對應的構造方法中的參數的位置.

Bean屬性注入:經過P名稱空間代替property

Spring2.5版本引入了名稱空間p.
p: <屬性名> ="xxx" 引入常量值
p: <屬性名> -ref="xxx" 引用其它Bean對象

引入名稱空間:(引入p命名空間)

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       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">

經過p配置普通屬性和帶有對象的屬性

<bean id="car2" class="cn.itcast.spring3.demo5.Car2" p:name="寶馬" p:price="400000"/>
<bean id="person" class="cn.itcast.spring3.demo5.Person" p:name="童童" p:car2-ref="car2"/>

經過SpEL注入屬性

Spring3.0提供注入屬性方式:

語法:#{表達式}

<bean id="" value="#{表達式}">

<bean id="car2" class="cn.itcast.spring3.demo5.Car2">
        <property name="name" value="#{'大衆'}"></property>
        <property name="price" value="#{'120000'}"></property>
</bean>

<bean id="person" class="cn.itcast.spring3.demo5.Person">
        <!--<property name="name" value="#{personInfo.name}"/>-->
<property name="name" value="#{personInfo.showName()}"/>
        <property name="car2" value="#{car2}"/>直接使用別的bean中的對象
</bean>
    
<bean id="personInfo" class="cn.itcast.spring3.demo5.PersonInfo">
        <property name="name" value="張三"/>
</bean>

集合屬性注入

<!-- 集合的注入 -->
    <bean id="collectionBean" class="com.luogg.demo3.CollectionBean">
        <property name="list">
            <list>
                <value>小花</value>
                <value>小李</value>
            </list>
        </property>
        <property name="map">
            <map>
                <entry key="1" value="洛哥"></entry>
                <entry key="2" value="小美"></entry>
            </map>
        </property>
        <property name="set">
            <set>
                <value>呵呵</value>
                <value>哈哈</value>
            </set>
        </property>
    </bean>

配置文件引入的問題

一種寫法:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean1.xml",」bean2.xml」);
二種方法:在xml中經過import標籤引入
<import resource="applicationContext2.xml"/>

經過註解裝配Bean

Spring2.5 引入使用註解去定義Bean
@Component 描述Spring框架中Bean

Spring的框架中提供了與@Component註解等效的三個註解:

  • @Repository 用於對DAO實現類進行標註
  • @Service 用於對Service實現類進行標註
  • @Controller 用於對Controller實現類進行標註

三個註解爲了後續版本進行加強的.

先去包中掃描這些帶註解的類
<!-- 去掃描註解 裝配的Bean -->
<context:component-scan base-package="com.luogg.demo1"></context:component-scan>

在類頭部經過註解標識這個類是Spring加載的Bean

@Service("helloService")
public class HelloService {
    
    public void sayHello(){
        System.out.println("Hello Spring");
    }
}

最後測試

@Test
    public void test1(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        HelloService hello = (HelloService) ac.getBean("helloService");
        hello.sayHello();
    }

Bean的屬性的注入

普通屬性;

@Value(value="itcast")    
    private String info;

對象屬性:

@Autowired:自動裝配默認使用類型注入.
    @Autowired
    private UserDao userDao;

@Autowired
    @Qualifier("userDao")   --- 按名稱進行注入.    
    private UserDao userDao;
等價於
    @Resource(name="userDao")
    private UserDao userDao;

Bean的其餘屬性的配置

配置Bean初始化方法和銷燬方法:

  • init-method 和 destroy-method.
    @PostConstruct 初始化
    @PreDestroy 銷燬

配置Bean的做用範圍:
@Scope

實際開發中使用XML仍是註解?

XML:

  • bean管理

註解;

  • 注入屬性的時候比較方便.

兩種方式結合;通常使用XML註冊Bean,使用註解進行屬性的注入.

<context:annotation-config/> 在xml中加上這句話能夠識別註解
@Autowired
    @Qualifier("orderDao")
    private OrderDao orderDao;

Spring整合Web開發

正常整合Servlet和Spring沒有問題的
可是每次執行Servlet的時候加載Spring配置,加載Spring環境.

  • 將加載的信息內容放到ServletContext中.ServletContext對象時全局的對象.服務器啓動的時候建立的.在建立ServletContext的時候就加載Spring的環境.
  • ServletContextListener:用於監聽ServletContext對象的建立和銷燬的.
方法

導入;spring-web-3.2.0.RELEASE.jar
在web.xml中配置:

<!-- 服務啓動時候加載spring -->
 <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <!-- 由於配置文件不在web-info下邊,因此須要配置路徑 -->
 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
 </context-param>

修改程序代碼

/*ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");*/
        WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        HelloService hello = (HelloService) ac.getBean("helloService");
        hello.sayHello();

Spring與Junit整合

  • 1.先導入spring-junit包,spring-test-3.2.0.RELEASE.jar
  • 2.在類上標示
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class SpringTest {
    @Autowired
    private HelloService helloService;
    @Test
    public void test1(){
        helloService.sayHello();
    }
}
相關文章
相關標籤/搜索