首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring避免使用bean:使用bean而不是编写bean?

Spring避免使用bean:使用bean而不是编写bean?
EN

Stack Overflow用户
提问于 2012-03-28 10:13:58
回答 2查看 2.6K关注 0票数 0

这是有效的:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security-3.0.xsd">

    <beans:bean id="test2" name="name" class="org.chicago.home.Test2"> </beans:bean>

    <beans:bean id="test1" class="org.chicago.home.Test1">
        <beans:constructor-arg ref="test2"/>
    </beans:bean>
</beans:beans>

这不是。但是,我发现这种类型的配置更好。我应该对命名空间做些什么才能使下面的配置生效?

另外,我可以在constructor arg中传递idref吗?这似乎对我不起作用。构造器-arg中的idref bean="“和idref local="”都不工作。

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/security"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security-3.0.xsd">

    < I am pressing Ctrl + Space here, but it says beans doesn't have any child tags. 

</beans>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-28 11:11:51

您可以使用类似以下内容:

代码语言:javascript
复制
<beans xmlns="http://www.springframework.org/schema/beans"

示例here

对于注入构造器,您可以使用:

代码语言:javascript
复制
<constructor-arg><ref bean="anotherExampleBean"/></constructor-arg>

Spring documentation for inject constructor

票数 0
EN

Stack Overflow用户

发布于 2012-03-28 11:15:10

看起来你在第二个版本中可能有一个打字错误。我认为:

代码语言:javascript
复制
<beans:xmlns="http://www.springframework.org/schema/security"

应该是这样:

代码语言:javascript
复制
<beans xmlns="http://www.springframework.org/schema/security"

除非你只是抄袭错了。如果这不能修复它,你能发布错误消息吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9900260

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档