我是Spring的新手.
这是bean注册的代码:
<bean id="user" class="User_Imple"> </bean>
<bean id="userdeff" class="User"> </bean>
Run Code Online (Sandbox Code Playgroud)
这是我的bean类:
public class User_Imple implements Master_interface {
private int id;
private User user; // here user is another class
public User_Imple() {
super();
}
public User_Imple(int id, User user) {
super();
this.id = id;
this.user = user;
}
// some extra functions here....
}
Run Code Online (Sandbox Code Playgroud)
这是我执行操作的主要方法:
public static void main(String arg[]) {
ApplicationContext context = new ClassPathXmlApplicationContext("/bean.xml");
Master_interface master = (Master_interface)context.getBean("user");
// here is my some operations..
int id = ... …Run Code Online (Sandbox Code Playgroud) 在CDI中,您可以使用以下命令定义一个对象,该对象将为您提供特定类型的项目:
@Inject Instance<MyObject> myObjectInstance; //... MyObject myObjectInstance.get();
同样在Guice中你可以做到:
@Inject Provider<MyObject> myObjectInstance; //... MyObject myObjectInstance.get();
我想知道Spring中是否有类似的构造,或者你必须使用它ApplicationContext才能获得引用?
我刚刚开始使用Spring IOC概念.我经常看到互联网上发现的大多数例子都使用代码来获取对象.
ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Hello hello = (Hello) appContext.getBean("hello");
Run Code Online (Sandbox Code Playgroud)
作为stackoverflow中这些问题1和2的参考.我推断,没有必要在代码中使用appContext.getBean("hello"),这被认为是不好的做法.此外,不再推荐.在这里纠正我,如果我的推论是错误的.
保持这一点,我相应地改变了我的项目.这是我的applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<bean id="utilClassRef" class="org.hd.derbyops.DUtils" lazy-init="false" />
<bean id="appContext" class="org.hd.derbyops.ContextProvider" lazy-init="false">
<property name="utils" ref="utilClassRef" />
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
我的contextProvider类代码
public class ContextProvider implements ApplicationContextAware {
private static ApplicationContext ctx;
/**
* Objects as properties
*/
private static DUtils utils;
public void setApplicationContext(ApplicationContext appContext)
throws BeansException {
ctx = appContext;
}
public static ApplicationContext getApplicationContext() {
return ctx; …Run Code Online (Sandbox Code Playgroud) 我有一个名为的界面MyInterface.实现的类MyInterface(允许调用它MyImplClass)也实现了Runnable接口,因此我可以使用它来实例化线程.这是我的代码.
for (OtherClass obj : someList) {
MyInterface myInter = new MyImplClass(obj);
Thread t = new Thread(myInter);
t.start();
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是在我的ApplicationContext.xml中声明实现类,并为每次迭代获取一个新实例.所以我的代码看起来像这样:
for (OtherClass obj : someList) {
MyInterface myInter = // getting the implementation from elsewhere
Thread t = new Thread(myInter);
t.start();
}
Run Code Online (Sandbox Code Playgroud)
如果可能的话,我还想保留IoC模式.
我怎么能这样做?
谢谢
我有一个使用服务的记录器类.每次创建新的记录器时,我都希望能够访问单例范围的日志记录服务.
我将日志记录服务自动装入logger,但是返回了一个空指针异常.我尝试了一些解决方案:
我试图在junit测试中使用它,并且我确实指定了上下文文件以使用不同的应用程序上下文.但即使保持相同,也无法解决问题.
请在下面找到代码:
以下是应用程序上下文的摘录.
<context:component-scan base-package="com.platform"/>
<bean id="asyncLoggingService" class="com.platform.services.AsyncLoggingServiceImplementation" scope="prototype"/>
Run Code Online (Sandbox Code Playgroud)
以下是Logger类.
package com.platform.utils;
import com.platform.services.AsyncLoggingService;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
public class OurLogger
{
private static Logger logger;
@Autowired
private AsyncLoggingervice asyncLoggingService;
public OurLogger(Class Clazz)
{
logger = LoggerFactory.getLogger(Clazz);
}
public void trace(TraceableObject object, String message)
{
//Do nothing yet
}
Run Code Online (Sandbox Code Playgroud)
}
然后我在另一个服务中使用Logger来记录最新情况.(我写另一个记录器的原因是使用RabbitMQ服务器)在服务中我实例化Logger的一个新实例,然后相应地使用它.
@Service
public class AsyncAccountServiceImplementation implements AsyncAccountService
{
private static final String GATEWAY_IP_BLOCK = "1";
private static OurLogger logger = new OurLogger(AsyncAccountServiceImplementation.class); …Run Code Online (Sandbox Code Playgroud) 有没有办法将Spring bean自动转换为应用程序上下文XML中定义的类?我想避免在两个位置放置关于bean的类型信息....在xml配置文件中以及在代码中作为强制转换.
例如,给定此配置文件
<bean id="bean-name" class="SimpleSpringBean" scope="prototype">
<property name="myValue" value="simple value"></property>
</bean>
Run Code Online (Sandbox Code Playgroud)
我可以这样调用ApplicationContext.getBean("bean-name")以避免直接将返回类型转换为SimpleStringBean.我知道我也可以打电话ApplicationContext.getBean("bean-name", SimpleSpringBean.class)来避免演员本身,但我仍然有2个地方的类型信息.
看起来Spring可以获取类info(ApplicationContext.getType)或者从bean本身获取类型,但没有办法在没有程序员干预的情况下自动转换类型.
我有一个具有多个屏幕的应用程序,并通过按钮选择每个屏幕.每个屏幕都包含相当重的组件,因此重要的是只有激活屏幕在内存中 - 所有其他屏幕都应该可用于垃圾收集.
该应用程序使用Spring作为粘合剂,目前它使用getBean()切换屏幕:
//event handler for a specific button
public void actionPerformed(Event e) {
setScreen( (Screen) applicationContext.getBean("screen1"));
}
Run Code Online (Sandbox Code Playgroud)
"screen1"是原型bean,因此在按下按钮时会创建一个新的屏幕实例.此外,setScreen()是唯一在应用程序中维护对屏幕的引用的位置,因此之前活动的屏幕可用于垃圾回收.我还没有测试过这个,但我希望它能正常工作 - 这里没有火箭科学!
问题是 - 在阅读了这个页面后,为什么getBean()被认为是坏的 - 我想知道是否有一种更惯用的方法可以在删除对getBean()的依赖时获得相同的结果.
我已经看过方法注入,它在我看来引入复杂性并没什么好处.这是学习的另一个概念,更多魔术,增加对CGLIB的依赖等.如果我真的想要删除对Spring的依赖,我可以引入一个暴露getBean()方法的接口.
getBean()和方法注入是我的唯一选项还是我错过了什么?
如果是这样,getBean()真的那么糟糕吗?
我试图找到一种在Spring启动中创建优美的Runnable bean的方法。该应用程序的重点是提供一项服务,该服务将接收一些数据并启动受监视的外部过程。
在之前的尝试中,我只是形成一个常规new MyRunnable()并将其传递给执行服务。现在,我正在考虑如何在Spring环境中正确使用并使用@Scope("prototype")。
我确实找到了使用的示例,ApplicationContext.getBean(...)以及为什么Spring的ApplicationContext.getBean被认为是不好的更好的方法?,但我仍然无法正确理解如何new MyRunnable()根据一项服务来实际调用,这将遵循以下简单思想:
class MyService {
public void triggerNewExternalTask() {
....
executionService.run(new MyRunnable());
Run Code Online (Sandbox Code Playgroud)