美文网首页
Spring配置步骤

Spring配置步骤

作者: 溺于眼里星河 | 来源:发表于2019-05-16 02:18 被阅读0次

1、导入jar包
spring-context
spring-beans
spring-core
spring-jdbc
spring-web
mybatis-spring
druid
struts2-spring-plugin

2、service的实现类,在类上加注解 @Service
属性上加@Resource

如下所示:

@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleMapper roleMapper;

}

3.控制器类加@Controller注解 ,注入的service加@Resource注解
如下所示:

@Controller
public class UserAction extends ActionSupport {
    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;
    
}

4.配置applicationContext.xml文件
创建 applicationContext.xml,放在web-info目录下.
在applicationContext文件中配置数据源, 在mybatis.xml中把连接数据库的environments删除
在applicationContext.xml文件中配置会话工厂 ,配置扫描打了@Service和@Controller的包.
配置MapperScannerConfigurer生成mapper的bean

5.在web.xml中配置监听器
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

1.使用声明式事务导jar包
spring-aop
spring-tx

2.配置applicationContext文件
在文件中定义要切入的事务
示例如下:
<bean name="tm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:advice id="txAdvice" transaction-manager="tm">
<tx:attributes>

<tx:method name="add" propagation="REQUIRED"/>
<tx:method name="update
" propagation="REQUIRED"/>
<tx:method name="delete" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="pointcut" expression="execution(
com.szxs.service...(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
</aop:config>

相关文章

网友评论

      本文标题:Spring配置步骤

      本文链接:https://www.haomeiwen.com/subject/zagvaqtx.html