相关疑难解决方法(0)

不推荐使用Springs XmlBeanFactory

我试着学春天.我关注此网站http://www.roseindia.net/spring/spring3/spring-3-hello-world.shtml

我试过一个例子.我正在使用下面的一些内容,但这里显示:

不推荐使用XmlBeanFactory类型

作为替代方案,我必须使用什么?

public class SpringHelloWorldTest {
    public static void main(String[] args) {

        XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("SpringHelloWorld.xml"));

        Spring3HelloWorld myBean = (Spring3HelloWorld)beanFactory.getBean("Spring3HelloWorldBean");
        myBean.sayHello();
    }
}
Run Code Online (Sandbox Code Playgroud)

java spring xmlbeans deprecated

50
推荐指数
5
解决办法
5万
查看次数

Java Spring多个ApplicationContext

春天的定义ApplicationContext非常模糊,我几乎完成了整本教程,但仍然无法理解它的ApplicationContext立场.

根据Spring API,ApplicationContext是:

  • 用于为应用程序提供配置的中央接口.这在应用程序运行时是只读的,但如果实现支持,则可以重新加载.

  • 用于访问Spring bean容器的根接口.这是bean容器的基本客户端视图.

从上面,我的问题是:

1)我一直看到书中提到的"容器",容器是指什么?一个容器是否意味着一个java进程?或一个容器是指一个ApplicationContext对象?

2)如果我ApplicationContext在一个java应用程序中实例化两个(都在main体内),这两个接口是一个中央容器吗?还是两个单独的实例?看下面的代码,context1和之间有什么区别context2?如果有一个Singleton Beans.xml,它是由context1context2两个独立的实例或同一个实例调用的?

ApplicationContext context1 = new ClassPathXmlApplicationContext("Beans.xml");
ApplicationContext context2 = new ClassPathXmlApplicationContext("Beans.xml");
Run Code Online (Sandbox Code Playgroud)

java spring dependency-injection web-applications

25
推荐指数
3
解决办法
3万
查看次数

17
推荐指数
1
解决办法
4万
查看次数

Spring应用程序上下文和Spring容器之间有什么区别?

我对这两个概念感到困惑.阅读Spring文档,我发现,例如.bean工厂是Spring容器.我还读到"ApplicationContext是BeanFactory的完整超集".但两者之间的差异并不明显.那么区别是什么呢?

spring

16
推荐指数
2
解决办法
1万
查看次数

春豆的生命周期是什么?

我对Spring的生命周期感到困惑.

XmlBeanFactory beanFactory 
= new XmlBeanFactory(new ClassPathResource("SpringHelloWorld.xml"));
Run Code Online (Sandbox Code Playgroud)

上面的代码片段是否创建了对象?

如果上述答案是真的.

a)然后,对于范围为"singleton"的bean,获取在上面的代码片段中创建的对象.我是对还是错?

b)对于范围是"原型"的情况,创建的对象是否未使用.因为,容器总是返回新对象.

XmlBeanFactory beanFactory 
= new XmlBeanFactory(new ClassPathResource("SpringHelloWorld.xml"));
Run Code Online (Sandbox Code Playgroud)

上面的代码片段是否创建了对象?

如果答案是假的,

spring框架如何验证bean定义是否正确.

From the answer of Henry

Usually, singleton beans are created when the context starts. This can be changed with the lazy-init or default-lazy-init attributes.

Prototype beans are only created when needed.

Only syntactically, there might still be errors when the bean is instantiated, for example if a required property is not provided.

java lifecycle spring

11
推荐指数
1
解决办法
6133
查看次数

Spring Context和Bean Lifecycle回调:实际的使用示例

我在春天有一点经验.我想知道Spring Context/Bean Lifecycle中的回调量.我从来没有使用它们,并且可以成像需要大部分的情况.

我的问题是:你能为每个回调至少提供一个使用示例吗?表示需要回调时的情况.

Conext回调: 在此输入图像描述

Bean回调: 在此输入图像描述

PS:

当大多数回调调用时,或者为ApplicationContext编写了一个或另一个实现时,我很清楚.但我无法弄清楚为什么有人可能想要从回调\实施中获利.例如:

  • AbstractRefreshableApplicationContext用于在飞行中更改bean配置.但为什么?在哪种情况下我可能想要在飞行中更改bean的配置?
  • afterPropertiesSet 回调,显然是在设置了所有bean的属性后调用的:)但为什么我应该知道这个,什么时候我应该(可能想要)使用它?

java spring

6
推荐指数
1
解决办法
6317
查看次数

Spring @Autowired构造函数没有找到默认的构造函数

这里是Spring 3.0的一些奇怪行为。

package com.service.schedule;

import org.springframework.stereotype.Component;

@Component("outroJob")
public class OutroJob {

    public void printMe() {
        System.out.println("running...");
    }

}
Run Code Online (Sandbox Code Playgroud)

package com.service.schedule;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

@Component("testeAutowired")
public class TesteAutowired {

    @Autowired
    public TesteAutowired(OutroJob outroJob) {
        outroJob.printMe();
    }

    public static void main(String[] args) {
        ClassPathResource res = new ClassPathResource("applicationContext.xml");
        XmlBeanFactory ctx = new XmlBeanFactory(res);

        OutroJob outroJob = (OutroJob) ctx.getBean("outroJob");
        outroJob.printMe(); // gives: running...

        ctx.getBean("testeAutowired");
    }
}
Run Code Online (Sandbox Code Playgroud)

这些bean均未在applicationContext.xml上声明

因此,行outroJob.printMe(); 工作正常...打印“正在运行...”

但是,当我尝试获取“ testeAutowired” bean时,它说:

无法实例化Bean类[com.service.schedule.TesteAutowired]:找不到默认的构造函数;默认值为0。嵌套异常是java.lang.NoSuchMethodException:com.service.schedule.TesteAutowired。

问题是:为什么,如果Spring找到了“ outroJob” bean,那么它不会在TesteAutowired构造函数上对其进行自动装配?

似乎很明显它必须做什么...

java spring default-constructor autowired

5
推荐指数
1
解决办法
1万
查看次数