annotation-config可以在java应用程序项目中工作吗?

时间:2017-06-22 12:00:37

标签: java spring

我定义了一个类加载器并添加了我的所有库,然后我定义了一个ApplicationContext,我尝试使用ApplicationContext从外部jar中的类中获取getBean,这里是我的密钥代码:

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

我试着像这样getBean:

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

但问题是,如果我定义了哪个属性具有setter / getter方法的目标类,则可以使用两个属性注入实例对象,类定义如下:

    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;
        }
    }

但如果使用自动装配的注释定义它,它就不起作用:

    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),实例被注入,但它的两个属性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)),谢谢你的帮助~~~

3 个答案:

答案 0 :(得分:1)

我明白了。

添加或两者都可以。

我遇到一个问题,当我添加上面的任何配置时,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

非常感谢。

答案 1 :(得分:0)

请将<context:component-scan base-package="com.taobao.qa" />添加到您的配置

答案 2 :(得分:0)

mvc:annotation-driven 用于使用其默认配置启用Spring MVC组件。

如果您不包含mvc:annotation-driven,如果您使用上下文:component-scan 来创建bean或在XML文件中定义bean,那么您的MVC应用程序也会起作用

spring配置文件中的

context:component-scan 元素将不再需要声明XML文件中的所有bean。请查看spring配置文件中的以下声明:

<context:component-scan base-package="org.controller"/>

spring应用程序配置文件中的上述声明将扫描指定包中的类并创建beans实例。请注意,只有在使用正确的注释对该类进行批注时,它才能创建bean。以下是此元素扫描的注释:

  • @Component
  • @Repository
  • @服务
  • @Controller