非WEB工程怎么在main方法中加载spring容器
来源:互联网 发布:c 语言整型常量 编辑:程序博客网 时间:2024/06/04 23:27
很多非WEB工程想引入spring的支持,就需要通过一个main方法启动加载spring容器
1.配置文件形式
//加载spring容器,并得到类的实例,下面配置文件是放在src/spring下面
public static void main(String[] args) {
//所有配置文件
args = new String[] {
"classpath:spring/spring-servlet.xml",
"classpath:spring/ApplicationContext.xml",
"classpath:spring/mybatis-config.xml",
};
ApplicationContext actx = new ClassPathXmlApplicationContext(args);
//得到类的实例
UserService userService = (UserService) actx.getBean("userService");
//调用类的方法
userService.deleteUser(2);
}
2.注解形式
public static void main( String[] args ) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.getEnvironment().setActiveProfiles("prod"); // 先将激活的Profile设置为prod ctx.register(Config.class, DevConfig.class, ProdConfig.class); // 后置注册Bean配置类,不然为报Bean未定义的错误 ctx.refresh(); // 刷新容器 Student student = ctx.getBean(Student.class); System.out.println(student.getName()); ctx.close();}
阅读全文
0 0
- 非WEB工程怎么在main方法中加载spring容器
- 非WEB工程怎么在main方法中加载spring容器
- Spring在非web应用中关闭IoC容器 (registerShutdownHook)
- Spring在非web应用中关闭IoC容器 (registerShutdownHook)
- web 工程中 非spring 管理的对象 调用 spring 容器中对象
- 初探spring applicationContext在web容器中加载过程
- 初探spring applicationContext在web容器中加载过程
- 初探spring applicationContext在web容器中加载过程
- Spring在非web应用中优雅地关闭Spring IoC容器
- Spring在非web应用中优雅地关闭Spring IoC容器
- 在非Spring容器中使用注入
- 怎么在类中调用spring web容器中定义得bean
- 初探spring applicationContext在web容器中加载过程 首先从WEB.XML入手
- dubbo框架中Main方法运行spring容器
- dubbo框架中Main方法运行spring容器
- 从源码看spring applicationContext在web容器中加载过程
- 从源码看spring applicationContext在web容器中加载过程
- 初探spring applicationContext在web容器中加载过程 首先从WEB.XML入手 ==>web.xml(1)
- DefaultAnnotationHandlerMapping 和 AnnotationMethodHandlerAdapter过时
- U3D -- 图集分割和使用
- ssm 线程
- JVM优化
- 关于cookie的设置,查看和删除
- 非WEB工程怎么在main方法中加载spring容器
- PHP中获取页面的URL
- matlab常用滤波器
- 连接池 Druid 详细介绍
- 结构体与NSData的转换
- Windows上如何配置solr
- java获取客服端信息(系统,浏览器等)
- Java关于Integer面试题
- html超文本标记语言