Spring3MVC通过SpringContextUtil获取上下文(与不使用对比)
来源:互联网 发布:h3c网管软件 编辑:程序博客网 时间:2024/06/05 01:54
一,前言
Spring的发展简介:
1.Spring1.x时代:在Spring1.x时代,都是通过xml文件配置bean,随着项目的不断扩大,需要将xml配置分放到不同的配置文件中,需要频繁的在java类和xml配置文件中切换。
2.*究竟是应该使用xml还是注解呢?a:应用的基本配置用xml,比如:数据源、资源文件等;b:业务开发用注解,比如:Service中注入bean等;
3.Spring3.x到Spring4.x:从Spring3.x开始提供了Java配置方式,使用Java配置方式可以更好的理解你配置的Bean,现在我们就处于这个时代,并且Spring4.x和Springboot都推荐使用java配置的方式。
4.Spring无xml配置实现Spring-IOC,代码如下(略pom.xml):
//User.java的基础类public class User { private String username; private String password; private Integer age; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; }}//UserDao.javapublic class UserDao { public List<User> queryUserList(){ List<User> result = new ArrayList<User>(); for(int i=0;i<10;i++){ User user = new User(); user.setUsername("username_"+i); user.setPassword("password_"+i); user.setAge(20+i); result.add(user); } return result; }}//UserService.java@Servicepublic class UserService { @Autowired private UserDao userDao; public List<User> queryUserList(){ return userDao.queryUserList(); }}//SpringConfig.java@Configuration@ComponentScan(basePackages = "cn.itcast.springboot")public class SpringConfig { @Bean public UserDao getUserDao(){ return new UserDao(); }}//Main.javapublic class Main { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); UserService userService = context.getBean(UserService.class); List<User> list = userService.queryUserList(); for (User u : list) { System.out.println(u.getUsername() + "," + u.getPassword() + "," + u.getAge()); } context.destroy(); }}//打印效果:username_0,password_0,20username_1,password_1,21username_2,password_2,22username_3,password_3,23username_4,password_4,24username_5,password_5,25username_6,password_6,26username_7,password_7,27username_8,password_8,28username_9,password_9,29
二,项目简介
项目为Spring3MVC的项目,由于在前期规划的时候没有更好的处理获取上下文这块的环节,导致在对项目中的Dao、Service进行操作的时候需要单独将他们的依赖关系存放到单独的bean.xml文件中,这样的就会造成工作量的增加和多需求处理的时候,可能由于一时疏忽,导致Dao、Service的关系没有配置而报错。
后来,通过在项目中配置SpringContextUtil,以前配置bean.xml的文件则就不需要再进行配置了,这样在项目协同和开发上都有很大的益处。
三,没有添加SpringContextUtil前的方式
1.对于新添加的Dao、Service
2.在applicationContext.xml配置中引用的bean.xml
四、添加SpringContextUtil后
1.SpringContextUtil代码:
//SpringContextUtil.javapublic class SpringContextUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; /** * 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量. */ public void setApplicationContext(ApplicationContext applicationContext) { SpringContextUtil.applicationContext = applicationContext; // NOSONAR } /** * 取得存储在静态变量中的ApplicationContext. */ public static ApplicationContext getApplicationContext() { checkApplicationContext(); return applicationContext; } /** * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型. */ public static <T> T getBean(String name) { checkApplicationContext(); return (T) applicationContext.getBean(name); } /** * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型. */ public static <T> T getBean(Class<T> clazz) { checkApplicationContext(); return (T) applicationContext.getBeansOfType(clazz); } /** * 清除applicationContext静态变量. */ public static void cleanApplicationContext() { applicationContext = null; } private static void checkApplicationContext() { if (applicationContext == null) { throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextUtil"); } }}
2.在applicationContext.xml配置中配置SpringContextUtil:
<!-- 获取spring管理bean的工具类 --><bean class="com.company.common.springhelper.SpringContextUtil" lazy-init="false" /><!-- Spring上下文, 只扫描service和dao进行注入, 排除controller --><context:component-scan base-package="com.company"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /></context:component-scan><!-- 加载全部配置文件 -->
3.使用方法(以UserService为例):
UserService userService = (UserService) SpringContextUtil.getBean(“userService”);
userService.xxx();
五、总结
Spring在代码中获取上下文bean有很多种方式,不一定每一种都需要亲自去试试。对于项目本身来说,不同的项目框架可能适合的方法就不一样。对于项目开发来说,什么方法最省事,最节约时间,就应该考虑用哪种方法。
新手一枚,欢迎拍砖~ ~ ~
- Spring3MVC通过SpringContextUtil获取上下文(与不使用对比)
- 通过SpringContextUtil获取spring环境上下文
- 一种获取spring环境上下文方法:SpringContextUtil
- 获取SpringMVC上下文bean实例-SpringContextUtil
- 获得spring上下文工具SpringContextUtil
- 909422229_Spring上下文操作工具类:SpringContextUtil
- SpringContextUtil
- spring3mvc与struts2比较
- spring3mvc与struts2比较
- spring3mvc与struts2比较
- spring3mvc与struts2比较
- spring3mvc与struts2比较
- Spring3MVC+Mybatis快速使用
- 主动获取spring容器工具类SpringContextUtil
- 通过上下文获取bean和通过bean工厂获取bean
- spring3mvc与struts2的区别
- MasterPage中使用SiteMapPath获取参数 “当前上下文不可用”解决办法
- Objective-C与Swift中通过字符串获取类的方法对比与实现
- python的类的方法
- ORACLE强制索引
- python类的属性
- python中的单继承
- 进制
- Spring3MVC通过SpringContextUtil获取上下文(与不使用对比)
- OpenCV实践之路——人脸识别之三识别自己的脸
- Tensorflow实战学习(四十三)【TF.Learn 机器学习Estimator,DataFrame,监督器Monitors】
- 13.3Groovy向具体的实例(对象)中注入方法,使不同的对象有不同的方法(神奇不)
- python中的多继承
- Android Canvas画图形(更新中)
- android studio 启动DDMS时报错Java was started but returned exit code=13
- 第11周 【项目5
- The import java.util.Base64 cannot be resolved问题