表格內容-新增動畫和離開動畫
阿新 • • 發佈:2021-01-03
技術標籤:SpringIoc與AOP.xmlredismongodb
<?xml version="1.0" encoding="UTF-8"?><!--============================================================= (一) 載入配置檔案 ==============================================--> <!--載入配置檔案--> <context:property-placeholder location="classpath:db2.properties"/> <!--註解方式 在配置類上加@PropertySource("classpath:db2.propertes") --> <!-- @PropertySource("classpath:db2.propertes") 用於指定載入配置檔案的。--> <!--============================================================= (一) Spring IOC ==============================================--> <!--01 xml 配置檔案--> <!--由於dao維護了一個jdbctemplate成員變數,所以需要注入--> <bean id="jdbctemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> <!--由於dao維護了一個jdbctemplate成員變數,所以需要注入--> <bean id="jdbctemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 建立一個連線池--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"> <property name="jdbcTemplate" ref="jdbctemplate"/> </bean> <!--02 註解方式-開啟注ioc解掃描--> <context:component-scan base-package="com.itheima"/> <!-- 03 純註解開發--> <!-- 建立一個配置類加上@Configuration註解,代替bean.xml配置檔案。 @Configuration 該註解的作用代表了該類是一個配置類,用於取代bean.xml檔案的。 @ComponentScan("com.itheima") 掃描 ioc 相關的註解的 @Respository @Service @Controller @component這些註解的 @component這些註解的 @PropertySource("classpath:db2.propertes") 用於指定載入配置檔案的。 @Bean("dataSource") 方法上一旦新增 @Bean("dataSource")註解,那麼該方法會自動執行, 並且把該方法的返回值儲存到容器中。 --> <!--============================================================= (二) Spring Aop ==============================================--> <!-- 01 xml 配置--> <!--1. 建立目標物件與切面物件--> <!-- 目標物件 --> <bean id="userService" class="com.itheima.service.impl.UserServiceImpl"/> <!--切面物件--> <bean id="logAspect" class="com.itheima.service.aspect.LogAspect"/> <!--2. 使用aop.config標籤把目標物件與切面物件組合到一塊形成一個切面。--> <aop:config> <!-- --> <aop:aspect ref="自定義的切面類"> <!-- 2.1 切入點表示式 : 代表了我需要增強UserServiceImpl的所有方法--> <aop:pointcut id="pt" expression="execution(* com.itheima.service.impl.UserServiceImpl.*(..))"/> <!--2.2 切面 = 通知+切入點 --> <aop:before method="切面類的方法名" pointcut-ref="pt"/> <!--前置通知--> <aop:before method="before" pointcut-ref="pt"/> <!--後置通知--> <aop:after-returning method="afterReturn" pointcut-ref="pt"/> <!--異常通知--> <aop:after-throwing method="agfterException" pointcut-ref="pt"/> <!--最終的通知--> <aop:after method="afterFinal" pointcut-ref="pt"/> <!--配置環繞通知--> <aop:around method="around" pointcut-ref="pt"/> </aop:aspect> </aop:config> <!-- 02 註解方式 開啟aop的註解掃描 --> <!-- 切面類上添加註解 @Aspect 方法上 @PointCut( ) @Before @After @AfterReturning @AfterThrowing @Around--> <aop:aspectj-autoproxy/> <!-- 03 純註解開發--> <!-- 建立一個配置類加上@Configuration註解,代替bean.xml配置檔案。 @Configuration 該註解的作用代表了該類是一個配置類,用於取代bean.xml檔案的。 @EnableAspectJAutoProxy 掃描aop相關的註解的。(@Aspect ,@PointCut(),@Before,@After,@AfterReturning,@AfterThrowing,@Around) @ComponentScan("com.itheima") 掃描 ioc 相關的註解的 @Respository @Service @Controller @component這些註解的 @PropertySource("classpath:db2.propertes") 用於指定載入配置檔案的。 @Bean("dataSource") 方法上一旦新增 @Bean("dataSource")註解,那麼該方法會自動執行, 並且把該方法的返回值儲存到容器中。 --> <!--======================-============= ( 三) Spring Aop宣告式事務管理 ==============================================--> <!-- 01 xml 配置--> <!--由於dao維護了一個jdbctemplate成員變數,所以需要注入--> <bean id="jdbctemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> <bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"> <property name="jdbcTemplate" ref="jdbctemplate"/> </bean> <!-- 建立一個連線池--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--1. 建立目標物件+切面物件--> <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"> <property name="accountDao" ref="accountDao"/> </bean> <!--建立切面物件,spring已經為我們提供好了事務管理器,我們目標是要把事務管理器的程式碼新增到service方法上面--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!--2. 事務管理器配置通知的規則:為哪些方法配置事務--> <tx:advice id="adive" transaction-manager="transactionManager"> <tx:attributes> <!-- <!–spring在匹配一個方法事務要事務的時候是從上往下匹配的,只有匹配到其中一個,那麼就不會再往下去執行了。–> <tx:method name="find*" propagation="SUPPORTS"/> <tx:method name="get*" propagation="SUPPORTS"/> <tx:method name="query*" propagation="SUPPORTS"/> <tx:method name="select*" propagation="SUPPORTS"/> <!–所有的方法必須要有事務–> <tx:method name="*" propagation="REQUIRED"/>--> <!--事務的規則:哪些方法是需要事務管理的。--> <tx:method name="save" isolation="DEFAULT" propagation="REQUIRED"/> <!-- propagation:事務傳播行為,REQUIRED代表了一定要有事務, SUPPORTS代表事務可有可無--> <tx:method name="findAll" isolation="DEFAULT" propagation="SUPPORTS"/> </tx:attributes> </tx:advice> <!--3.切面--> <aop:config> <!--切入點表示式--> <aop:pointcut id="pt" expression="execution(* com.itheima.service.impl.AccountServiceImpl.*(..))"/> <aop:advisor advice-ref="adive" pointcut-ref="pt"/> </aop:config> <!-- 02 註解方式 開啟事務管理器掃描 @transactional 等同於上面的2 和3 --> <!-- 在service層實現類 需要開啟事務的類 方法 介面上使用 @Transactional註解 @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED,noRollbackFor = NullPointerException.class ) // 事務註解 transactionManager: 引用的事務管理器的id 預設@Transactional根據型別進行注入 如果有多個型別才會按照名稱進行注入 value: 和transactionManager的值一樣的 isolation: 設定事務的隔離級別 propagation: 設定事務的傳播行為 rollbackFor: (理論)針對某些異常進行事務回滾 (實際測試,所有異常都可以進行事務控制 ) noRollbackFor: 針對某些異常不進行事務回滾 (實際測試是OK的) --> <!--掃描事務管理器 專門用於掃描@transactional註解--> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- 03 純註解開發--> <!-- 建立一個配置類加上@Configuration註解,代替bean.xml配置檔案。 @ComponentScan("com.itheima") 掃描 ioc 相關的註解的 @Respository @Service @Controller @component這些註解的 @Configuration 該註解的作用代表了該類是一個配置類,用於取代bean.xml檔案的。 @EnableTransactionManagement //開啟事務管理的掃描 掃描@Transactional註解 @Import(配置類名.class) // 匯入其他的配置類的。 --> <!-- ======================================== (四) Spring整合MyBatis配置 =====================================--> <!-- Spring整合MyBatis配置(1)建立SqlSessionFactory物件, 因為每一個dao的代理物件都需要從SqlSession中獲取,每一個SqlSession又必須從SqlSessionFactory中產生。 --> <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--A.注入資料來源--> <property name="dataSource" ref="dataSource"/> <!-- <property name="typeAliasesPackage" value="掃描包,配置別名"/> <property name="configLocation" value="配置SqlMapConfig.xml"/> <property name="mapperLocations" value="配置載入的對映檔案"/> --> <!--別名掃描--> <property name="typeAliasesPackage" value="com.itheima.entity"/> <!-- 分頁外掛 PageHelper 配置--> <!-- 注意其他配置 --> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <!--使用下面的方式配置引數,一行配置一個 --> <value> params=value1 </value> </property> </bean> </array> </property> </bean> <!-- Spring整合MyBatis配置(2)建立對映掃描配置 建立一個Dao物件 進行包掃描, 掃描mybatis 不再使用 JdbcTemplate 建立Dao實現類,直接使用MyBatis MapperScannerConfigurer Dao 使用註解 或者 Mapper對映檔案 建立 Dao實現類 1.會自動載入對映檔案; 2. 也會載入dao方法上註解; 3. 生成代理物件 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--A. 自動載入該路徑下的對映檔案--> <!--B. 自動載入該路徑下的dao介面檔案(讀取方法上的註解) dao同一個方法,要麼通過註解對映要麼通過配置Mapper對映--> <!--C. 自動對包下的所有介面生成代理物件--> <property name="basePackage" value="com.itheima.dao"/> </bean> <!-- ======================================== (四) spring整合shiro配置 =====================================--> <!--1. 配置shiro的過濾器工廠, bean的id與web.xml中的配置的shiro過濾器名稱一致.--> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <!--A. 注入Shiro安全管理器--> <property name="securityManager" ref="securityManager"/> <!--B.注入其他配置--> <!--B1.登陸地址. 認證失敗後自動跳轉到的頁面.--> <property name="loginUrl" value="/login.jsp"/> <!--B2.認證成功後預設跳轉的頁面. 如果程式由指定會覆蓋這裡--> <property name="successUrl" value="/home.jsp"/> <!--B3.許可權校驗失敗,跳轉的頁面--> <property name="unauthorizedUrl" value="/unauthorized.jsp"/> <!--C.過濾器鏈的配置--> <!--anon 匿名訪問過濾器,指定需要放行的資源--> <!--authc 認證過濾器--> <property name="filterChainDefinitions"> <value> /css/** = anon /img/** = anon /make/** = anon /plugins/** = anon /login.jsp = anon /login* = anon /index.jsp = anon <!-- shiro 授權 需要多個許可權 perms=["許可權1","許可權2"] --> <!--/system/user/list.do = perms["使用者管理"] --> /** = authc </value> </property> </bean> <!--2. 建立安全管理器--> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="myRealm"/> </bean> <!--3. 建立自定義的realm物件 自定義realm類:AuthRealm,繼承AuthorizingRealm --> <bean id="myRealm" class="cn.itcast.web.shiro.AuthRealm"> <!--注入憑證匹配器,自動對使用者輸入的密碼按照指定的演算法加密--> <property name="credentialsMatcher" ref="credentialsMatcher"/> </bean> <!--4. 建立憑證匹配器,指定加密演算法為md5 <bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher"> 自動對使用者輸入的密碼按照md5加密 <property name="hashAlgorithmName" value="md5"/> </bean> --> <!--4. 自定義憑證匹配器 自定義類 繼承 SimpleCredentialsMatcher類 指定加密演算法 --> <bean id="credentialsMatcher" class="cn.itcast.web.shiro.CustomCredentialsMatcher"/> <!--5. 配置shiro註解支援 1. 在applicationContext-shiro.xml中開啟shiro註解支援 2. 開啟Aop自動代理(已經完成) 3. 在controller中對應的方法上或類上使用@RequiresPermissions({“許可權1”,”許可權2“})註解 depends-on 表示建立當前bean之前,先建立depends-on依賴的物件.控制物件建立順序 --> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> <property name="securityManager" ref="securityManager"/> </bean> <!-- ======================================== (四) dubbodemo_provider =====================================--> <!--指定服務提供者名稱,通常為專案名稱。可以隨意定義,唯一即可。--> <dubbo:application name="dubbodemo_provider"/> <!--配置註冊中心地址--> <dubbo:registry address="zookeeper://192.168.217.128:2181"/> <!-- 配置請求協議 name 指定的是傳輸協議的名稱, 值列表範圍如:dubbo rmi hessian webservice http port:服務提供者的真實請求埠
–>
<dubbo:protocol name=“dubbo” port=“20881”/>
<!--配置dubbo服務提供者的包掃描 @Service(timeout = 100000) 設定超時時間為100秒。預設1秒 import com.alibaba.dubbo.config.annotation.Service --> <dubbo:annotation package="com.itheima.service"/> <!-- 如果不使用包掃描,也可以通過如下配置的方式來發布服務--> <!-- <bean id="helloService" class="com.itheima.service.impl.HelloServiceImpl" />--> <!-- <dubbo:service interface="com.itheima.service.HelloService" ref="helloService" />--> <!-- ======================================== (四) dubbo_consumer =====================================--> <!--開啟SpringMVC註解掃描,掃描@Controller註解--> <context:component-scan base-package="com.itheima.web"/> <!--SpringMVC註解驅動--> <mvc:annotation-driven/> <!-- 消費方應用名,用於計算依賴關係,不是匹配條件,不要與提供方一樣 --> <dubbo:application name="dubbodemo_consumer"> <dubbo:parameter key="qos.enable" value="false"/> </dubbo:application> <!--配置註冊中心地址--> <dubbo:registry address="zookeeper://192.168.217.128:2181"/> <!-- 預設值為true,表示啟動服務消費者時候會檢查服務提供者是否有啟動,如果服務提供者沒有啟動消費者啟動不了。--> <!-- <dubbo:consumer check="false"/>--> <!--dubbo開啟包掃描 註解掃描( @Reference(retries = 2)註解 retries = 2呼叫dubbo服務如果超時,自動重試2次 retries可選填 )--> <dubbo:annotation package="com.itheima.web"/> <!-- 如果不使用包掃描,通過如下配置來引用服務,生成遠端服務代理,可以和本地bean一樣使用helloService 此時 controller 中 @Autowired // 注意這裡用@Autowired,因為是配置的方式建立的介面代理且加入了容器 private HelloService helloService; --> <dubbo:reference id="helloService" interface="com.itheima.api.HelloService"/> <!-- --> <!-- ======================================== (四) Spring整合Quartz 定時任務排程 配置 =====================================--> <!--1. 將自定義的 定時任務類交給spring容器--> <bean id="myTask" class="cn.itcast.web.task.MyTask"/> <!--2. 建立任務工廠,配置定時執行的類和方法--> <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="myTask"/> <property name="targetMethod" value="execute"/> </bean> <!--3. 建立任務排程觸發器,注入任務執行表示式 --> <bean id="trigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="jobDetail"/> <!--秒 分 小時 日 月 周 年--> <property name="cronExpression" value="0/5 * * * * ?"/> </bean> <!--4. 配置定時工作管理員--> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <array> <!--注入任務排程觸發器--> <ref bean="trigger"/> </array> </property> </bean> <!-- ======================= (四) WebService JAX-RS 規範下 Spring整合CXF釋出服務 =====================================--> <!-- <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://cxf.apache.org/core" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd "> <jaxrs:server address="/userService" serviceClass="cn.itcast.service.UserServiceImpl"/> </beans> --> <!-- ======================= (四) Spring整合 RabbitMQ topic萬用字元模式 =====================================-->
<!--配置連線-->
<rabbit:connection-factory
id="connectionFactory"
host="192.168.23.1"
port="5672"
username="heima"
password="888"
virtual-host="/itcast"/>
<!--配置RabbitAdmin-->
<rabbit:admin connection-factory="connectionFactory" />
<!--配置佇列名-->
<rabbit:queue name="myQueue"/>
<!--配置topic型別exchange;佇列繫結到交換機-->
<rabbit:topic-exchange name="myExchange">
<rabbit:bindings>
<rabbit:binding queue="myQueue" pattern="msg.#" />
</rabbit:bindings>
</rabbit:topic-exchange>
<!--配置 RabbitTemplate物件加入容器
Map<String,String> map = new HashMap<>();
map.put("email"," [email protected]");
rabbitTemplate.convertAndSend("msg.email",map); // 傳送訊息 -->
<rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"
exchange="myExchange" message-converter="jsonMessageConverter"/>
<!-- 訊息物件json轉換類 -->
<bean id="jsonMessageConverter"
class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter" />
<!--元件掃描, 掃描自定義的訊息監聽器 需要新增pom依賴 spring-context -->
<context:component-scan base-package="cn.itcast.listener"/>
<!--配置連線-->
<rabbit:connection-factory
id="connectionFactory"
host="192.168.23.1"
port="5672"
username="heima"
password="888"
virtual-host="/itcast"/>
<!--配置RabbitAdmin-->
<rabbit:admin connection-factory="connectionFactory" />
<!--配置佇列名-->
<rabbit:queue name="myQueue"/>
<!--配置監聽 emailMessageListener為自定義的訊息監聽器 實現 MessageListener介面 -->
<rabbit:listener-container connection-factory="connectionFactory">
<rabbit:listener ref="emailMessageListener" queue-names="myQueue" />
</rabbit:listener-container>
<!-- ======================= (四) Spring整合 ActiveMQ =============================================-->
<!-- 一 生產者 -->
<!-- 1. 建立ActiveMQ連線工廠 -->
<amq:connectionFactory
id="amqConnectionFactory"
userName="admin" password="admin"
brokerURL="tcp://192.168.23.51:61616"/>
<!-- 2. 建立快取工廠 -->
<bean id="cachingConnectionFactory"
class="org.springframework.jms.connection.CachingConnectionFactory">
<!-- 注入 連線工廠-->
<property name="targetConnectionFactory" ref="amqConnectionFactory"></property>
<!-- session快取數目 -->
<property name="sessionCacheSize" value="5"></property>
</bean>
<!-- 3. 建立JmsTemplate(傳送訊息的模板工具類物件) -->
<!-- 3.1傳送Queue佇列訊息 -->
<bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 注入快取工廠 -->
<property name="connectionFactory" ref="cachingConnectionFactory"></property>
<!-- 預設值 -->
<property name="pubSubDomain" value="false"></property>
</bean>
<!-- 3.2傳送Topic主題訊息 -->
<bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 注入快取工廠 -->
<property name="connectionFactory" ref="cachingConnectionFactory"></property>
<!-- 設定訊息模型為主題訊息 -->
<property name="pubSubDomain" value="true"></property>
</bean>
<!-- 二 消費者 -->
<!-- 1. 建立ActiveMQ連線工廠 -->
<amq:connectionFactory
id="amqConnectionFactory"
userName="admin" password="admin"
brokerURL="tcp://192.168.12.132:61616"/>
<!-- 2. 建立快取工廠 -->
<bean id="cachingConnectionFactory"
class="org.springframework.jms.connection.CachingConnectionFactory">
<!-- 注入 連線工廠-->
<property name="targetConnectionFactory" ref="amqConnectionFactory"></property>
<!-- session快取數目 -->
<property name="sessionCacheSize" value="5"></property>
</bean>
<!--開啟註解掃描-->
<context:component-scan base-package="cn.itcast.spring_activemq_consumer"/>
<!--
配置訊息監聽器類,監聽佇列或主題訊息模型中的訊息。從而實現消費訊息。
jms:listener-container
destination-type 監聽的JMS訊息型別(queue、topic)
connection-factory Spring的快取連線工廠
jms:listener
destination 對應MQ中佇列名稱或主題名稱
rel 自定義的訊息監聽器類(實現MessageListener介面)
-->
<!-- 3.1 監聽指定名稱(email)的佇列中的訊息-->
<jms:listener-container destination-type="queue" connection-factory="cachingConnectionFactory">
<jms:listener destination="email" ref="emailMessageListener"/>
</jms:listener-container>
<!-- 3.2 監聽指定名稱(email)的主題中的訊息 -->
<jms:listener-container destination-type="topic" connection-factory="cachingConnectionFactory">
<jms:listener destination="sms" ref="smsMessageListener"/>
</jms:listener-container>