在项目中有时需要根据需要在自己new一个对象,或者在某些util方法或属性中获取Spring Bean对象,从而完成某些工作,但是由于自己new的对象和util方法并不是受Spring所管理的,如果直接在所依赖的属性上使用@Autowired就会报无法注入的错误,或者是没报错,但是使用的时候会报空指针异常.总而言之由于其是不受IoC容器所管理的,因而无法注入.         Spring提供了两个接口:BeanFactoryAware和ApplicationContextAwar…
1.在spring中配置如下<context:spring-configured/>     <context:load-time-weaver aspectj-weaving="autodetect"/> 2.spring bean如下 用@configurable进行注解,这样我们可以直接new RealTimeStatisticTask,那么RealTimeStaticDao也能被正常注入了. 3.将spring-instrument-tomcat-4.1…
看到这个题目相信很多小伙伴都是懵懵的,平时我们的做法大都是下面的操作 @Component public class People{ @Autowired private Man man; } 这里如果Man是单例的,这种写法是没有问题的,但如果Man是原型的,这样是否会存在问题. 错误实例演示 这里有一个原型(生命周期为prototype)的类 package com.example.myDemo.component; import org.springframework.context.an…
问题说明 今天在web应用中用到了Java多线程的技术来并发处理一些业务,但在执行时一直会报NullPointerException的错误,问题定位了一下发现是线程中的Spring bean没有被注入,bean对象的值为null. 原因分析 web容器在启动应用时,并没有提前将线程中的bean注入(在线程启动前,web容易也是无法感知的) 解决方案 方法有多种,网上也看到了不少. 1. 使用static声明变量 可参见 引用 http://blog.csdn.net/bjamosgavin/ar…
问题说明 今天在web应用中用到了Java多线程的技术来并发处理一些业务,但在执行时一直会报NullPointerException的错误,问题定位了一下发现是线程中的Spring bean没有被注入,bean对象的值为null. 原因分析 web容器在启动应用时,并没有提前将线程中的bean注入(在线程启动前,web容易也是无法感知的) 解决方案 方法有多种,网上也看到了不少. 1. 使用static声明变量 可参见 引用 http://blog.csdn.net/bjamosgavin/ar…
思路: 1.实现Spring的ApplicationContextAware接口,重写setApplicationContext方法,将得到的ApplicationContext对象保存到一个静态变量中,有了这个上下文对象,就可以在项目的任意地方用它来得到任意Bean; 2.调用applicationContext.getBeanDefinitionNames()方法就可以拿到Spring容器中所有的Bean名称;这里为了测试方便就直接在InitializingBean 接口的afterProp…
spring framework中的spring web MVC模块 1.概述 spring web mvc是spring框架中的一个模块 spring web mvc实现了web的MVC架构模式,可以被用于开发web网站 spring web mvc 实现web网站的原理,如下图: 2.使用spring web mvc开发web应用的步骤 step1:在自己的工程中引入spring web mvc模块 step2:配置spring web mvc模块 中的DispatcherServlet,告…
Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下文中配置的Bean,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作. 权限控制是非常常见的功能,在各种后台管理里权限控制更是重中之重.在Spring Boot中使用 Spring Security 构建权限系统是非常轻松和简单的.下面我们就来快速入门 Spring Security .在开始前我们需要一对…
通过spring.net中的spring.caching CacheResult实现memcached缓存1.SpringMemcachedCache.cs2.APP.config3.Program.cs4.Common 待解决问题:CacheResult,CacheResultItems有什么区别???? SpringMemcachedCache.cs memcached的实现, 继承了Spring.Caching.AbstractCache, memcached的实现用了Enyim.Cach…
Spring Boot动态注入删除bean 概述 因为如果采用配置文件或者注解,我们要加入对象的话,还要重启服务,如果我们想要避免这一情况就得采用动态处理bean,包括:动态注入,动态删除. 动态注入bean思路 在具体进行代码实现的时候,我们要知道,Spring管理bean的对象是BeanFactory,具体的是DefaultListableBeanFactory,在这个类当中有一个注入bean的方法:registerBeanDefinition,在调用registerBeanDefiniti…
问题:在filter和interceptor中经常需要调用Spring的bean,filter也是配置在web.xml中的,请问一下这样调用的话,filter中调用Spring的某个bean,这个bean一定存在吗?现在总是担心filter调用bean的时候,bean还没被实例化? 答案:因为spring bean.filter.interceptor加载顺序与它们在 web.xml 文件中的先后顺序无关.即不会因为 filter 写在 listener 的前面而会先加载 filter.最终得出…
原文链接:http://outofmemory.cn/java/spring/spring-DI-inner-class 在spring中注入内部类,有可能会遇到如下异常信息: 2014-5-14 21:52:45 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlA…
我们可以在Spring的bean中轻松的注入HttpServletRequest,使用@Autowired HttpServletRequest request;就可以了. 但是,为什么我们可以直接这样用呢? 原因肯定是Spring在容器初始化的时候就将HttpServletRequest注册到了容器中. 那么我们就查原码,发现在WebApplicationContextUtils.registerWebApplicationScopes(ConfigurableListableBeanFact…
在使用spring容器的web应用中,业务对象间的依赖关系都可以用context.xml文件来配置,并且由spring容器来负责依赖对象 的创建.如果要在servlet中使用spring容器管理业务对象,通常需要使用WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext())来获得WebApplicationContext,然后调用WebApplicationContext.getBean("be…
在有些情况下需要使用main使用Spring bean,但是main方法启动并没有托管给Spring管理,会导致bean失败,报空指针异常. 可以使用 ClassPathXmlApplicationContext 加载配置文件,获获取bean: public static void main(String[] args) { @SuppressWarnings("resource") ClassPathXmlApplicationContext context = new ClassP…
check to see if spring security is applied that the appropriate resources are permitted: @Configuration public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter { @Override public void configure(WebSecurity web) throws Exception { h…
依赖注入 Spring主要提供以下两种方法用于依赖注入 基于属性Setter方法注入 基于构造方法注入 Setter方法注入 例子: public class Communication { private Messaging messaging; /* * DI via Setter */ public void setMessaging(Messaging messaging){ this.messaging = messaging; } public void communicate(){…
web应用中java多线程并发处理业务时,容易抛出NullPointerException. 原因: 线程中的Spring Bean没有被注入.web容器在启动时,没有提前将线程中的bean注入,在线程启动之前,web容器是无法感知的. 解决方案: 方法一.在声明成员变量的时候,将其定义为static的.(据说不可行) 方法二.将线程设置为主程序的内部类. 在外部类中注入bean,这样在内部类线程中就可以“共享”这个对象. 方法三.定义一个工具类,使用静态工厂方法通过getBean获得bean对…
swagger提供开发者文档 ======================================================== 作用:想使用swagger的同学,一定是想用它来做前后台分离,后台开发为前台提供API,以供前台的同学调用和调试. 那么swagger的作用就是上面这句话. 具体swagger包含了哪些,swagger官网展示的很齐全 本篇只表达swagger2+spring boot怎么用,也是给想用swagger但是无从下手的同学们带带路!!!! =========…
1.使用@Bean 注解,用于注入第三方 jar 包到SpringIOC容器中. 2.使用 @Import({Order.class, Member.class, MyImportBeanDefinitionRegistrar.class}) 注解,可以注入多个类,多个类之间使用 , 分割,主要用于注入第三方的 jar 包到SpirngIOC容器中. 3.@Service.@Repository 注入对象到SpringIOC容器中,能更好的的区分使用场景(自定义的类注入 Spring 容器中使用…
首先,确认你是对spring boot的自动配置相关机制是有了解的,如果不了解请看我spring boot相关的源码分析. 通常的使用方法是继承自org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter,然后重写 org.springframework.web.servlet.config.annotation.WebMvcConfigurer…
摘要 本文主要简单介绍下如何在Spring Boot 项目中使用Spring data mongdb.没有深入探究,仅供入门参考. 文末有代码链接 准备 安装mongodb 需要连接mongodb,所以需要提前安装mongodb.在本地安装 安装文档见官网 install mongodb 安装完mongodb后配置环境变量.创建目录"C:\data\db"作为mongo 数据存储的默认文件 注意本篇文章代码连接的mongo3.2. 在window7 64 bit 上安装mongo,可以…
我们在编写Web应用时,经常需要对页面做一些安全控制,比如:对于没有访问权限的用户需要转到登录表单页面.要实现访问控制的方法多种多样,可以通过Aop.拦截器实现,也可以通过框架实现(如:Apache Shiro.Spring Security). 本文将具体介绍在Spring Boot中如何使用Spring Security进行安全控制. 准备工作 首先,构建一个简单的Web工程,以用于后续添加安全控制,也可以用之前Chapter3-1-2做为基础工程.若对如何使用Spring Boot构建We…
参考:https://blog.csdn.net/qq_35056292/article/details/78430777 问题出现: 在一个非controller/service类中,我需要注入Config类 @Autowired MyConfig myConfig; public int getUrl(){ String url=myConfig.getUrl; } 这时候,myConfig是null 解决方法: @Component // 关键1,将该工具类注册为组件 public cla…
以下内容引用自http://wiki.jikexueyuan.com/project/spring/injecting-collection.html: 如果你想传递多个值,如Java Collection类型List.Set.Map和Properties,Spring 提供了四种类型的集合的配置元素,如下所示: 元素 描述 <list> 它有助于连线,如注入一列值,允许重复. <set> 它有助于连线一组值,但不能重复. <map> 它可以用来注入键值对的集合,其中键…
spring 提供了Awear 接口去 让bean 能感受到外界的环境.Awear 接口有很多实现,常用的有 ApplicationContextAware (可以通过实现这个接口去获取ApplicationContext), BeanNameAware(可以获取Bean自身的一些属性), BeanFactoryAware(可以获取BeanFactory) @Component public class ApplicationContextManager implements Applicati…
建立一个实体 package com.java.test4; import java.util.*; /** * @author nidegui * @create 2019-06-22 14:45 */ public class People { private Integer id; private String name; private String age; private Dog dog; private List<String> a=new ArrayList<>()…
俩个实体 package com.java.test4; /** * @author nidegui * @create 2019-06-22 14:45 */ public class People { private Integer id; private String name; private String age; private Dog dog; public Integer getId() { return id; } public People() { } public Dog…
@Bean(autowire = Autowire.BY_NAME,value = "kaptchaProducer") public Producer kaptchaProducer() { Properties kaptchaProperties = new Properties(); kaptchaProperties.put("kaptcha.border", "no"); kaptchaProperties.put("kapt…
丢代码地址 https://gitee.com/a247292980/spring-security 再丢pom.xml <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.…