Java 使用静态变量命名Springbean
我试着给一个豆子取这样的名字:Java 使用静态变量命名Springbean,java,spring,Java,Spring,我试着给一个豆子取这样的名字: <bean id="${EntityManager.ALGO}" class="path.AlgoManagerImpl"> <property name="dao" ref="algoDao" /> </bean> 如何指定一个静态字符串来命名该资源,而不是在任何地方复制/粘贴该特定名称?甚至可能吗?不确定是否可能,但在维护此代码时可能会出现问题。我是spring新手,这是我想要避免的事情之一,因为很难记住
<bean id="${EntityManager.ALGO}" class="path.AlgoManagerImpl">
<property name="dao" ref="algoDao" />
</bean>
如何指定一个静态字符串来命名该资源,而不是在任何地方复制/粘贴该特定名称?甚至可能吗?不确定是否可能,但在维护此代码时可能会出现问题。我是spring新手,这是我想要避免的事情之一,因为很难记住每个bean的名称,为什么您要尝试使用bean名称而不是按类型进行连接?您不应该自己查找bean,这就是您应该使用依赖项注入的原因。因此,可以用XML配置连接,或者使用
@Autowired
获取对EntityManagerFactory
@M.Deinum的引用。注意,使用@Autowired
和EntityManagerFactory
通常会导致问题<代码>@PersistenceUnit。
public interface EntityManager<T> {
/** */
public static final String ALGO = "algoManager654321";
EntityManager<Algo> algoMngr = (EntityManager<Algo>) appContext
.getBean(EntityManager.ALGO);
Offending resource: class path resource ; nested exception is org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 7 in XML document from class path resource is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 101; cvc-datatype-valid.1.2.1: '${EntityManager.ALGO}' is not a valid value for 'NCName'.
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from relative location
Caused by: org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 101; cvc-datatype-valid.1.2.1: '${EntityManager.ALGO}' is not a valid value for 'NCName'.