【Spring框架】Spring框架Aop和IOC的注解方式

1.使用注解的方式实现IOC和AOP

(a)注解式的IOC

(1)注解文件applicationContext.xml

<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"

       xmlns:aop="http://www.springframework.org/schema/aop"

       xmlns:tx="http://www.springframework.org/schema/tx"

       xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-4.0.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

">

<!-- 包扫描:扫描哪些包下的注解可以被识别 -->

<context:component-scan base-package="com.svse"></context:component-scan>

<!-- 开启IOC注解配置 -->

<context:annotation-config></context:annotation-config>

(2)编写代码

Dao层

@Repository("usersDAO")

public class UsersDAOImpl implements UsersDAO {

  .......

}

Service层

@Service("usersService")

public class UsersServiceImpl implements UsersService {

         @Autowired

         private UsersDAO usersDAO;

 .......

}

注意:使用注解IOC的时候可以不用setter方法,进行注入的准备

Action层

@Controller("usersAction")

public class UsersAction {

         @Autowired

         private UsersService usersService;

    .....

 

}

(b)注解式的AOP

(1)配置文件applicationContext.xml

<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"

       xmlns:aop="http://www.springframework.org/schema/aop"

       xmlns:tx="http://www.springframework.org/schema/tx"

       xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-4.0.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

">

,<context:component-scan base-package="com.svse"></context:component-scan>

<!-- 开启AOP注解配置 -->

<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

<!-- 事务管理bean -->

<bean id="transactionManager" class=" org.springframework.orm.hibernate4.HibernateTransactionManager">

         <property name="sessionFactory" ref="sessionFactory">

         </property>

</bean>

<!-- 使用事务注解 -->

<tx:annotation-driven transaction-manager="transactionManager"/>

 

</beans>

(2)代码编写:

(a)切面类

//切面

@Aspect

public class SecurityHandlerAspect {

 

         //该方法只是一个标记,没有任何含义,只是在这方法上写切入表达式,该方法的调用就是这个切入表达式的id

         @Pointcut("execution(* com.svse.dao.impl.*.*(..))")

         private void aspect() {}

        

         @Before("aspect()")

         public void checkSecurity() {

                   System.out.println("安全检查......");

         }

        

         @AfterReturning("aspect()")

         public void logcat() {

                   System.out.println("日志记录...................");

         }

}

(b)需要事物控制的类

@Service("usersService")

public class UsersServiceImpl implements UsersService {

         @Autowired

         private UsersDAO usersDAO;

        

         @Override

         @Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)

         public void add() {

                   // TODO Auto-generated method stub

                   usersDAO.add();

         }

 

         @Override

         @Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)

         public void update() {

                   // TODO Auto-generated method stub

                   usersDAO.update();

         }

 

         @Override

         @Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)

         public void delete() {

                   // TODO Auto-generated method stub

                   usersDAO.delete();

         }

 

         @Override

         @Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)

         public void find() {

                   // TODO Auto-generated method stub

                   usersDAO.find();

         }

 

}

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值