标签: autowired

如何@Autowire隐藏在ProxyFactoryBean后面的bean?

假设我们有两个bean,在Spring中定义

<bean class="foo.A"/>
<bean class="foo.B"/>
Run Code Online (Sandbox Code Playgroud)
public class A {
   @Autowired
   private B b;
}

public class B {
   public void foo() {
      ...
   }
}
Run Code Online (Sandbox Code Playgroud)

我想要实现的是截取所有调用B.foo().看一下文档,我写了拦截器C并改变了bean的定义B如下:

public class C implements org.springframework.aop.MethodBeforeAdvice {
    public void before(final Method method, final Object[] args, final Object target) {
        // interception logic goes here
    }
}
Run Code Online (Sandbox Code Playgroud)
<bean class="foo.C"/>

<bean class="org.springframework.aop.framework.ProxyFactoryBean" scope="prototype">
    <property name="proxyTargetClass" value="true"/>
    <property name="singleton" value="false"/>
    <property name="target">
        <bean class="foo.B" scope="prototype"/>
    </property>
    <property name="interceptorNames">
        <list>
            <value>foo.C</value>
        </list>
    </property> …
Run Code Online (Sandbox Code Playgroud)

java spring javabeans autowired interceptor

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

在由多个子类扩展的超类中自动装配Spring bean

将一大堆@Autowired bean放在超类中是否有任何缺点,这些bean在该类中没有使用,而是在扩展超类的子类中使用?

java spring autowired

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

Eclipse无法识别@Inject?

Eclipse没有在我的JSF-Apache Myfaces/Spring/Hibernate项目中识别@Inject.

我试图在setter方法上使用它来注入对我在Spring容器中注册为@Component,@ SessionScoped的类的引用.

是什么赋予了?我似乎在我的POM中定义了最新版本的Spring.

spring inject autowired pom.xml

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

Spring @autowired返回null

这个特定类的成员私有字段(注释为@autowired)位于服务JAR中,当我试图在我的代码中获取该类的实例时,@ autowired成员总是返回null.我在我的项目的context.xml中有下面的组件扫描语句,它试图在JAR中查找base-package,但是仍然会出现空值被注入到带注释的成员中.JAR内部还有一个context.xml,它具有与下面相同的组件扫描条目.JAR源代码现在无法更改,有什么我遗漏的吗?

<!-- local WEB-INF/spring-context/spring-application-context.xml -->
<context:annotation-config /> 
<context:component-scan base-package="blah.blah" />
Run Code Online (Sandbox Code Playgroud)
//this code within my project
//wjc.getMethod() dereferencing comes back null
 public class A{
    WithinJARInterface wjc = new WithinJARInterfaceImpl()
    List someObject = wjc.getMethod()
 }
Run Code Online (Sandbox Code Playgroud)
 //this code interface within JAR
 public interface WithinJARInterface{
    public List getMethod() throws Exception(); 
 }
Run Code Online (Sandbox Code Playgroud)
 //this code within JAR
 public class WithinJARInterfaceImpl implements WithinJARInterface{

    //below member always comes back NULL
    @Autowired
    private JARService jService;

    public List getMethod(){.....}

 }
Run Code Online (Sandbox Code Playgroud)
 public interface JARService{
    public List method1();
 }
Run Code Online (Sandbox Code Playgroud)
  @Service("JARService") 
   public …
Run Code Online (Sandbox Code Playgroud)

java spring autowired

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

Spring没有类型匹配的bean,预计至少有1个bean

得到错误

 No matching bean of type [foo.bar.service.AccountService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Run Code Online (Sandbox Code Playgroud)

我的服务:

public interface AccountService {

@Service
public class AccountServiceImpl implements AccountService {
Run Code Online (Sandbox Code Playgroud)

所以我应该得到实施

在哪里我想得到它:

public class CustomAuthentication implements AuthenticationProvider {

@Autowired
private AccountService accountService;
Run Code Online (Sandbox Code Playgroud)

在我的其他班级也做同样的事情,但它有效.

@Controller
public class AccountController {

@Autowired
private AccountService accountService;
Run Code Online (Sandbox Code Playgroud)

当我从我的那两行删除时CustomAuthentication,没有错误.

配置只是因为:

    <context:component-scan base-package="foo.bar" />
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc autowired

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

Scala with Spring:构造函数自动装配

我正在寻找通过构造函数注入使用Spring自动装配Scala类的"惯用"方法.我尝试过这样的事情:

   @Component
   class MyService @Autowired() ( val myDao: MyDao) extends Logging {
   ...
   }
Run Code Online (Sandbox Code Playgroud)

但是我收到一个错误:bean的实例化失败了; 嵌套的例外是org.springframework.beans.BeanInstantiationException:无法实例化bean类[为MyService]:没有发现默认的构造函数; 嵌套异常是java.lang.NoSuchMethodException:为MyService()在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:964)〜[弹簧豆-3.0.7.RELEASE.jar:3.0. 7.RELEASE]

spring constructor scala autowired

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

我应该使用哪种方法在Spring控制器中注入10多个服务?

我正在开发Spring mvc应用程序.

我有一个控制器,我注入了超过10个服务.

我从这个控制器中暴露了10个以上的url,并且我one or two service在每个方法中都使用了对象.

我在考虑两种方法.

  1. 使用@Autowired直接注入所有服务.
  2. 必要时,在运行时从ApplicationContext获取服务.

请建议我哪种方法更好,或者两种方法在内存使用和时间方面都相同

谢谢

java spring spring-mvc autowired applicationcontext

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

Spring Boot @Autowired在运行时创建实例

作为大多数Spring Boot新用户,我遇到了@Autowired:D的问题

我在这里引用了关于这个注释的大量话题,但仍然无法找到适合我的问题的解决方案.

我们假设我们有这个Spring Boot层次结构:

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

类,我们想要每次调用时实例化:

@Service
public class TestWire {
    public TestWire() {
        System.out.println("INSTANCE CREATED: " + this);
    }
}
Run Code Online (Sandbox Code Playgroud)

out get controller,每次请求都会创建新的SomeRepo对象:

@RestController
public class CreatingRepo {
    @RequestMapping("/")
    public void postMessage() {
        SomeRepo repo = new SomeRepo();
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,使用@Autowired创建TestWire实例的类:

public class SomeRepo {
    @Autowired
    private TestWire testWire;

    public SomeRepo() {
        System.out.println(testWire.toString());
    }
}
Run Code Online (Sandbox Code Playgroud)

我们假设,我们多次向"/"发出GET请求.

因此,因此,只有在项目构建时,TestWire类才会同步,并且@Scope(value ="prototype")proxyMode = ScopedProxyMode.TARGET_CLASS都不 …

java spring dependency-injection autowired spring-boot

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

Spring Boot Autowired失败 - null

我有一个在春季启动应用程序的不同包中找到的3个类,如下所示:为什么@Autowired只在某些类中工作?我做错了什么?

@Configuration
public class Configurations{
   @Autowired
   Prop prop;  //works fine

   @Bean
   //other bean definitions

}

@Component
public class Prop{
   public void method(){};
}

public class User{
   @Autowired
   Prop prop;  //does not work, null

   public void doWork(){
      prop.method();
   }

}
Run Code Online (Sandbox Code Playgroud)

我也试过了@PostConstruct,但结果相同

public class User{
       @Autowired
       Prop prop;  //does not work, null

       @PostConstruct
       public void doWork(){
          prop.method();
       }

    }
Run Code Online (Sandbox Code Playgroud)

autowired spring-boot

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

Autowire all components in a package without implementing an interface

I got a project with tons of services and repositories. Currently each repository is autowired to a service using annotations.

@Service
public class Service1 {
    @Autowired
    private Repository1 repository1;

    @Autowired
    private Repository2 repository2;

    ...100+ more
}
Run Code Online (Sandbox Code Playgroud)

All of these repository are under the same package. Is it possible to skip declaration for each repository?

A simple solution I found would be to implement an interface like this:

@Autowired
private Map<String,RepositoryInterface> repositoryInterface

public void method1(){
    repositoryInterface.get("repository1").doMethod();
}
Run Code Online (Sandbox Code Playgroud)

It should have been …

java spring autowired

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