Spring \ AspectJ错误

时间:2014-01-26 21:40:51

标签: java spring aspectj

晚安!如果删除错误的方面消失,会出现类型不匹配错误,为什么会发生这种情况? Aspect也写得正确。如果这个类不用于注入,并创建为bob(Sender sender =(Sender)context.getBean(“sendService”);),一切正常,我在文档中没有看到这个

方面

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");
}
}

接口

package com.work.Interfaces;

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

实施界面

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();


}
}

注入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;
}
 }

错误

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)

1 个答案:

答案 0 :(得分:1)

您似乎正在使用JDK代理。包装类型为sendMail的bean的代理实际上只会实现其接口,我假设sendingMechanizm

尝试调用

public void setSendMail(sendMail sendMail) {

通过反射方法,Spring会将它传递给你的代理。类型sendingMechanizm(代理)的对象不是类型sendMail的参数的有效参数。

使用proxy-target-class="true"或相应的Java配置更改配置以使用CGLIB代理。请参阅here