Spring学习笔记03(XML配置)

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一定要是非单例的!!!

猜你喜欢

转载自blog.csdn.net/cheng6202/article/details/84591373