Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Maven SpringAOP:在对bean应用ProxyFactoryBean之后,我们可以通过getBean(“beanName”)方法访问bean对象吗?_Maven_Spring Aop - Fatal编程技术网

Maven SpringAOP:在对bean应用ProxyFactoryBean之后,我们可以通过getBean(“beanName”)方法访问bean对象吗?

Maven SpringAOP:在对bean应用ProxyFactoryBean之后,我们可以通过getBean(“beanName”)方法访问bean对象吗?,maven,spring-aop,Maven,Spring Aop,我已经通过“ProxyFactoryBean”在“MyXMLApplication”类型的Bean上应用了“BeforeAdvice”,现在我无法直接访问Bean对象{by getBean(MyXMLApplication.class)},它给出了错误:- 通过setter依赖注入myxml 线程“main”org.springframework.beans.factory.nouniquebeandDefinitionException中的异常:未定义类型为[com.journaldev.sp

我已经通过“ProxyFactoryBean”在“MyXMLApplication”类型的Bean上应用了“BeforeAdvice”,现在我无法直接访问Bean对象{by getBean(MyXMLApplication.class)},它给出了错误:-

通过setter依赖注入myxml 线程“main”org.springframework.beans.factory.nouniquebeandDefinitionException中的异常:未定义类型为[com.journaldev.spring.di.consumer.MyXMLApplication]的合格bean:应为单个匹配bean,但找到了2:MyXMLApp,代理

但是,我能够通过“代理”bean对象{(MyXMLApplication)context.getBean(“代理”)}获取bean对象。现在我的问题是,在对任何bean应用代理之后,是否有任何方法可以不使用代理bean直接访问它

我的代码是:

********Before Advisor*********




 public class MyBeforeAdvisor implements MethodBeforeAdvice{
        public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
            // TODO Auto-generated method stub
            System.out.println("before advice run");
        }
    }

*********applicationContext.xml********


<bean id="twitter" class="com.journaldev.spring.di.services.TwitterService"></bean>
<bean id="MyXMLApp" class="com.journaldev.spring.di.consumer.MyXMLApplication" autowire="byType"/>
<bean id="beforeAdvice" class="com.journaldev.spring.aop.advisor.MyBeforeAdvisor"></bean>
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="MyXMLApp"></property>
        <property name="interceptorNames">
          <list>
             <value>beforeAdvice</value>
          </list>
        </property>
</bean>


**********Main Bean Class************


  public class MyXMLApplication {
        private MessageService service;
        public void setService(MessageService svc){
            System.out.println("by setter dependency injection myxml");
            this.service=svc;
        }
        public boolean processMessage(String msg, String rec) {
            return this.service.sendMessage(msg, rec);
        }
    }



**********Autowired Bean Interface ******* 


    public interface MessageService {
            boolean sendMessage(String msg, String rec);
        }

**********Autowired Bean Impl*********


 public class TwitterService implements MessageService {
        public boolean sendMessage(String msg, String rec) {
            System.out.println("Twitter message Sent to "+rec+ " with Message="+msg);
            return true;
        }
    }



 ************* Main function**********


      public static void main(String[] args) {
                ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                        "applicationContext.xml");
     MyXMLApplication app = context.getBean(MyXMLApplication.class); --> Not Working

    //MyXMLApplication app = (MyXMLApplication)context.getBean("proxy"); -->working

                app.processMessage("Hi", "abc@abc.com");
                context.close();
            }
顾问前的
****************
公共类MyBeforeAdvisor实现MethodBeforeAdvice{
在(方法arg0,对象[]arg1,对象arg2)抛出Throwable之前的public void{
//TODO自动生成的方法存根
System.out.println(“建议运行前”);
}
}
*********applicationContext.xml********
事先建议
**********主Bean类************
公共类MyXMLApplication{
私人信息服务;
公共无效设置服务(MessageService svc){
println(“通过setter依赖项注入myxml”);
this.service=svc;
}
公共布尔processMessage(字符串消息、字符串记录){
返回此.service.sendMessage(msg,rec);
}
}
**********自动连线Bean接口*******
公共接口消息服务{
布尔sendMessage(字符串消息、字符串记录);
}
**********自动连线Bean Impl*********
公共类TwitterService实现MessageService{
公共布尔发送消息(字符串消息,字符串记录){
System.out.println(“Twitter消息发送到“+rec+”并带有message=“+msg”);
返回true;
}
}
*************主要功能**********
公共静态void main(字符串[]args){
ClassPathXmlApplicationContext上下文=新的ClassPathXmlApplicationContext(
“applicationContext.xml”);
MyXMLApplication app=context.getBean(MyXMLApplication.class);-->不工作
//MyXMLApplication app=(MyXMLApplication)context.getBean(“代理”);-->正在工作
app.processMessage(“嗨”abc@abc.com");
context.close();
}

抱歉,伙计们,错误来了,因为我调用了getBean(MyXMLApplication.class)[按类型]这就是为什么它发现了两个类型为MyXMLApplication.class的bean“proxy”和“MyXMLApp”,并给出了错误


这并不是说在对bean应用任何代理之后,我们不能直接调用它的“getBean”方法。

这是最流行的spring aop问题。Spring事务基础架构构建在Spring aop之上。SpringAOP允许使用两种代理机制——基于jdk或cglib。Jdk代理在您的案例中使用,因此,代理不能转换为非接口类型。只需将“proxy target class=“true”添加到元素中。