gpt4 book ai didi

java - 注释配置可以在java应用程序项目中工作吗?

转载 作者:行者123 更新时间:2023-12-02 12:47:54 24 4
gpt4 key购买 nike

我定义了一个类加载器并添加了所有库,然后定义了一个ApplicationContext,我尝试从外部jar中的类中获取带有ApplicationContext的Bean,这是我的关键代码:

        ApplicationContext context = new FileSystemXmlApplicationContext(beanPath) {
@Override
protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {
super.initBeanDefinitionReader(reader);
reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
reader.setBeanClassLoader(classLoader);
}
};

我尝试像这样获取Bean:

AswanService instance = (AswanService)context.getBean("aswanService", AswanService.class);

但问题是,如果我定义目标类的属性有setter/getter方法,则实例对象可以注入(inject)这两个属性,类定义如下:

    public class AswanServiceImpl implements AswanService {
SpringInjectFaker springInjectFaker;

ItemDO itemDO;

public AswanResponse request(AswanRequest aswanRequest) {
springInjectFaker.fake();

AswanResponse response = new AswanResponse();
response.setName("response: " + aswanRequest.getName() + "; " + itemDO.getItemId());
return response;
}

public SpringInjectFaker getSpringInjectFaker() {
return springInjectFaker;
}

public void setSpringInjectFaker(SpringInjectFaker springInjectFaker) {
this.springInjectFaker = springInjectFaker;
}

public ItemDO getItemDO() {
return itemDO;
}

public void setItemDO(ItemDO itemDO) {
this.itemDO = itemDO;
}
}

但如果它是用 Autowiring 注释定义的,则不起作用:

    public class AswanServiceImpl implements AswanService {
@Autowired
SpringInjectFaker springInjectFaker;

@Autowired
ItemDO itemDO;

public AswanResponse request(AswanRequest aswanRequest) {
springInjectFaker.fake();

AswanResponse response = new AswanResponse();
response.setName("response: " + aswanRequest.getName() + "; " + itemDO.getItemId());
return response;
}
}

instance = context.getBean("aswanService", AswanService.class),instance被注入(inject),但它的两个属性itemDO和springInjectFaker都为null。

我尝试添加配置,但没有成功。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">

<context:annotation-config/>

<bean id="aswanService" class="com.taobao.qa.perf.tr.AswanServiceImpl" autowire="byName"></bean>
<bean id="springInjectFaker" class="com.taobao.qa.perf.tr.SpringInjectFakerImp" autowire="byName"></bean>
<bean id="itemDO" class="com.taobao.qa.perf.tr.ItemDO" autowire="byName">
<constructor-arg index="0" value="1"></constructor-arg>
<constructor-arg index="1" value="2"></constructor-arg>
</bean>
</beans>

我的项目是java项目,我在main函数中运行程序(public static void main(String[] args)),谢谢你的帮助~~~

最佳答案

我明白了。

添加或都可以。

我遇到一个问题,当我添加上面的任何配置时,IDE 会抛出异常:

org.springframework.beans.factory.BeanDefinitionStoreException: Parser configuration exception parsing XML from URL [file:xxxxxxx/beans.xml]; nested exception is javax.xml.parsers.ParserConfigurationException: Unable to validate using XSD: Your JAXP provider [org.apache.xerces.jaxp.DocumentBuilderFactoryImpl@1d89c040] does not support XML Schema. Are you running on Java 1.4 with Apache Crimson? Upgrade to Apache Xerces (or Java 1.5) for full XSD support.
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:404)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451)
at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:140)
at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:84)
at com.alibaba.middleware.Application$2.<init>(Application.java:159)
at com.alibaba.middleware.Application.main(Application.java:159)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: javax.xml.parsers.ParserConfigurationException: Unable to validate using XSD: Your JAXP provider [org.apache.xerces.jaxp.DocumentBuilderFactoryImpl@1d89c040] does not support XML Schema. Are you running on Java 1.4 with Apache Crimson? Upgrade to Apache Xerces (or Java 1.5) for full XSD support.
at org.springframework.beans.factory.xml.DefaultDocumentLoader.createDocumentBuilderFactory(DefaultDocumentLoader.java:102)
at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:70)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:388)
... 20 more
Caused by: java.lang.IllegalArgumentException: No attributes are implemented
at org.apache.xerces.jaxp.DocumentBuilderFactoryImpl.setAttribute(DocumentBuilderFactoryImpl.java:93)
at org.springframework.beans.factory.xml.DefaultDocumentLoader.createDocumentBuilderFactory(DefaultDocumentLoader.java:99)
... 22 more

所以我放弃了,我又试了一次,直到我发现在VM选项中添加两个args可以解决这个异常,我的问题也解决了。不知道为什么,还在学习中。

-Djavax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
-Djavax.xml.parsers.SAXParserFactory=com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl

非常感谢。

关于java - 注释配置可以在java应用程序项目中工作吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44698870/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com