相关疑难解决方法(0)

使用@Configurable进行Spring自动装配

我正在尝试使用Spring @Configurable并将@AutowireDAO注入域对象,这样他们就不需要直接了解持久层了.

我正在尝试关注http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-atconfigurable,但我的代码似乎没有效果.

基本上,我有:

@Configurable
public class Artist {

    @Autowired
    private ArtistDAO artistDao;

    public void setArtistDao(ArtistDAO artistDao) {
        this.artistDao = artistDao;
    }

    public void save() {
        artistDao.save(this);
    }

}
Run Code Online (Sandbox Code Playgroud)

和:

public interface ArtistDAO {

    public void save(Artist artist);

}
Run Code Online (Sandbox Code Playgroud)

@Component
public class ArtistDAOImpl implements ArtistDAO {

    @Override
    public void save(Artist artist) {
        System.out.println("saving");
    }

}
Run Code Online (Sandbox Code Playgroud)

在application-context.xml中,我有:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springsource.org/dtd/spring-beans-2.0.dtd">
<beans>

    <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />
    <bean class="org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect" factory-method="aspectOf"/>

</beans>
Run Code Online (Sandbox Code Playgroud)

类路径扫描和初始化由弹簧模块执行Play!框架,虽然其他autowired …

java spring aspectj spring-aop

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

访问AttributeConverter类中的Spring Bean

我正在开发一个Spring Data JPA应用程序,并且我已经创建了一个AttributeConverter类,以便ArrayList在数据库列中将对象保存为JSON.在这个类中,我需要使用我定义为Spring Bean的类.

由于AttributeConverter该类由Hibernate管理,它似乎在创建任何Spring bean之前被实例化,因此DI似乎不起作用(类中的Spring Bean AttributeConverternull,并且我正在NullPointer抛出异常).所以目前我正在创建另一个所述bean的实例,以便能够在AttributeConverter类中使用它(这违背了DI的目的).

我也尝试创建一个实现的Util类(带注释@Component)ApplicationContextAware,它提供了一个给出SpringBean(cxt.getBean(BeanClass.class))的方法.但是,这也被实例化AttributeConverter.

有什么想法可以解决这个问题吗?

谢谢.

spring hibernate jpa

9
推荐指数
3
解决办法
3241
查看次数

Spring:不带@Component的@Autowired

在实际项目中,我发现@Component以下代码中可能会省略:

// no @Component !!!!!
public class MovieRecommender {

    private final CustomerPreference customerPreference;

    @Autowired
    public MovieRecommender(CustomerPreference customerPreference) {
        this.customerPreference = customerPreference;
    }

    // ...
}

@Component
public class CustomerPreference {...}
Run Code Online (Sandbox Code Playgroud)

(该示例取自Spring官方文档https://docs.spring.io/spring-framework/docs/4.3.x/spring-framework-reference/htmlsingle/#beans-autowired-annotation,文档显示没有@Component根本没有,这可能意味着不需要它,或者只是没有显示。)

我工作的项目不使用任何 XML bean 声明,但它使用 Spring 以外的框架,因此可能有某些东西将类声明为 bean。或者它可能是我们使用的 Spring 版本的一个特性,如果该特性没有记录下来,它可能会在以后被删除。

问题: 使用的类必须用(好吧,是一个bean)@Autowired注释吗?@Component有相关的官方文档吗?

UPD伙计们,项目中没有@Configuration也没有 XML 配置,我知道这样的声明从类中创建了一个 bean,但问题不在于它们。我什至在上面的问题中写了“(好吧,做个豆子)”来涵盖这一点。是否@Autowired在不是 bean 的类中工作?或者也许它声明了将其用作 bean 的类?

java spring spring-boot

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

Vaadin 组件作为 Spring bean

题:

Vaadin 组件何时可以是 spring 容器中的 bean(@SpringComponent注释)?

问题澄清:

我问这个问题是因为我知道 Vaadin View 在使用@SpringView. 但是如果我Button@SpringComponent它注释组件将只创建一次。它可以制造任何问题吗?

例子:

我有很多 JpaRepository bean:

public interface CustomerRepository extends JpaRepository<Customer, Long> 
// ...

public interface UserRepository extends JpaRepository<User, Long> {
// ...
Run Code Online (Sandbox Code Playgroud)

我想在不同的地方使用它们 - 例如在 Tab 组件中(在 Vaadin TabSheet 中)。所以我有一个想法 -tabContent也可以是弹簧组件:

@SpringView(name = "viewName")
public class SomeView extends VerticalLayout implements View {

    @Autowired
    private SomeTabContent tabContent;
    //...
    public void init() { // call every view enter()
        removeAllComponents();
        // Initialize …
Run Code Online (Sandbox Code Playgroud)

java spring vaadin vaadin7

6
推荐指数
2
解决办法
2827
查看次数

标签 统计

spring ×4

java ×3

aspectj ×1

hibernate ×1

jpa ×1

spring-aop ×1

spring-boot ×1

vaadin ×1

vaadin7 ×1