您的位置:首页 > 编程语言 > Java开发

Spring3MVC通过SpringContextUtil获取上下文(与不使用对比)

2017-11-22 11:12 405 查看
一,前言

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 <
4000
span class="hljs-title">setAge(Integer age) {
this.age = age;
}
}

//UserDao.java
public 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
@Service
public 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.java
public 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,20
username_1,password_1,21
username_2,password_2,22
username_3,password_3,23
username_4,password_4,24
username_5,password_5,25
username_6,password_6,26
username_7,password_7,27
username_8,password_8,28
username_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.java
public 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有很多种方式,不一定每一种都需要亲自去试试。对于项目本身来说,不同的项目框架可能适合的方法就不一样。对于项目开发来说,什么方法最省事,最节约时间,就应该考虑用哪种方法。

新手一枚,欢迎拍砖~ ~ ~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java spring mvc xml