GVKun编程网logo

spring – @ManagedBean @Component类中的@Autowired服务在JSF请求期间为空(spring中autowired功能)

9

关于spring–@ManagedBean@Component类中的@Autowired服务在JSF请求期间为空和spring中autowired功能的问题就给大家分享到这里,感谢你花时间阅读本站内容

关于spring – @ManagedBean @Component类中的@Autowired服务在JSF请求期间为空spring中autowired功能的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于@Autowired entityManagerFactory为空、@Autowired注释无法在JUnit类中注入bean、@Component 类不能注入 @Autowired 的属性,spring 的 ioc 容器是有这个 bean 的、A component required a bean named ''cacheManager'' that could not be found.等相关知识的信息别忘了在本站进行查找喔。

本文目录一览:

spring – @ManagedBean @Component类中的@Autowired服务在JSF请求期间为空(spring中autowired功能)

spring – @ManagedBean @Component类中的@Autowired服务在JSF请求期间为空(spring中autowired功能)

参见英文答案 > Spring JSF integration: how to inject a Spring component/service in JSF managed bean?                                    3个
我尝试使用JSF 2进行联合Spring 3(MVC).我在Spring和JSF中有一些经验,但之前从未尝试过加入它们.最后我有2个文件

@ManagedBean(name = "userBean")
@Scope
@Component
public class someBean {

  @Autowired
  private TestService testService;

  public void printString() {
    System.out.println(testService.getString());
  }
}

@ManagedBean(name = "studentBean")
@Scope
@Component
public class StudentBean {

  @Autowired
  private TestService testService;

  public void printString() {
    System.out.println(testService.getString());
  }
}

对于这些文件,我有正确的spring,jsf和web.xml配置.并有.xhtml页面,我为’someBean’和’StudentBean’启动printString().我在第一种情况下使用NPE,在第二种情况下在控制台中使用“some string”.
原因很简单 – Spring上下文和JSF中的bean名称不同.所有问题都完成了

@Component => @Component("userBean") 
public class someBean {

在调试中我看到了

private TestService testService;

@Autowired
public void setTestService(TestService testservice) {
  this.testService = testService;
}

当JSF bean创建的testService集不为null时,但在JSF生命周期中它为null时

public void pringString() {
  testService.blah();
}

testService为null.这是我无法理解的.有谁深入了解Spring和JSF详细描述这种情况?

最佳答案
JSF和Spring都可以充当bean容器. @ManagedBean批注指示JSF托管bean工具创建该类的新实例,并在给定名称下对其进行管理. @Component注释指示Spring ApplicationContext创建类的新实例,并在给定名称下对其进行管理.也就是说,JSF和Spring都创建了该类的实例,JSF可以通过EL访问,但是Spring会注入其依赖项(因为,作为一个spring注释,@ Autowired不被JSF托管bean工具理解) .

所以你有一个选择:将JSF托管bean工具用于所有东西(我不推荐,因为它相当有限),使用CDI进行一切(这是一个选项,但不使用Spring),或者使用Spring进行一切(我通常这样做),删除@ManagedBean注释,并通过在faces-config.xml中注册SpringBeanFacesELResolver使Spring bean可以通过EL访问. Spring参考手册在section 19.3.1中对此进行了描述.

@Autowired entityManagerFactory为空

@Autowired entityManagerFactory为空

我的应用无法自动装配entityManagerFactory。

我的 applicationContext.xml

<tx:annotation-driven/>
<context:component-scan base-package="top.level.package" />

<bean id="persistenceUnitManager">
    <property name="persistenceXmlLocation">
        <value>classpath:jpa-persistence.xml</value>
    </property>
</bean>

<bean id="entityManagerFactory">
    <property name="persistenceUnitManager" ref="persistenceUnitManager" />
</bean>

<bean id="transactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

我的 java课

@Component
public class Engine {

    @Autowired
    @Qualifier("entityManagerFactory")
    private EntityManagerFactory entityManagerFactory;
......
}

题:

为什么entityManagerFactory为空?

@Autowired注释无法在JUnit类中注入bean

@Autowired注释无法在JUnit类中注入bean

我的测试课:

public class myTest extends TestCase{
@Autowired
BeanClass beanObject
public void beanTest()
{
Classdata data = beanObject.getMethod();
}
}

我在行上得到一个空指针异常:

Classdata data = beanObject.getMethod();

beanObject.getMethod();精确的给出空指针异常

如何使Junit类中的beanObject字段自动装配成为可能,以便可以使用“ BeanClass”类中的方法?


从评论中复制:

用简单的术语来说.. beanClass是具有某些方法的接口..我用@Service("beanObject")
注释标记了该beanClass。.banClass
是由具有方法实现的beanClassImpl类实现的。.我需要在我的testClass中使用这些实现来获取要比较的数据..为此,我正在@Autowired我的testClass..mi中执行
beanClass beanObject。

@Component 类不能注入 @Autowired 的属性,spring 的 ioc 容器是有这个 bean 的

@Component 类不能注入 @Autowired 的属性,spring 的 ioc 容器是有这个 bean 的

正常的 controller 能通过 @Autowired 注入这个类,但是在 @Component 类里,不能通过 @Autowired 注入这个类,  spring 的 ioc 有这个 bean

A component required a bean named ''cacheManager'' that could not be found.

A component required a bean named ''cacheManager'' that could not be found.

***************************
APPLICATION FAILED TO START
***************************

Description:

A component required a bean named ''cacheManager'' that could not be found.


Action:

Consider defining a bean named ''cacheManager'' in your configuration.

在使用 springboot 集成 hazelcast 时候,启动时候报错。

需要在启动类中加入一个 bean

    @Bean
    public CacheManager cacheManager() {
        return new HazelcastCacheManager();
    }

这样启动后,可以进行正常启动

或者添加配置文件为:

@Configuration
public class HazelcaseConfig {

    /**
     * @description 3.创建Hazelcase的Config类
     */
    @Bean
    public Config getConfig() {
        Config hazelcaseConfig = new Config();
//        MapConfig mapConfig = new MapConfig();
//        mapConfig.setName("myMap");// 设置当前的mapConfig的名称
        hazelcaseConfig.setInstanceName("test-hazelcase");// 设置当前创建的实例的名称
//                .addMapConfig(mapConfig);//添加当前的map
        return hazelcaseConfig;
    }
}

可以进行实现

关于spring – @ManagedBean @Component类中的@Autowired服务在JSF请求期间为空spring中autowired功能的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于@Autowired entityManagerFactory为空、@Autowired注释无法在JUnit类中注入bean、@Component 类不能注入 @Autowired 的属性,spring 的 ioc 容器是有这个 bean 的、A component required a bean named ''cacheManager'' that could not be found.等相关内容,可以在本站寻找。

本文标签: