1.1 基於註解的IOC配置 既註解配置和xml配置要實現的功能都是一樣的,都是要降低程式間的耦合.只是配置的形式不一樣. 1.2 環境搭建 1.2.1 第一步:拷貝必備的jar包 需要多拷貝一個spring-aop-4.2.4.RELEASE.jar 1.2.2 創建xml文件,導入約束 ... ...
1.1 基於註解的IOC配置 既註解配置和xml配置要實現的功能都是一樣的,都是要降低程式間的耦合.只是配置的形式不一樣. 1.2 環境搭建 1.2.1 第一步:拷貝必備的jar包 需要多拷貝一個spring-aop-4.2.4.RELEASE.jar 1.2.2 創建xml文件,導入約束 <?xml version="1.0" encoding="UTF-8"?> <!-- 導入schema 約束的位置在: ..\spring-framework-4.2.4.RELEASE\docs\spring-framework-reference\html\xsd-configuration.html 文件中。 註意:要導入schema約束 --> <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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> </beans> 1.2.3 使用@Component註解配置管理的資源 /** * 客戶的業務層實現類 * @author zhy * @Component(value="customerService") public class CustomerServiceImpl implements ICustomerService { @Override public void saveCustomer() { System.out.println("執行了保存客戶"); } } 1.2.4 第四步在spring的配置文件中開啟spring對註解ioc的支持 <!--告知spring框架在,讀取配置文件,創建容器時,掃描註解,依據註解創建對象,並存入容器中.> <context:component-scan base-package="com.baidu"></context:component-sacn> 1.3 常用註解 1.3.1 用於創建對象的 相當於 : <bean id="" class=""> 1.3.1.1 @Component 作用: 把資源讓spring來管理.相當於在xml配置一個bean. 屬性: value: 指定bean的id.如果不指定value屬性,預設bean的id是當前類的類名.首字母小寫. 1.31.2 @Controller @Service @Repository 們三個註解都是針對一個的衍生註解,他們的作用及屬性都是一模一樣的。 他們只不過是提供了更加明確的語義化。 @Controller:一般用於表現層的註解。 @Service:一般用於業務層的註解。 @Repository:一般用於持久層的註解。 細節:如果註解中有且只有一個屬性要賦值時,且名稱是value,value在賦值是可以不寫。 1.3.2 用於註入數據的 相當於 : <property name="" ref=""> 或者 <property name="" value=""> 1.3.2.1@Autowired 作用: 自動按照類型註入。當使用註解註入屬性時,set方法可以省略。它只能註入其他bean類型。當有多個類型匹配時,使用要註入的對象變數名稱作為bean的id,在spring容器查找,找到了也可以註入成功。找不到就報錯。 /** * @Autowired 自動裝配,自動按類型註入對象 * <bean id="userService" class="com.baidu.demo2.UserServiceImpl" scope="" init-method="init"> * <property name="userDao" ref="ud"/> * </bean> * * @Autowired @Qualifier(value="userDao") 這2個註解必須要一起使用,按id名稱註入 @Resource 是Java提供註解,Spring容器支持該註解。可以通過name在容器中查找指定名稱的對象 * 1.3.2.2@Qualifier 作用: 在自動按照類型註入的基礎之上,再按照Bean的id註入。它在給欄位註入時不能獨立使用,必須和@Autowire一起使用;但是給方法參數註入時,可以獨立使用。 屬性: value:指定bean的id。 1.3.2.3@Resource 作用: 直接按照Bean的id註入。它也只能註入其他bean類型。 屬性: name:指定bean的id。 1.3.2.4@Value 作用: 註入基本數據類型和String類型數據的 屬性: value:用於指定值 1.3.3用於改變作用範圍的: 相當於:<bean id="" class="" scope=""> 1.3.3.1@Scope 作用: 指定bean的作用範圍。 屬性: value:指定範圍的值。 取值:singleton prototype request session globalsession 1.3.4和生命周期相關的:(瞭解) 相當於:<bean id="" class="" init-method="" destroy-method="" /> 1.3.4.1@PostConstruct 作用: 用於指定初始化方法。 1.3.4.2@PreDestroy 作用: 用於指定銷毀方法。 1.3.5代碼示例 業務層代碼: /** * 客戶的業務層介面 */ public interface ICustomerService { /** * 保存客戶 * @param customer */ void saveCustomer(); } /** * 客戶的業務層實現類 */ //作用就相當於在xml中配置了一個bean標簽,該註解有value屬性,含義是bean的id。 //不寫的時候,預設的id是:當前類名,且首字母小寫。即:customerServiceImpl @Component(value="customerService") @Scope(value="singleton") public class CustomerServiceImpl implements ICustomerService { // @Autowired // 自動按照數據類型註入,拿著當前變數的數據類型在spring的容器中找,找到後,給變數賦值。 // 當有多個類型匹配時,會使用當前變數名稱customerDao作為bean的id,繼續在容器中找。 // 找到了,也能註入成功。找不到就報錯。 // @Qualifier(value="customerDao2")//在自動按照類型註入的基礎之上,再按照id註入 @Resource(name="customerDao2")//直接按照bean的id註入 private ICustomerDao customerDao = null; @Value("com.mysql.jdbc.Driver")//註入基本類型和String類型數據 private String driver; @Override public void saveCustomer() { System.out.println(driver); customerDao.saveCustomer(); } } 持久層代碼: /** * 客戶的持久層介面 */ public interface ICustomerDao { /** * 保存客戶 */ void saveCustomer(); } /** * 客戶的持久層實現類11111111111111111111 */ @Repository("customerDao1") public class CustomerDaoImpl implements ICustomerDao { @Override public void saveCustomer() { System.out.println("保存了客戶111111111111111111"); } } /** * 客戶的持久層實現類222222222222222222222222 */ @Repository("customerDao2") public class CustomerDaoImpl2 implements ICustomerDao { @Override public void saveCustomer() { System.out.println("保存了客戶2222222222222222222"); } } 測試類代碼: public class Client { public static void main(String[] args) { //1.獲取容器 ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); //2.根據id獲取對象 ICustomerService cs = (ICustomerService) ac.getBean("customerService"); cs.saveCustomer(); } } 配置文件: <?xml version="1.0" encoding="UTF-8"?> <!-- 我們導入約束時,除了昨天的那部分之外,還要單獨導入一個context名稱空間 --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 告知spring框架在通過讀取配置文件創建容器時,掃描的包,並根據包中類的註解創建對象--> <context:component-scan base-package="com.baidu"></context:component-scan> </beans> 1.3.6 關於Spring註解和XML的選擇問題 註解的優勢 : 配置簡單,維護方便(我們找到類,就相當於找到了對應的配置) XML的優勢 : 修改時,不用改源碼.不涉及重寫編譯和部署. Spring管理Bean方式的比較 : 基於XML配置 基於註解配置 Bean定義 <bean id="..." class=".."> @Component 衍生類@Repository @Service @Controller Bean名稱 通過id或name指定 @Component("person") Bean註入 <property>或者通過p命名空間 @Autowired按類型註入 @Qualifier按名稱註入 生命過程, init-method destroy-method @PostConstruct初始化 @PreDestroy銷毀 @Scope設置作用範圍 Bean作用範圍 範圍scope屬性 適合場景 Bean來自第三方,使用其它 Bean的實現類由用戶自己開發 1.5 spring的純註解配置 /** * 客戶的業務層實現類 */ @Configuration//表明當前類是一個配置類 @ComponentScan(basePackages = "com.baidu")//配置要掃描的包 public class SpringConfiguration { } 那麼新的問題又來了,我們如何獲取容器呢? public class Client { public static void main(String[] args) { //1.獲取容器:由於我們已經沒有了xml文件,所以再用讀取xml方式就不能用了。 //這時需要指定載入哪個類上的註解 ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class); //2.根據id獲取對象 ICustomerService cs = (ICustomerService) ac.getBean("customerService"); cs.saveCustomer(); } } 1.5.3新註解說明 1.5.3.1@Configuration 作用: 用於指定當前類是一個spring配置類,當創建容器時會從該類上載入註解。獲取容器時需要使用AnnotationApplicationContext(有@Configuration註解的類.class)。 屬性: value:用於指定配置類的位元組碼 示例代碼: /** * 用於初始化spring容器的配置類 */ @Configuration public class SpringConfiguration{ } 1.5.3.2@ComponentScan 作用: 用於指定spring在初始化容器時要掃描的包。作用和在spring的xml配置文件中的: <context:component-scan base-package="com.baidu"/>是一樣的。 屬性: basePackages:用於指定要掃描的包。和該註解中的value屬性作用一樣。 1.5.3.4@Import 作用: 用於導入其他配置類,在引入其他配置類時,可以不用再寫@Configuration註解。當然,寫上也沒問題。 屬性: value[]:用於指定其他配置類的位元組碼。 示例代碼: @Configuration @ComponentScan(basePackages = "cn.baidu.spring") @Import({ Configuration_B.class}) public class Configuration_A { } @Configuration @PropertySource("classpath:info.properties") public class Configuration_B { } 1.5.3.5@Bean 作用: 該註解只能寫在方法上,表明使用此方法創建一個對象,並且放入spring容器。它就相當於我們之前在xml配置中介紹的factory-bean和factory-method。 屬性: name:給當前@Bean註解方法創建的對象指定一個名稱(即bean的id)。 示例代碼: @Bean(name = "datasource2") public DataSource createDS() throws Exception { ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource(); comboPooledDataSource.setUser("root"); comboPooledDataSource.setPassword("1234"); comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver"); comboPooledDataSource.setJdbcUrl("jdbc:mysql:///spring_ioc"); return comboPooledDataSource; } 單元整合 /** * Spring整合Junit單元測試 * @author Administrator */ @RunWith(value=SpringJUnit4ClassRunner.class) @ContextConfiguration(value="classpath:applicationContext.xml") public class Demo1 { // 測試哪個對象,可以使用resource註解把對象註入進來 @Resource(name="userService") private UserService userService; @Resource(name="userDao") private UserDao userDao; /** * Spring 整合Juint單元測試的方法 */ @Test public void run3(){ userService.save(); } @Test public void run4(){ // userService.update(); userDao.save(); } 使用XML方式完成IOC的入門 1. 導入jar包 2. 編寫介面和實現類 3. 編寫配置文件,管理實現類 4. 編寫入門的程式 使用註解的方式完成IOC的入門 1. 導入jar包 2. 編寫介面和實現類 3. 編寫applicationContext.xml配置文件,目的 : 讓註解生效 4. 在實現類上編寫註解 5. 編寫入門的程式 1. 管理類的 @Component 所有類都可以使用 @Controller Web層使用的註解,就是Action使用的. @Service 業務層使用的註解 @Repository 持久層使用的註解 2. 依賴註入的註解 (set方法可以省略不寫) @Value 給普通類型屬性註入值(String int double) @Resource 給引用類型註入值的,強調 : 該註解的屬性時name @Scope 對象作用範圍,預設單例的. Spring整合WEB ServletContext對象,伺服器啟動就創建,伺服器關閉就銷毀. 監聽器 : 監聽ServletContext域對象的創建和銷毀的. ServletContextListener監聽器 , init destory 總結 : 伺服器啟動後,Spring創建對象,Spring創建Web版本的工廠,把工廠保存到ServletContext域對象中. 編寫 : 從ServletContext對象中獲取到Web版本的工廠,從工廠中獲取到Service對象,調用方法.