Spring學習筆記03(XML配置)
阿新 • • 發佈:2018-12-27
Struts2+Spring+MyBatis的配置(使用註解)
xml檔案一般有個約定俗稱的名字
applicationContext.xml
下面開始寫裡面的具體配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" >
<!--
aop 配置aop相關的
tx 事務相關的
xsi 預設必須新增的
context 載入掃描檔案
-->
<!--掃描元件 Action Service-->
<context:component-scan base-packge="比如Action在com.action,這裡只要寫上com就好"/>
<!--讀取jdbc的小配置檔案-->
<context:property-placeholder location="classpath:jdbc.properties" system-properties-mode ="NEVER(這個是防止讀取到自己計算機的名字)"/>
<!--配置資料庫和連線池-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${寫上小配檔案對應的}"/>
<property name="url" value="${}"/>
<property name="username" value="${}"/>
<property name="password" value="${}"/>
</bean>
<!--MyBatis的SqlSession工廠-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--Mapper檔案的路徑 交給spring讀取-->
<property name="mapperLocations">
<!--底層是陣列,這裡用list讀取-->
<list>
<value>classpath:com/dao/*Mapper.xml</value>
</list>
</property>
</bean>
<!--掃描dao介面生成實現類-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackge" value="com.dao"/>
</bean>
<!--spring宣告事務-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--查詢方法用只讀-->
<tx:method name="get*" read-only="true"/>
<tx:method name="login" read-only="true"/>
<!--其他的方法用預設的屬性就好-->
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!--配置切入點-->
<aop:config>
<aop:pointcut id="pt" expression="execution(* com.service.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"></aop:advisor>
</aop:config>
下面就是Service 和Action裡面怎麼配置了
首先先看幾個註解
@component
放在自定義類的前面,將其交給spring管理。
通過component衍生的幾個註解
-------------------------------------------------------------
@Controller
加到action類的上面,代表這是一個控制器
@Service
加到Service的實現類上面,代表這是一個業務邏輯層的類。
@Repository
加到Dao的實現類上面,代表這是操作資料庫的。(因為MyBatis沒有dao的實現類,所以不用)
------------------------------------------------------------
@Autowired 自動裝配。會自動給屬性賦值.
@Scope("") 裡面有屬性 singleton 單例的 和 prototype非單例
@Qualifier("") 可以給屬性寫一個其他的名字。例如UserService 預設值是userService,用了這個註解後可以起其他的名字,比如說 us 但是 不光這個註解要加上這個名字,修飾對應實現類的@Service(“as”) ,這個可以解決同一個介面多個實現類的問題。
所以說
//在Action中
@Controller
@Scope("prototype")
public class UserAction{
@Autowired
private UserService userService;
}
//在Service中
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserDao userDao;
}
還有一個問題 Struts2 怎麼和spring關聯起來呢
在web.xml中加入監聽器
<!--監聽器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--讀取配置檔案-->
<context-param>
<param-name>contextConfigLocation</param-naem>
<param-name>classpath:applicationContext.xml</param-name>
</context-param>
配置到這裡框架的整合就OK啦
Action一定要是非單例的!!!