和xml配置相比,用@註解配置**更加簡單,但是需要先理解xml配置
druid.properties
jdbc.driverclassname=com.mysql.jdbc.driver
jdbc.url=jdbc:mysql:
//localhost:
3306
/db2
jdbc.username=root
jdbc.password=root
第三方配置類 springconfig.class@configuration
@componentscan
("com.itheima"
)@propertysource
("classpath:druid.properties"
)@enableaspectjautoproxy
//開啟自動**
public
class
springconfig")
private string driver;
@value
("$"
)private string url;
@value
("$"
)private string username;
@value
("$"
)private string password;
@bean
("datasource"
)//bean註解的作用是將方法的返回值儲存在spring容器中
public datasource createdatasource()
@bean
("jdbctemplate"
)//@qualifier會按照指定的名稱進行引數注入
public jdbctemplate getjdbctemplate
(@qualifier
("datasource"
) datasource datasource)
}
userdaoimpl@repository
("userdao"
)public
class
userdaoimpl
implements
userdao
@override
public
void
add(user user)
throws sqlexception
@override
public
void
update
(user user)
throws sqlexception
@override
public
void
delete
(integer id)
throws sqlexception
@override
public user findbyid
(integer id)
throws sqlexception
@override
public list
findall()
throws sqlexception
}
userserviceimpl@service
("userservice"
)public
class
userserviceimpl
implements
userservice
@override
public
void
add(user user)
throws sqlexception
@override
public
void
update
(user user)
throws sqlexception
@override
public
void
delete
(integer id)
throws sqlexception
@override
public user findbyid
(integer id)
throws sqlexception
@override
public list
findall()
throws sqlexception
}
增強類@component
@aspect
public
class
logger
/* @before("getaa()")
public void before()
@afterreturning("getaa()")
public void afterreturning()
@afterthrowing("getaa()")
public void afterthrowing()
@after("getaa()")
public void after()
*/// 增強方法型別 目標方法
@around
("execution(* com.itheima.service.impl.userserviceimpl.*(..))"
)public object around
(proceedingjoinpoint pjp)
catch
(throwable throwable)
finally
return proceed;
}}
測試類@runwith
(springjunit4classrunner.
class
)@contextconfiguration
(classes = springconfig.
class
)public
class
userservicetest
}
Spring Aop 註解和配置實現
1 註解方式實現 1 需要在配置檔案中配置開啟 2 其他資訊直接寫在 中,方便理解。package com.cms.modules.audit import org.aspectj.lang.proceedingjoinpoint import org.aspectj.lang.annotation...
Spring AOP配置之註解方式
在pom.xml檔案中匯入所需的依賴 在配置檔案中開啟aop註解支援 配置切面類 aspect 配置切入點 pointcut 配置通知方法,並為通知方法配置通知型別 before aspect pointcut before afterreturning afterthrowing after ar...
springAop的配置註解方式實現
前面一篇是xml實現的,現在用註解方式實現一下,測試類還是原來的,xml的配置中多餘的部分這裡就不在貼出來了。xml version 1.0 encoding utf 8 beans xmlns xmlns context xmlns xsi xmlns aop xsi schemalocation...