Spring应用中注入Spring bean的情况总结

1.在一个应用类(也是一个Spring bean)中,通过Spring bean注入接口的实现类

  • 接口定义如下:
public interface AnInterface {
    String getMessage();
}

1) 只定义了接口的一个实现类(这也是最常见的一种情况)

  • 实现类定义如下:
@Component
public class AnImpl implements AnInterface {
	...
  • 则应用类中的依赖注入定义如下:

 

@Configuration
@ComponentScan
class MyRunner {
	@Autowired
	private AnInterface aReference;
        ...

2) 定义了接口的多个实现类(注入时需要指出注入哪个实现类)

  • 定义一个默认实现类
@Component("aDefaultImpl")
@Primary
public class ADefaultImpl implements AnInterface {
	...

注意,这里的@Primary,表明主实现类(即默认实现类)

  • 定义其他非默认实现类
@Component("aNonDefaultImpl")
public class ANonDefaultImpl implements AnInterface {
	...

注意,这就是个基本Spring bean。

  • 在应用类中注入默认实现类
@Configuration
@ComponentScan
class MyRunner {
	@Autowired
	private AnInterface aReference;
        ...

注意,注入默认实现类时,用法等同于只有一个主实现类。

  • 应用类中注入非默认实现类时(需要给出实现类的Spring bean名称)
@Configuration
@ComponentScan
class MyRunner {
	@Autowired
	@Qualifier("aNonDefaultImpl")
	private AnInterface aReference;
	...

注意,这里的@Qualifier,给出了要注入的实现类的Spring bean名称

除此之外,还可以使用@javax.annotation.Resource如下:

@Configuration
@ComponentScan
class MyRunner {
	@javax.annotation.Resource(name="aNonDefaultImpl")
	private AnInterface aReference;
	...


2.在应用类(不是一个Spring bean)中,通过Spring bean注入接口的实现类

 

  • 接口定义同上AnInterface
  • 定义接口的实现类同上AnImpl
  • 在应用类中引用接口的实现类
@Configurable
public class ANoneSpringBean {
	@Autowired
	private AnInterface aReference;
        ...

注意,这里的@Configurable,表明该类虽然不是Spring bean,但是还是希望能够借助于Spring来注入必要对象。

  • 在另一个应用类(该应用类是Spring bean)中,为非Spring bean的ANoneSpringBean注入必要对象依赖对象
@Configuration
@ComponentScan
@EnableSpringConfigured
@EnableLoadTimeWeaving
public class MyApplication {
	public aMethod() {
		ApplicationContext context = new AnnotationConfigApplicationContext(MyApplication.class);//Spring context中有被注入的实现类对象
		public ANoneSpringBean aNoneSpringBean = new ANonSpringBean();//为ANoneSpringBean注入AnInterface对象
		...//后续就可以访问ANoneSpringBean中的aReference对象

注意,在new ANoneSpringBean()之前,必须初始化Spring的ApplicationCentext,并且AnImpl类必须能够被加入到该ApplicationCentext。

此外,如果是Maven项目,必须有如下依赖:

    
        org.springframework
        spring-context
        4.3.9.RELEASE
    
    
        org.springframework
        spring-core
        4.3.9.RELEASE
    
    
        org.springframework
        spring-beans
        4.3.9.RELEASE
    
    
        org.springframework
        spring-aspects
        4.3.9.RELEASE
    
    
        org.springframework
        spring-instrument
        4.3.9.RELEASE
    
    
      org.aspectj
      aspectjrt
      1.8.9
    

运行时,还需要设置如下JVM启动参数:

-javaagent:C:\Users\myname\.m2\repository\org\springframework\spring-instrument\4.3.9.RELEASE\spring-instrument-4.3.9.RELEASE.jar

运行结果如下:

Aug 23, 2017 5:26:52 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1a6c5a9e: startup date [Wed Aug 23 17:26:52 CST 2017]; root of context hierarchy
Aug 23, 2017 5:26:53 PM org.springframework.context.weaving.DefaultContextLoadTimeWeaver setBeanClassLoader
INFO: Found Spring's JVM agent for instrumentation
Aug 23, 2017 5:26:53 PM org.springframework.context.weaving.DefaultContextLoadTimeWeaver setBeanClassLoader
INFO: Found Spring's JVM agent for instrumentation
[AppClassLoader@18b4aac2] error can't determine superclass of missing type org.springframework.transaction.interceptor.TransactionAspectSupport
 [Xlint:cantFindType]
[AppClassLoader@18b4aac2] error can't determine annotations of missing type org.springframework.transaction.annotation.Transactional
when weaving type org.springframework.aop.scope.ScopedProxyUtils
when weaving classes 
when weaving 
 [Xlint:cantFindType]
[AppClassLoader@18b4aac2] error can't determine annotations of missing type org.springframework.transaction.annotation.Transactional
when weaving type org.springframework.aop.scope.ScopedProxyUtils
when weaving classes 
when weaving 
 [Xlint:cantFindType]
[AppClassLoader@18b4aac2] error can't determine annotations of missing type org.springframework.transaction.annotation.Transactional
when weaving type org.springframework.aop.scope.ScopedProxyUtils
when weaving classes 
when weaving 
 [Xlint:cantFindType]
[AppClassLoader@18b4aac2] error can't determine annotations of missing type org.springframework.transaction.annotation.Transactional
when weaving type org.springframework.aop.scope.ScopedObject
when weaving classes 
when weaving 
 [Xlint:cantFindType]
[AppClassLoader@18b4aac2] error can't determine annotations of missing type org.springframework.transaction.annotation.Transactional
when weaving type org.springframework.aop.RawTargetAccess
when weaving classes 
when weaving 
 [Xlint:cantFindType]
[AppClassLoader@18b4aac2] error can't determine annotations of missing type org.springframework.transaction.annotation.Transactional
when weaving type org.springframework.context.event.EventListenerMethodProcessor$1
when weaving classes 
when weaving 
 [Xlint:cantFindType]
[AppClassLoader@18b4aac2] error can't determine annotations of missing type org.springframework.transaction.annotation.Transactional
when weaving type org.springframework.core.MethodIntrospector
when weaving classes 
when weaving 
 [Xlint:cantFindType]
[AppClassLoader@18b4aac2] error can't determine annotations of missing type org.springframework.transaction.annotation.Transactional
when weaving type org.springframework.core.MethodIntrospector$1
when weaving classes 
when weaving 
 [Xlint:cantFindType]
[AppClassLoader@18b4aac2] error can't determine annotations of missing type org.springframework.transaction.annotation.Transactional
when weaving type org.springframework.context.event.EventListener
when weaving classes 
when weaving 
 [Xlint:cantFindType]
[AppClassLoader@18b4aac2] error can't determine annotations of missing type org.springframework.transaction.annotation.Transactional
when weaving type org.springframework.context.MessageSourceResolvable
when weaving classes 
when weaving 
 [Xlint:cantFindType]
[AppClassLoader@18b4aac2] error can't determine annotations of missing type org.springframework.transaction.annotation.Transactional
when weaving type org.springframework.util.ErrorHandler
when weaving classes 
when weaving 
 [Xlint:cantFindType]
[AppClassLoader@18b4aac2] error can't determine annotations of missing type org.springframework.transaction.annotation.Transactional
when weaving type org.springframework.context.support.DefaultLifecycleProcessor
when weaving classes 
when weaving 
 [Xlint:cantFindType]
[AppClassLoader@18b4aac2] error can't determine annotations of missing type org.springframework.transaction.annotation.Transactional
when weaving type org.springframework.context.ApplicationContextException
when weaving classes 
when weaving 
 [Xlint:cantFindType]
[AppClassLoader@18b4aac2] error can't determine annotations of missing type org.springframework.transaction.annotation.Transactional
when weaving type org.springframework.context.event.AbstractApplicationEventMulticaster$ListenerCacheKey
when weaving classes 
when weaving 
 [Xlint:cantFindType]
[AppClassLoader@18b4aac2] error can't determine annotations of missing type org.springframework.transaction.annotation.Transactional
when weaving type org.springframework.context.support.LiveBeansView
when weaving classes 
when weaving 
 [Xlint:cantFindType]
[AppClassLoader@18b4aac2] error can't determine annotations of missing type org.springframework.transaction.annotation.Transactional
when weaving type org.springframework.context.support.LiveBeansViewMBean
when weaving classes 
when weaving 
 [Xlint:cantFindType]
[AppClassLoader@18b4aac2] error can't determine annotations of missing type org.springframework.transaction.annotation.Transactional
when weaving type org.springframework.util.ConcurrentReferenceHashMap$EntrySet
when weaving classes 
when weaving 
 [Xlint:cantFindType]
[AppClassLoader@18b4aac2] error can't determine annotations of missing type org.springframework.transaction.annotation.Transactional
when weaving type org.springframework.util.ConcurrentReferenceHashMap$EntryIterator
when weaving classes 
when weaving 
 [Xlint:cantFindType]
[AppClassLoader@18b4aac2] error can't determine annotations of missing type org.springframework.transaction.annotation.Transactional
when weaving type com.ericsson.tao5.spring.nonbean_beans.ANoneSpringBean
when weaving classes 
when weaving 
 [Xlint:cantFindType]
[AppClassLoader@18b4aac2] error can't determine annotations of missing type org.springframework.transaction.annotation.Transactional
when weaving type org.springframework.beans.factory.aspectj.ConfigurableObject
when weaving classes 
when weaving 
 [Xlint:cantFindType]
[AppClassLoader@18b4aac2] error can't determine annotations of missing type org.springframework.transaction.annotation.Transactional
when weaving type com.sun.proxy.$Proxy25
when weaving classes 
when weaving 
 [Xlint:cantFindType]
[AppClassLoader@18b4aac2] error can't determine annotations of missing type org.springframework.transaction.annotation.Transactional
when weaving type org.springframework.beans.factory.wiring.BeanWiringInfo
when weaving classes 
when weaving 
 [Xlint:cantFindType]
ANoneSpringBean
ANonDefaultImpl


参考链接:

http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/

https://github.com/ahmedbhaila/non-managed-beans

http://docs.spring.io/spring-framework/docs/current/javadoc-api/overview-summary.html

 

你可能感兴趣的:(Spring,framework)