简体   繁体   English

Spring \\ AspectJ错误

[英]Spring\AspectJ error

Good night!There is a type mismatch error if I remove the aspect of the error disappears , why is this happening? 晚上好!如果我删除了错误消失的方面,就会出现类型不匹配的错误,为什么会这样呢? Aspect also written correctly. 方面也写得正确。 if this class is not used for injection, and create as a bob(Sender sender = (Sender) context.getBean("sendService");), everything works fine, I have not seen this in the documentation 如果此类不用于注入,而是创建为bob(Sender sender =(Sender)context.getBean(“ sendService”);),则一切正常,我在文档中没有看到

Aspect 方面

package com.work.Spring.Aspects;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class AspectLogger 
{
//private Logger Log = Logger.getLogger("stdout");

@Pointcut("execution(* com.work.Interfaces.sendingMechanizm.send(String, String))" +
        " && args(messageText, destination)")
public void send(String messageText, String destination) {}

@Before("send(messageText, destination)")
public void test(String messageText, String destination)
{
    System.out.println("Test");
}
}

interface 接口

package com.work.Interfaces;

public interface sendingMechanizm 
{
public void send(String messageText, String destination);
}

implementation interface 实现接口

package com.work.Spring.mainClass.sendingMechanizms;

import java.util.Date;

import org.springframework.stereotype.Component;

import com.skype.ContactList;
import com.skype.Friend;
import com.skype.Skype;
import com.skype.SkypeException;
import com.work.Interfaces.sendingMechanizm;
import com.work.Spring.Exception.SkypeNotRunning;
import com.work.Spring.Exception.notFoundUserException;

@Component("sendSkype")
public class sendSkype implements sendingMechanizm 
{
private ContactList contactList = null;

private Date lastUpdate = null;

public void send(String messageText, String destination) 
{
    try 
    {
        if (Skype.isRunning())
        {
            initContactList();
            String userId = searchFriend(destination);
            if(userId.equals(""))
                throw new notFoundUserException();
            else
                sendMessage(messageText, userId);
        }
        else
            throw new SkypeNotRunning();
    } 
    catch (SkypeException | notFoundUserException | SkypeNotRunning e) 
    {
        e.printStackTrace();
    }
}

private String searchFriend(String destination) throws SkypeException
{
    String userId = "";
    for (Friend friend : contactList.getAllFriends())
    {
        if (friend.getFullName().equals(destination))
        {
            userId = friend.getId();
            break;
        }
    }
    return userId;
}

private void sendMessage(String message, String userId) throws SkypeException
{
    Friend friend = contactList.getFriend(userId);
    friend.send(message);
}

private void initContactList() throws SkypeException
{
    if (contactList == null)
    {   
        contactList = Skype.getContactList();
        lastUpdate = new Date(System.currentTimeMillis());
    }
    else
    {
        checkDateUpdate();
    }
}

@SuppressWarnings("deprecation")
private void checkDateUpdate() throws SkypeException
{
    Date currentDate = new Date();
    if (currentDate.getDay() > lastUpdate.getDay() || 
            currentDate.getMonth() > lastUpdate.getMonth() ||
            currentDate.getYear() > lastUpdate.getYear())
        contactList = Skype.getContactList();


}
}

Inject bean class 注入bean类

package com.work.Spring.mainClass;

 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;

 import com.work.Interfaces.Sender;
 import com.work.Model.Reminder;
 import com.work.Spring.mainClass.sendingMechanizms.sendMail;
 import com.work.Spring.mainClass.sendingMechanizms.sendSkype;
 import com.work.Spring.mainClass.sendingMechanizms.sendVk;

 @Service("sendService")
 public class sendService implements Sender<Reminder> 
 {
private ReminderService reminderService;

private sendMail sendMail;

private sendVk sendVk;

private sendSkype sendSkype;

@Transactional(readOnly = true)
public void startSending() 
{
    for (Reminder reminder : reminderService.getAll())
    {
        String message = reminder.getMessage();
        String destination = reminder.getData();
        switch (reminder.getType()) 
        {
            case "Mail":
            {
                sendMail.send(message, destination);
                break;
            }

            case "Vk":
            {
                sendVk.send(message, destination);
                break;
            }

            case "Skype":
            {
                sendSkype.send(message, destination);
                break;
            }
        }
    }
}

public ReminderService getReminderService() {
    return reminderService;
}

@Autowired
public void setReminderService(ReminderService reminderService) {
    this.reminderService = reminderService;
}

public sendMail getSendMail() {
    return sendMail;
}

@Autowired
public void setSendMail(sendMail sendMail) {
    this.sendMail = sendMail;
}

public sendVk getSendVk() {
    return sendVk;
}

@Autowired
public void setSendVk(sendVk sendVk) {
    this.sendVk = sendVk;
}

public sendSkype getSendSkype() {
    return sendSkype;
}

@Autowired
public void setSendSkype(sendSkype sendSkype) {
    this.sendSkype = sendSkype;
}
 }

error 错误

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sendService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.work.Spring.mainClass.sendService.setSendMail(com.work.Spring.mainClass.sendingMechanizms.sendMail); nested exception is java.lang.IllegalArgumentException: argument type mismatch
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:609)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:469)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.work.Spring.App.main(App.java:22)

It seems like you are using JDK proxies. 似乎您正在使用JDK代理。 The proxy wrapping your bean of type sendMail will actually only be implementing its interfaces, I'm assuming sendingMechanizm . 包装您sendMail类型的bean的代理实际上只会实现其接口,我假设是sendingMechanizm

When trying to invoke your 尝试调用您的

public void setSendMail(sendMail sendMail) {

method through reflection, Spring will pass it your proxy. 通过反射的方法,Spring会将其传递给您的代理。 An object of type sendingMechanizm (the proxy) is not a valid argument for a parameter of type sendMail . sendingMechanizm类型的对象(代理)不是sendMail类型的参数的有效参数。

Change your configuration to use CGLIB proxies using proxy-target-class="true" or the corresponding Java configuration. 使用proxy-target-class="true"或相应的Java配置,更改您的配置以使用CGLIB代理。 See here . 这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM