1. 程式人生 > >Spring基礎知識1--環境搭建、bean創建、依賴註入、註解註入

Spring基礎知識1--環境搭建、bean創建、依賴註入、註解註入

管理 entry 知識 spring容器 get not string 方法 person

一、Spring兩大核心內容

1、控制反轉IOC/DI: 應用本身不負責對象的創建和維護,對象和依賴對象的創建完全交給容器管理。

2、AOP(面向切面編程):通過預編譯的方式,在運行期通過動態代理的方式來實現的一種技術

  (1)在項目中使用AOP管理事務,事務的開啟,提交,回滾

  (2)在項目中管理事務的傳播特性。

二、Spring的環境搭建

1、導包: 技術分享圖片

2、創建Spring的配置文件 ApplicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 3
xmlns="http://www.springframework.org/schema/beans" 4 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 7 8 9 "> 10 11 </beans>

3、創建bean

<bean id="person" class
="com.tx.model.Person" >

4、創建Spring容器

1 @Test
2     public void test1(){
3         ApplicationContext ctx = new ClassPathXmlApplicationContext("ApplicationContext.xml");
4         Person p = (Person) ctx.getBean("person");
5         System.out.println(p);
6     }

三、Bean的創建方式

1、使用構造器創建(常用)

  使用構造器創建bean,bean必須有默認的構造器

2、使用靜態工廠方式

  提供靜態工廠:

1 public class FactoryBean {
2     /**
3      * 必須是靜態方法
4      * @return
5      */
6     public static User createUser(){
7         return new User();
8     }
9 }

  進行配置:

1 <!-- id:唯一標識
2         class:靜態工廠的類
3         factory-method:靜態工廠的方法
4      -->
5 <bean id="user" class="com.rl.spring.factory.FactoryBean" factory-method="createUser"></bean>

3、實例工廠方式

  實例工廠:

public class FactoryBean1 {
    public User createUser(){
        return new User();
    }
}

  配置:

1 <!-- 通過Spring來定義實例工廠 -->
2     <bean id="factoryBean" class="com.rl.spring.factory.FactoryBean1"></bean>
3 <!-- 指定要創建的bean
4     factory-bean:指定實例工廠類,
5     factory-method:工廠的創建bean的方法
6  -->
7 <bean id="user" factory-bean="factoryBean" factory-method="createUser"></bean>

4、延遲加載

在默認情況下,所有的bean都不是延遲加載的,在spring容器創建的時候把bean創建出來,所有getBean可以直接取出。。 可通過lazy-init來控制,默認是false, 如果設置為true,則在getBean時,才創建bean

5、bean的作用域

scope:singleton單例的: 可以設置延遲加載 和 非延遲加載

scope:prototype多例的:只能延遲加載

6、bean的生命周期

與容器一致,容器銷毀即銷毀

7、依賴註入:

  (1)、常量註入

1 <bean id="person" class="com.tx.model.Person"  >
2     <!-- 常量註入 -->
3     <property name="id" value="1"></property>
4     <property name="name" value="張三"></property>
5 </bean>

  (2)、構造器註入

 1 <bean id="user" class="com.rl.spring.model.User">
 2     <!-- 
 3         index:構造方法的參數的索引順序
 4         type:構造方法的參數的類型(不是必須 的)
 5         value:值
 6      -->
 7     <!-- <constructor-arg index="0" type="java.lang.Integer" value="2"/>
 8     <constructor-arg index="1" type="java.lang.String" value="張三"/>
 9     <constructor-arg index="2" type="java.lang.String" value="666"/> -->
10     <constructor-arg index="0"  value="2"/>
11     <constructor-arg index="1"  value="張三"/>
12     <constructor-arg index="2"  value="666"/>
13 </bean>

  (3)、外部bean註入,必須提供set方法

1 <bean id="userDao" class="com.tx.spring.dao.impl.UserDaoImpl">
2     <property name="dataSource" ref="dataSource"></property>
3 </bean>
4 <bean id="userService" class="com.tx.spring.service.impl.UserServiceImpl">
5 <property name="userDao" ref="userDao"></property>
6 </bean>

  (4)、內部bean註入,必須提供set方法

  (5)、集合註入,必須提供set方法

 1 <bean id="ci" class="com.rl.spring.model.CollectionInjection">
 2         <property name="set">
 3             <set>
 4                 <value>football</value>
 5                 <value>basketball</value>
 6             </set>
 7         </property>
 8         <property name="list">
 9             <list>
10                 <value>male</value>
11                 <value>female</value>
12             </list>
13         </property>
14         <property name="map">
15             <map>
16                 <entry key="key1" value="value1"></entry>
17                 <entry key="key2" value="value2"></entry>
18             </map>
19         </property>
20         <property name="prop">
21             <props>
22                 <prop key="name">張三</prop>
23                 <prop key="job">程序員</prop>
24             </props>
25         </property>
26     </bean>

8、註解方式註入

  (1)、引包:annocation包,如果4.2還需要引入aop包

  (2)、引入約束文件spring-context-4.2.xsd

  (3)、開啟註解驅動

<!-- 開啟註解的驅動 -->
<context:annotation-config/>

  @Resource註解: 放在屬性上,也可以放在set方法上。 不需要提供set方法。 默認先根據名稱找,如果沒有按照接口與實現類的關系找,如果有多個實現類需要指定name

  @Autowired註解:放在屬性上,也可放在set方法上,根據接口與實現類的關系找,如果多個實現類需要配合 @Qulifier的註解指定value

9、Spring掃描器註解  

  @Controller:控制層的類

  @Service:服務層的類

  @Repository:數據層的類

  @Component:無法分層的類上

  

Spring基礎知識1--環境搭建、bean創建、依賴註入、註解註入