相关疑难解决方法(0)

在抽象基类中使用@autowired

据我所知,field injection不推荐.应该使用constructor.

我在这里尝试做的是@Autowired在基类的构造函数中使用,并使其可供所有子类访问.在某些子类中,我还需要一些特定的bean @Autowired来自它们的构造函数.演示代码如下:

基类:

public abstract class Base {

    protected final MyDemoService myDemoService;

    @Autowired
    public Base(MyDemoService myDemoService) {
        this.myDemoService = myDemoService;
    }
}
Run Code Online (Sandbox Code Playgroud)

继承(子)类:

public class Sub extends Base {

    private final MySubService mySubService;

    @Autowired
    public Sub(MySubService mySubService) {
        this.mySubService = mySubService;
    }
} 
Run Code Online (Sandbox Code Playgroud)

这将给我一个'无默认构造函数'错误.
它类似于问题:类似的问题和答案,但它在这里不起作用.


我已经潜入了一段时间,我发现这篇文章dependency injection:进一步阅读

我在想Setter Injection我的问题是正确的方法吗?

塞特犬注射:

public abstract class Base {

    protected MyDemoService myDemoService;

    @Autowired
    public void setMyDemoService(MyDemoService myDemoService) { …
Run Code Online (Sandbox Code Playgroud)

java spring javabeans spring-boot

14
推荐指数
2
解决办法
7947
查看次数

带有注释的Bean定义继承?

是否可以使用基于注释的配置(@Bean等)实现相同的bean继承?

<bean id="inheritedTestBean" abstract="true"
        class="org.springframework.beans.TestBean">
    <property name="name" value="parent"/>
    <property name="age" value="1"/>
</bean>

<bean id="inheritsWithDifferentClass"
        class="org.springframework.beans.DerivedTestBean"
        parent="inheritedTestBean" init-method="initialize">
    <property name="name" value="override"/>
    <!-- the age property value of 1 will be inherited from parent -->
</bean>
Run Code Online (Sandbox Code Playgroud)

http://docs.spring.io/spring/docs/4.1.0.BUILD-SNAPSHOT/spring-framework-reference/htmlsingle/#beans-child-bean-definitions

java spring

12
推荐指数
1
解决办法
5711
查看次数

Spring IoC/DI 中接口使用 @Component 注解进行注解。可能是什么原因?

有时接口会使用@Component 注解进行注解。那么我的明显推理是实现此类接口的类也将被视为组件。但如果我是对的,情况就不是这样了。

那么接口上@Component注解的目的是什么呢?

java spring dependency-injection inversion-of-control spring-ioc

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

Spring Boot:如何在运行时更改内容安全策略?

我正在尝试热重新加载 Spring Boot 应用程序的内容安全策略(CSP)中的更改,即用户应该能够通过管理 UI 更改它,而无需重新启动服务器。

Spring Boot 中常规的做法是:

@Configuration
class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) {
        // ... lots more config here...
        http.headers()
            .addHeaderWriter(
                 StaticHeadersWriter(
                     "Content-Security-Policy", 
                     "<some policy string>"
                 )
            )
    } 
}
Run Code Online (Sandbox Code Playgroud)

...但这不允许在分配后重新配置。

我可以在运行时(重新)配置它吗?重新加载应用程序上下文不是一个选项,我只需要能够适应这个特定的设置。

java spring content-security-policy spring-boot

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