Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否有一种基于注释的方法来获取springs应用程序上下文?_Java_Spring - Fatal编程技术网

Java 是否有一种基于注释的方法来获取springs应用程序上下文?

Java 是否有一种基于注释的方法来获取springs应用程序上下文?,java,spring,Java,Spring,我想知道是否有一种基于注释的方法来启动spring应用程序 i、 e.将其替换如下: public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml"); User user = (User)ctx.getBean("user"); user.createUser(); } 使用基于注释的方法

我想知道是否有一种基于注释的方法来启动spring应用程序

i、 e.将其替换如下:

    public static void main(String[] args) {

    ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");  

    User user = (User)ctx.getBean("user");
    user.createUser();

}

使用基于注释的方法获取上下文,然后在bean中自动连接?

我认为您无法做到这一点,毕竟需要有人理解和处理该注释。如果Spring还没有初始化,谁会呢?

我认为你做不到,毕竟需要有人理解和处理这个注释。如果Spring还没有初始化,谁会呢?

您不能避免使用ClassPathApplicationContextXml代码,但可以通过如下操作避免使用ctx.getBeaUser。理想情况下,它会要求xml扫描spring想要注入内容的包。这里您唯一不应该做的是,我已经将我的主类声明为spring组件,因为spring注释在spring识别的类上工作,因此我将我的主类声明为spring识别的类。无法避免使用Classpathapplicationcontext加载xml

package com.spring.sample;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import com.spring.sample.component.Sample;

@Component
public class SampleMain {

    @Autowired
    Sample testSample;

    static ApplicationContext appCtx = new ClassPathXmlApplicationContext("META-INF/webmvc-application.xml");

    public static void main(String[] args){

        SampleMain sampleMain = appCtx.getBean(SampleMain.class);
        sampleMain.invokeSample();
    }

    private void invokeSample(){
        testSample.invokeSample();
    }

}

您无法避免使用ClassPathApplicationContextXml代码,但可以通过如下操作避免使用ctx.getBeaUser。理想情况下,它会要求xml扫描spring想要注入内容的包。这里您唯一不应该做的是,我已经将我的主类声明为spring组件,因为spring注释在spring识别的类上工作,因此我将我的主类声明为spring识别的类。无法避免使用Classpathapplicationcontext加载xml

package com.spring.sample;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import com.spring.sample.component.Sample;

@Component
public class SampleMain {

    @Autowired
    Sample testSample;

    static ApplicationContext appCtx = new ClassPathXmlApplicationContext("META-INF/webmvc-application.xml");

    public static void main(String[] args){

        SampleMain sampleMain = appCtx.getBean(SampleMain.class);
        sampleMain.invokeSample();
    }

    private void invokeSample(){
        testSample.invokeSample();
    }

}

spring中有一个名为@ContextConfiguration的函数 非常有助于测试。 您需要扩展为TestSupportTestNG或JUnit创建的一个spring抽象类

e、 g.针对JUnit的AbstractTransactionalTestNGSpringContextTests或AbstractTransactionalJUnit4SpringContextTests

然后您只需使用@ContextConfiguration annotationfor类、包含注释类型的接口或枚举声明

junit测试的一些示例代码:

请阅读:

春天有一个名字叫@ContextConfiguration 非常有助于测试。 您需要扩展为TestSupportTestNG或JUnit创建的一个spring抽象类

e、 g.针对JUnit的AbstractTransactionalTestNGSpringContextTests或AbstractTransactionalJUnit4SpringContextTests

然后您只需使用@ContextConfiguration annotationfor类、包含注释类型的接口或枚举声明

junit测试的一些示例代码:

请阅读: