简体   繁体   English

Spring Roo @PostConstruct没有被调用

[英]Spring Roo @PostConstruct not being called

I have the following config file: 我有以下配置文件:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd   http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd   http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!--
    This will automatically locate any and all property files you have
    within your classpath, provided they fall under the META-INF/spring
    directory. The located property files are parsed and their values can
    then be used within application context files in the form of
    ${propertyKey}.
-->
<context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>
<!--
    Turn on AspectJ @Configurable support. As a result, any time you
    instantiate an object, Spring will attempt to perform dependency
    injection on that object. This occurs for instantiation via the "new"
    keyword, as well as via reflection. This is possible because AspectJ
    is used to "weave" Roo-based applications at compile time. In effect
    this feature allows dependency injection of any object at all in your
    system, which is a very useful feature (without @Configurable you'd
    only be able to dependency inject objects acquired from Spring or
    subsequently presented to a specific Spring dependency injection
    method). Roo applications use this useful feature in a number of
    areas, such as @PersistenceContext injection into entities.
-->
<context:spring-configured/>
<!--
    This declaration will cause Spring to locate every @Component,
    @Repository and @Service in your application. In practical terms this
    allows you to write a POJO and then simply annotate the new POJO as an
    @Service and Spring will automatically detect, instantiate and
    dependency inject your service at startup time. Importantly, you can
    then also have your new service injected into any other class that
    requires it simply by declaring a field for your service inside the
    relying class and Spring will inject it. Note that two exclude filters
    are declared. The first ensures that Spring doesn't spend time
    introspecting Roo-specific ITD aspects. The second ensures Roo doesn't
    instantiate your @Controller classes, as these should be instantiated
    by a web tier application context. Refer to web.xml for more details
    about the web tier application context setup services.

    Furthermore, this turns on @Autowired, @PostConstruct etc support. These 
    annotations allow you to use common Spring and Java Enterprise Edition 
    annotations in your classes without needing to do any special configuration. 
    The most commonly used annotation is @Autowired, which instructs Spring to
    dependency inject an object into your class.
-->
<context:component-scan base-package="com.mycompany.emailengine">
    <context:exclude-filter expression=".*_Roo_.*" type="regex"/>
    <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

<!-- enable task scheduling annotations -->
<task:annotation-driven/>

<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
    <property name="driverClassName" value="${database.driverClassName}"/>
    <property name="url" value="${database.url}"/>
    <property name="username" value="${database.username}"/>
    <property name="password" value="${database.password}"/>
    <property name="testOnBorrow" value="true"/>
    <property name="testOnReturn" value="true"/>
    <property name="testWhileIdle" value="true"/>
    <property name="timeBetweenEvictionRunsMillis" value="1800000"/>
    <property name="numTestsPerEvictionRun" value="3"/>
    <property name="minEvictableIdleTimeMillis" value="1800000"/>
    <property name="validationQuery" value="SELECT 1"/>
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
    <property name="persistenceUnitName" value="persistenceUnit"/>
    <property name="dataSource" ref="dataSource"/>
</bean>
<bean class="org.springframework.mail.javamail.JavaMailSenderImpl" id="mailSender">
    <property name="host" value="${email.host}"/>
</bean>

<!-- Define Spring beans for classes we never developed -->
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/>

The following method in my EmailServiceImpl class is not being called: 我的EmailServiceImpl类中的以下方法未调用:

@PostConstruct
public void init () {
    System.out.println("############initialize");
    MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
    mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
    mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
    mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
    mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
    mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
    CommandMap.setDefaultCommandMap(mc);
}

After a lot of googling, I still cannot find out why the @PostConstruct method wont run. 经过大量的搜寻后,我仍然无法找出为什么@PostConstruct方法无法运行。 Can anybody suggest anything to try? 有人可以建议尝试吗?

Nevermind, silly mistake on my side....it was calling my @PostConstruct method all along. 没关系,我这边是愚蠢的错误。。。它一直在调用我的@PostConstruct方法。 My Spring config was correct. 我的Spring配置是正确的。 My STS console limit was defaulted to 80,000 characters and there was more than that in Springframework debug lines so I couldnt not see my println in the console, and incorrectly assumed it wasnt being called. 我的STS控制台限制默认为80,000个字符,并且在Springframework调试行中有更多限制,因此我无法在控制台中看到我的println,并且错误地假定未调用它。

I thought I was going mad as I've used @PostConstruct many times without a problem. 我以为我发疯了,因为我已经多次使用@PostConstruct了而没有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM