Alfresco注入Bean

3
我知道如何将Bean注入Java Web脚本中,这很简单: ```

我知道如何将Bean注入Java Web脚本中。这非常简单:

```
<bean id="webscript.ru.mycompany.maypackage.myclass.get"
    class="ru.mycompany.maypackage.myclass"
    parent="webscript">             
    <property name="properties">
            <ref bean="global-properties"/>
    </property>
</bean>

但是对于自定义Java类,它并不起作用,变量属性为空 :(

<bean id="myclass"
    class="ru.mycompany.maypackage.myclass">                
    <property name="properties">
            <ref bean="global-properties"/>
    </property>
</bean>

public class myclass
{
    private Properties properties;
    public myclass()
    {

    }

    public void setProperties(Properties properties) 
    {
        this.properties = properties;       
    }
}

What should I do?


1
你如何调用myclass的实例?也许你是通过“new”运算符来调用你的myclass。 - M.Diachenko
是的,new myclass().mymethod()。 - Slava Lazarev
2个回答

1
您需要将myclass bean注入到其他bean中(在您想使用它的地方),方法与您在myclass类中注入全局属性相同。如果您通过“new”操作符创建实例,则Spring IOC对myclass一无所知。

0
你需要实现ApplicationContextAware,这样你就能够获取到。
public class myclass implements ApplicationContextAware

或者你需要使用自动装配,例如:

@Value("${custom.url}")
protected String url;

谢谢,但 MyClass 在仓库端,而不是共享端。 - Slava Lazarev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接