Spring 普通类与工具类调用service层

在Spring MVC中,Controller中使用service只需使用注解@Resource/@Autowired就行,但是一般类(即不使用@Controller注解的类)要用到service时,Spring中的Service不是你想new就能new的,因为通过new实例化的对象脱离了Spring容器的管理,获取不到注解的属性值,所以会是null,就算调用service的类中有@Component注解加入了Spring容器管理,也还是null.

实例:

[java]  view plain  copy
  1. @Component  
  2. public class Test {  
  3.     @Resource(name = "testServiceImpl")  
  4.     private SessionService testService;  
  5.   
  6.     public Test() {  
  7.     }  
  8.   
  9.     public void run() {  
  10.         System.out.println("****");  
  11.         System.out.println(testService);  
  12.     }  
  13. }  

在Controller里 new 这个对象

[java]  view plain  copy
  1. Test test = new Test();  
  2. test.run();  
通过Debug可以得到 test为null


可用如下方法:

一、普通类与工具类共同的解决方法:

这里的普通类指的:是不在spring容器管理下的类,是通过new 来实例化的类。

1、SpringContextUtil

复制代码
package com.test.framework.utils;
 
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
 
public class SpringContextUtil implements ApplicationContextAware {
 
    private static ApplicationContext applicationContext; // Spring应用上下文环境
 
    // 下面的这个方法上加了@Override注解,原因是继承ApplicationContextAware接口是必须实现的方法
    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        SpringContextUtil.applicationContext = applicationContext;
    }
 
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
 
    public static Object getBean(String name) throws BeansException {
        return applicationContext.getBean(name);
    }
 
    public static Object getBean(String name, Class requiredType)
            throws BeansException {
        return applicationContext.getBean(name, requiredType);
    }
 
    public static boolean containsBean(String name) {
        return applicationContext.containsBean(name);
    }
 
    public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
        return applicationContext.isSingleton(name);
    }
 
    public static Class getType(String name)    throws NoSuchBeanDefinitionException {
        return applicationContext.getType(name);
    }
 
    public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
        return applicationContext.getAliases(name);
    }
}
复制代码

2、Spring的配置文件application.xml中进行如下配置

<bean id="SpringContextUtil" class="com.test.framework.utils.SpringContextUtil" scope="singleton"></bean>

3、使用

DictService dictService = (DictService) SpringContextUtil.getBean("dictService");
List<dict> dict = (List<dict>) dictService.findByHQL(hql);

 

扫描二维码关注公众号,回复: 886143 查看本文章

二、工具类单独的解决方法

        如果我们要在我们自己封装的Utils工具类中或者非controller普通类中使用@Autowired注解注入Service或者Mapper接口,直接注入是不可能的,因为Utils使用了静态的方法,我们是无法直接使用非静态接口的,当我们遇到这样的问题,我们就要想办法解决了。

    Spring Bean初始化和销毁Bean的方式虽然后三种,但是第三种是不符合这个,因为Utils是不用继承或者声明接口的,

1、注解方式:

[java]  view plain  copy
  1. @Component   
  2. public class TestUtils {  
  3.     @Autowired  
  4.     private ItemService itemService;  
  5.       
  6.     @Autowired  
  7.     private ItemMapper itemMapper;  
  8.       
  9.     public static TestUtils testUtils;  
  10.       
  11.     @PostConstruct  
  12.     public void init() {      
  13.         testUtils = this;  
  14.     }   
  15.       
  16.     //utils工具类中使用service和mapper接口的方法例子,用"testUtils.xxx.方法" 就可以了        
  17.     public static void test(Item record){  
  18.         testUtils.itemMapper.insert(record);  
  19.         testUtils.itemService.queryAll();  
  20.     }  
  21. }  

我们在init方法中使用以下注解就可以了,时间上这个init()的方法是可以自己随便定义的,注意:inti()方法里面只需要一行代码,跟我这样的就绝对ok了,不用看网上其他人瞎掰!


2、xml配置方式:

我们可以把init()方法上的@PostConstruct注解去掉,在spring-comtext.xml中配置以下bean就好了,里面什么内容都不用写,是不是很简单?

[html]  view plain  copy
  1. <bean id="testUtils" class="这里写utils类的包全路径名" init-method="init"></bean>  


附录: 另外一种Spring bean的初始化和销毁方式

实现InitializingBean, DisposableBean这两个接口,并复写afterPropertiesSet()destroy()方法

示例代码如下:

[java]  view plain  copy
  1. public class InitializingDisposableInit implements InitializingBean,  
  2.         DisposableBean {  
  3.   
  4.     @Override  
  5.     public void destroy() throws Exception {  
  6.         System.out.println("执行InitializingDisposableInit: destroy");  
  7.     }  
  8.   
  9.     @Override  
  10.     public void afterPropertiesSet() throws Exception {  
  11.         System.out.println("执行InitializingDisposableInit: afterPropertiesSet");  
  12.     }  
  13.       
  14.     public static void main(String[] args) {  
  15.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  
  16.                 "classpath:beans-impl.xml");  
  17.         context.close();  
  18.     }  

猜你喜欢

转载自blog.csdn.net/chrise_/article/details/80338565