Spring中ClassPathXmlApplicationContext类的简单使用和spring的简单小案例

一、简单的用ApplicationContext做测试的话,获得Spring中定义的Bean实例(对象).可以用:

ApplicationContext ac = new ClassPathXmlApplicationContext(“applicationContext.xml”);
RegisterDAO registerDAO = (RegisterDAO)ac.getBean(“RegisterDAO”);

如果是两个以上:
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{“applicationContext.xml”,“dao.xml”});

或者用通配符:
ApplicationContext ac = new ClassPathXmlApplicationContext(“classpath:/*.xml”);

二、ClassPathXmlApplicationContext[只能读放在web-info/classes目录下的配置文件]和FileSystemXmlApplicationContext的区别

classpath:前缀是不需要的,默认就是指项目的classpath路径下面;
如果要使用绝对路径,需要加上file:前缀表示这是绝对路径;

对于FileSystemXmlApplicationContext:
默认表示的是两种:

1.没有盘符的是项目工作路径,即项目的根目录;
2.有盘符表示的是文件绝对路径.

如果要使用classpath路径,需要前缀classpath:

public class HelloClient {

protected static final Log log = LogFactory.getLog(HelloClient.class);

public static void main(String[] args) {
// Resource resource = new ClassPathResource(“appcontext.xml”);
// BeanFactory factory = new XmlBeanFactory(resource);

// 用classpath路径
// ApplicationContext factory = new ClassPathXmlApplicationContext("classpath:appcontext.xml");
// ApplicationContext factory = new ClassPathXmlApplicationContext("appcontext.xml");

// ClassPathXmlApplicationContext使用了file前缀是可以使用绝对路径的
// ApplicationContext factory = new ClassPathXmlApplicationContext("file:F:/workspace/example/src/appcontext.xml");

// 用文件系统的路径,默认指项目的根路径
// ApplicationContext factory = new FileSystemXmlApplicationContext("src/appcontext.xml");
// ApplicationContext factory = new FileSystemXmlApplicationContext("webRoot/WEB-INF/appcontext.xml");


// 使用了classpath:前缀,这样,FileSystemXmlApplicationContext也能够读取classpath下的相对路径
// ApplicationContext factory = new FileSystemXmlApplicationContext("classpath:appcontext.xml");
// ApplicationContext factory = new FileSystemXmlApplicationContext("file:F:/workspace/example/src/appcontext.xml");

// 不加file前缀
ApplicationContext factory = new FileSystemXmlApplicationContext("F:/workspace/example/src/appcontext.xml");
IHelloWorld hw = (IHelloWorld)factory.getBean("helloworldbean");
log.info(hw.getContent("luoshifei"));

}
}

下面是我对bean生命周期的测试的一个小案例:
我在这边说一下这个spring小案例的原理吧,可能对刚入门学spring的同学有帮助,毕竟我也是刚学不久的。
这个实际上是先创建好CustomerDAO.java接口文件,在该文件里面写好抽象方法,然后在CustomerDAOImpl.java实现接口类文件,就是把刚才的接口文件中的全部方法进行重写,可以再写一些其他方法,applicationContext.xml的spring配置文件里面实际上就是spring工厂模式下比较重要的管理,写上bean标签后,id是自己随便定的,为的就是在测试类里面写的getbean(“ id名”)括号里面的实际上就是我们自己定的名字,然后这个案例是为了测试bean的生命周期用的,所以说在那个xml文件中写上init-method和destroy-method属性实际上也就是bean生命的开始和消失,属性值就是我们要在实现类里面写的方法名,SpringDemo2的spring测试文件中第一行ApplicationContext applicationContext = new ClassPathXmlApplicationContext(“applicationContext.xml”);实际上就是获取到xml文件,这个时候就已经创建好了对象。
1.CustomerDAO.java接口文件:

在这里插入图片描述
2.CustomerDAOImpl.java实现接口类文件:
在这里插入图片描述
3.applicationContext.xml的spring配置文件:
在这里插入图片描述
4.SpringDemo2的spring测试文件:
在这里插入图片描述

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值