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="https://www.cnblogs.com/haizai/p/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="" > 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" scope="" init-method="init"> * <property name="userDao" ref="ud"/> * </bean> * * @Autowired @Qualifier(value="https://www.cnblogs.com/haizai/p/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="" scope=""> 1.3.3.1@Scope 作用: 指定bean的作用范圍, 屬性: value:指定范圍的值, 取值:singleton prototype request session globalsession 1.3.4和生命周期相關的:(了解) 相當于:<bean id="" 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="https://www.cnblogs.com/haizai/p/customerService") @Scope(value="singleton") public class CustomerServiceImpl implements ICustomerService { // @Autowired // 自動按照資料型別注入,拿著當前變數的資料型別在spring的容器中找,找到后,給變數賦值, // 當有多個型別匹配時,會使用當前變數名稱customerDao作為bean的id,繼續在容器中找, // 找到了,也能注入成功,找不到就報錯, // @Qualifier(value="https://www.cnblogs.com/haizai/p/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物件,呼叫方法.
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/48366.html
標籤:架構設計
上一篇:使用Docker Compose 部署Nexus后初次登錄賬號密碼不正確,并且在nexus-data下沒有admin,password
下一篇:系統架構設計師視頻教程免費下載
