Spring普通类获取并调用Spring service方法
2017-03-31 14:08
225 查看
创建获取Spring的生成类
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class SpringUtil implements ApplicationContextAware{
private static ApplicationContext appCtx;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
appCtx = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return appCtx;
}
public static Object getBean(String beanName) {
return appCtx.getBean(beanName);
}
}
调用方法
声明service
@Resource
private XXXXServiceImpl xxxService=(XXXXServiceImpl) SpringUtil.getBean(“xxxService”);
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class SpringUtil implements ApplicationContextAware{
private static ApplicationContext appCtx;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
appCtx = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return appCtx;
}
public static Object getBean(String beanName) {
return appCtx.getBean(beanName);
}
}
调用方法
声明service
@Resource
private XXXXServiceImpl xxxService=(XXXXServiceImpl) SpringUtil.getBean(“xxxService”);
相关文章推荐
- spring基于注解的普通类怎么调用@Services注解的service方法
- spring基于注解的普通类怎么调用@Services注解的service方法
- Spring 注解中,普通类获取@Service标记的方法 或者bean对象
- MyBatis+Spring在注解@Autowried后通过反射的方式调用方法获取注入的Service或DAO对象为空
- spring非controller类获取service方法
- 主动从spring获取对象的方法,并且在调用该方法的时候不用强制转换类型
- 普通java类获取 spring中的bean方法
- 普通Java类获取spring 容器的bean的5种方法
- 普通静态类方法获取Spring注入的Been实体
- spring在普通类中获取request的方法
- 普通静态类方法获取Spring注入的Been实体
- 在spring-mvc的非controller类里面调用service方法
- jsp调用spring的Service方法访问数据库
- Spring AOP进行日志记录,管理 (使用Spring的拦截器功能获取对action中每个方法的调用情况,在方法调用前
- Spring框架中普通的类如何调用注解Service中的方法
- 普通Java类获取Spring的Bean的方法
- java普通类获取Spring中bean方法
- Spring AOP进行日志记录,管理 (使用Spring的拦截器功能获取对action中每个方法的调用情况,在方法调用前和调用后记录相关日志。)
- main方法调用spring中dao service方法
- Spring AOP进行日志记录,管理 (使用Spring的拦截器功能获取对action中每个方法的调用情况)