@在没有bean声明的情况下,不会调用PostConstructSpring
为什么在不将bean放入applicationContext.xml中的情况下调用post构造@在没有bean声明的情况下,不会调用PostConstructSpring,spring,spring-mvc,Spring,Spring Mvc,为什么在不将bean放入applicationContext.xml中的情况下调用post构造 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.sp
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr/spring-dwr-3.0.xsd">
<dwr:annotation-config />
<dwr:annotation-scan base-package="org.stalwartz" scanDataTransferObject="true" scanRemoteProxy="true" />
<dwr:url-mapping />
<!-- <bean id="proeprtyLoader" class="org.stalwartz.config.PropertyLoader"></bean> -->
<dwr:controller id="dwrController" debug="false">
<dwr:config-param name="activeReverseAjaxEnabled" value="true" />
</dwr:controller>
<context:annotation-config>
<context:component-scan base-package="org.stalwartz" annotation-config="true"></context:component-scan>
</context:annotation-config>
<mvc:annotation-driven />
...
...
...
</beans>
这是我的类,它包含@PostConstruct注释
package org.stalwartz.config;
import javax.annotation.PostConstruct;
import javax.inject.Singleton;
@Singleton
public class PropertyLoader {
@PostConstruct
public void init() {
System.out.println("PropertyLoader.init()");
}
}
下面是我的applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr/spring-dwr-3.0.xsd">
<dwr:annotation-config />
<dwr:annotation-scan base-package="org.stalwartz" scanDataTransferObject="true" scanRemoteProxy="true" />
<dwr:url-mapping />
<!-- <bean id="proeprtyLoader" class="org.stalwartz.config.PropertyLoader"></bean> -->
<dwr:controller id="dwrController" debug="false">
<dwr:config-param name="activeReverseAjaxEnabled" value="true" />
</dwr:controller>
<context:annotation-config>
<context:component-scan base-package="org.stalwartz" annotation-config="true"></context:component-scan>
</context:annotation-config>
<mvc:annotation-driven />
...
...
...
</beans>
...
...
...
看起来很简单,但如果不取消bean声明的注释,它就无法工作 Singleton是范围注释。它可以用来声明特定bean的“singletone”作用域,但不能实例化它。见文章 如果想将类实例化为单例,可以尝试Spring注释
@Service
public class PropertyLoader {
@PostConstruct
public void init() {
System.out.println("PropertyLoader.init()");
}
}
此外,还可以使用组件扫描替换注释配置标记。这里有一个关于注释配置和组件扫描标记差异的好例子。因为将bean放入applicationContext.xml中就是将bean添加到Spring容器中,Spring容器具有用于此注释的拦截器。当Spring注入bean时,它会在其他bean之间检查@PostConstruct注释 调用simple new PropertyLoader()时,JVM不会搜索@PostConstruct注释
@Service
public class PropertyLoader {
@PostConstruct
public void init() {
System.out.println("PropertyLoader.init()");
}
}
来自@PostConstruct注释的文档:
构造后注释用于需要执行的方法
完成依赖项注入后,执行任何初始化。这
方法必须在类投入服务之前调用。这
所有支持依赖关系的类都必须支持注释
注射。必须调用用PostConstruct注释的方法
如果类没有请求注入任何资源。在Spring环境中,初始化回调方法(由
@PostConstruct
注释的方法)仅在Spring托管bean上有意义。要管理PropertyLoader
类的实例,必须执行以下操作之一:
@component、@Repository、@Service、@Controller
中的一个来注释李>
注意:使用
会隐式启用
的功能。当使用
时,通常不需要包含
元素,因为您使用的是@Singleton
fromjavax.inject
包,它不是由spring容器作为bean获取的。将其更改为:
package org.stalwartz.config;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
public class PropertyLoader {
@PostConstruct
public void init() {
System.out.println("PropertyLoader.init()");
}
}
spring将自动检测
PropertyLoader
,并通过@Component
注释将其作为bean包含在spring容器中,该bean将具有单例作用域默认情况下,bean的作用域为单例spring
,而@PostConstruct
通常用于服务
bean,服务bean的作用域必须是原型
,这里因为您需要该特定类的多个对象,Spring
将为您提供单例
实例
同样,通过执行此操作,spring将多次尝试查找此服务bean,并最终抛出以下异常:
java.lang.NoClassDefFoundError:无法初始化类org.springframework.beans.factory.BeanCreationException
因此,请以注释方式尝试以下操作:
package org.stalwartz.config;
import javax.annotation.PostConstruct;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope("prototype") //you have to make it prototype explicitly
public class PropertyLoader {
@PostConstruct
public void init() {
System.out.println("PropertyLoader.init()");
}
}
现在一切都很好,对您来说也很好。刚刚尝试了@Service annotation,结果是否定的。只是以前没有看到:为什么在annotation config中使用组件扫描?注释配置标记可以替换为组件扫描。刚刚更新了我的回答谢谢你解决了这个问题。我的JSF和Spring注释不正确。是的,尝试了@Service&@Component,但似乎组件扫描没有按预期运行。问题在于嵌套声明。在applicationContext.xml中查看类中编辑的post.bullseye@Component的最后一个注释