IDEA的使用之spring中ClassPathXmlApplicationContext读取路径

之前习惯使用eclipse的应用以后总是会把spring的配置文件放在src目录下,然后无论怎么写路径问题都无法读取spring.xml。

正确的配置文件放在resources目录下:
在这里插入图片描述

resource目录可以在以下界面中可以自定义:
在这里插入图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是一个简单的Spring使用AspectJ的案例: 1. 定义切面类 ```java @Aspect @Component public class LoggingAspect { @Before("execution(* com.example.service.*.*(..))") public void logBefore(JoinPoint joinPoint) { System.out.println("Before " + joinPoint.getSignature().getName() + " method is called"); } @After("execution(* com.example.service.*.*(..))") public void logAfter(JoinPoint joinPoint) { System.out.println("After " + joinPoint.getSignature().getName() + " method is called"); } } ``` 上面的代码定义了一个切面类`LoggingAspect`,其包含了两个通知方法`logBefore`和`logAfter`,分别在目标方法执行前和执行后打印日志。 2. 配置Spring AOP ```xml <aop:aspectj-autoproxy /> <bean id="loggingAspect" class="com.example.aspect.LoggingAspect" /> ``` 在Spring配置文件加入以上两行配置,启用AspectJ自动代理和定义切面类。 3. 使用切面 ```java @Service public class UserServiceImpl implements UserService { @Override public User getUserById(int id) { System.out.println("getUserById(" + id + ")"); return new User(id, "John"); } @Override public void saveUser(User user) { System.out.println("saveUser(" + user + ")"); } } ``` 在目标类`UserServiceImpl`的方法上不需要添加任何注解或配置,AspectJ会自动将切面织入到这些方法。 4. 测试 ```java public class Application { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = context.getBean(UserService.class); userService.getUserById(1); userService.saveUser(new User(2, "Alice")); } } ``` 运行上面的代码,可以看到控制台输出: ``` Before getUserById method is called getUserById(1) After getUserById method is called Before saveUser method is called saveUser(User [id=2, name=Alice]) After saveUser method is called ``` 这表明切面已经成功织入到了目标方法

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值