目录

- <context:component-scan base-package="com.superdemo"/>
-
- <aop:aspectj-autoproxy/>
-
-
-
-
-
-
-

- @Service("userService")
- public class UserServiceImpl implements UserService {
- public void save(int i){
- //0.将共性功能抽取出来
- //System.out.println("共性功能");
- System.out.println("user service running..."+i);
- //int i=1/0;
- }
-
- @Override
- public int update(int a,int b) {
- System.out.println("user service update running...");
- return a+b;
- }
-
- @Override
- public void delete() {
- System.out.println("user service delete running...");
- int i = 1/0;
- }
- }

- public class AOPPointcut {
- @Pointcut("execution(* *..*(..))")
- public void pt1(){}
- }


- //1.制作通知类,在类中定义一个方法用于完成共性功能
- @Component
- @Aspect
- public class AOPAdvice {
-
- //@Pointcut("execution(* *..*(..))")
- //public void pt(){}
-
- @Before("AOPPointcut.pt1()")
- public void before(JoinPoint jp){
- Object[] args = jp.getArgs();
- System.out.println("前置before..."+args[0]);
- }
-
- @After("AOPPointcut.pt1()")
- public void after(JoinPoint jp){
- Object[] args = jp.getArgs();
- System.out.println("后置after..."+args[0]);
- }
-
- @AfterReturning(value = "AOPPointcut.pt1()",returning = "ret")
- public void afterReturing(Object ret){
- System.out.println("返回后afterReturing..."+ret);
- }
-
- @AfterThrowing(value = "AOPPointcut.pt1()",throwing = "t")
- public void afterThrowing(Throwable t){
- System.out.println("抛出异常后afterThrowing..."+t.getMessage());
- }
-
- @Around("AOPPointcut.pt1()")
- public Object around(ProceedingJoinPoint pjp) throws Throwable {
- System.out.println("环绕前around before...");
- //对原始方法的调用
- /*Object ret = null;
- try {
- ret = pjp.proceed();
- } catch (Throwable e) {
- System.out.println("around...exception..."+e.getMessage());
- }*/
- Object ret = pjp.proceed();
- System.out.println("环绕后around after...");
- return ret;
- }
- }

- public class App {
- public static void main(String[] args) {
- ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
- UserService userService = (UserService) ctx.getBean("userService");
- //userService.save(666);
- int tj = userService.update(666,123);
- System.out.println("执行结果...."+tj);
- //userService.delete();
- }
- }

- @Configuration
- @ComponentScan("com.superdemo")
- @EnableAspectJAutoProxy
- public class SpringConfig {
- }

- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(classes = SpringConfig.class)
- public class UserServiceTest {
- @Autowired
- private UserService userService;
-
- @Test
- public void testupdate(){
- int ret = userService.update(666, 123);
- Assert.assertEquals(789,ret);
- }
- }

- public class Account {
- private Integer id;
- private String name;
- private Double money;
-
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Double getMoney() {
- return money;
- }
- public void setMoney(Double money) {
- this.money = money;
- }
- @Override
- public String toString() {
- return "Account{" +
- "id=" + id +
- ", name='" + name + '\'' +
- ", money=" + money +
- '}';
- }
- }

- public interface AccountDao {
- @Insert("INSERT INTO account(name, money) VALUES (#{name},#{money})")
- void save(Account account);
-
- @Delete("DELETE FROM account WHERE id = ${id}")
- void delete(Integer id);
-
- @Update("UPDATE account SET name = #{name},money = #{money} WHERE id = #{id}")
- void update(Account account);
-
- @Select("SELECT * FROM account")
- List
findAll(); -
- @Select("SELECT * FROM account WHERE id = #{id}")
- Account findByid(Integer id);
- }

- public interface AccountService {
- void save(Account account);
-
- void delete(Integer id);
-
- void update(Account account);
-
- List
findAll(); -
- Account findByid(Integer id);
- }

- @Service("accountService")
- public class AccountServiceImpl implements AccountService {
-
- @Autowired
- private AccountDao accountDao;
-
- /*public void setAccountDao(AccountDao accountDao) {
- this.accountDao = accountDao;
- }*/
-
- public void save(Account account){
- accountDao.save(account);
- }
-
- public void delete(Integer id){
- accountDao.delete(id);
- }
-
- public void update(Account account){
- accountDao.update(account);
- }
-
- public List
findAll(){ - return accountDao.findAll();
- }
-
- public Account findByid(Integer id){
- return accountDao.findByid(id);
- }
- }

- jdbc.driver=com.mysql.cj.jdbc.Driver
- jdbc.url=jdbc:mysql://localhost:3306/dp1?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&useSSL=false
- jdbc.username=root
- jdbc.password=123456

- @Component
- @Aspect
- public class RunTimeMonitorAdvice {
- //切入点,监控业务层接口
- @Pointcut("execution(* com.superdemo.service.*Service.find*(..))")
- public void pt(){}
-
- @Around("pt()")
- public Object runtimeAround(ProceedingJoinPoint pjp) throws Throwable {
- Signature signature = pjp.getSignature();
- String className = signature.getDeclaringTypeName();
- String methodName = signature.getName();
-
- Object ret = null;
- long sum = 0L;
-
- for(int i = 0; i < 10000; i++){
- long startTime = System.currentTimeMillis();
- ret= pjp.proceed();
- long endTime = System.currentTimeMillis();
- sum += endTime-startTime;
- }
-
- System.out.println(className+":"+methodName+":万次运行了:"+sum+"ms");
-
- return ret;
- }
- }

- public class JDBCConfig {
- @Value("${jdbc.driver}")
- private String driver;
- @Value("${jdbc.url}")
- private String url;
- @Value("${jdbc.username}")
- private String userName;
- @Value("${jdbc.password}")
- private String password;
-
- @Bean("dataSource")
- public DataSource getDataSource(){
- DruidDataSource ds = new DruidDataSource();
- ds.setDriverClassName(driver);
- ds.setUrl(url);
- ds.setUsername(userName);
- ds.setPassword(password);
- return ds;
- }
- }

- public class MybatisConfig {
- /*
- 对应XML格式下:
- spring整合mybatis后控制的创建连接用的对象
-
-
-
-
- 加载mybatis映射配置的扫描,将其作为spring的bean进行管理
-
-
- */
-
- @Bean
- public SqlSessionFactoryBean getSqlSessionFactoryBean(@Autowired DataSource dataSource){
- SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
- ssfb.setTypeAliasesPackage("com.superdemo.domain");
- ssfb.setDataSource(dataSource);
- return ssfb;
- }
-
- @Bean
- public MapperScannerConfigurer getMapperScannerConfigurer(){
- MapperScannerConfigurer msc = new MapperScannerConfigurer();
- msc.setBasePackage("com.superdemo.dao");
- return msc;
- }
- }

- @Configuration
- @ComponentScan("com.superdemo")
- @PropertySource("classpath:jdbc.properties")
- @Import({JDBCConfig.class, MybatisConfig.class})
- @EnableAspectJAutoProxy
- public class SpringConfig {
- }

- //设定spring专用的类加载器
- @RunWith(SpringJUnit4ClassRunner.class)
- //设定加载的spring上下文对应的配置
- @ContextConfiguration(classes = SpringConfig.class)
- public class UserServiceTest {
- @Autowired
- private AccountService accountService;
-
- @Test
- public void testFindById(){
- Account ac = accountService.findByid(2);
- System.out.println(ac);
- }
-
- @Test
- public void testFindAll(){
- List
list = accountService.findAll(); - Assert.assertEquals(3,list.size());
- }
- }