我尝试通过Spring Aspects将日志记录添加到我的project,但这些方面从未运行过。
这是我定义方面的文件:LoggingInfo.java
package org.synyx.sybil.config;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Aspect
public class LoggingInfo {
private static final Logger LOG = LoggerFactory.getLogger(LoggingInfo.class);
@Pointcut("execution(* com.tinkerforge.IPConnection.connect(String, int)) && args(host, port)")
public void connect(String host, int port) {
}
@Before("connect(host, port)")
public void CreateIPConnection(String host, int port) {
LOG.info("Creating IPConnection to {}:{}", host, port);
}`enter code here`
@AfterReturning("connect(host, port)")
public void FinishedCreatingIPConnection(String host, int port) {
LOG.info("Successfully created IPConnection to {}:{}", host, port);
}
@AfterThrowing("connect(host, port)")
public void FailedCreatingIPConnection(String host, int port) {
LOG.info("Failed creating IPConnection to {}:{}", host, port);
}
}
这是我的Spring配置:SpringConfigDev.java
package org.synyx.sybil.config;
import com.tinkerforge.AlreadyConnectedException;
import com.tinkerforge.BrickletLEDStrip;
import com.tinkerforge.IPConnection;
import com.tinkerforge.NotConnectedException;
import com.tinkerforge.TimeoutException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;
import org.springframework.core.env.Environment;
import org.synyx.sybil.out.OutputLEDStrip;
import org.synyx.sybil.out.SingleStatusOnLEDStrip;
import org.synyx.sybil.out.SingleStatusOutput;
import java.io.IOException;
@Profile("dev")
@Configuration
@EnableAspectJAutoProxy
@PropertySource("classpath:SpringConfigDev.properties")
public class SpringConfigDev {
// private static final Logger LOG = LoggerFactory.getLogger(SpringConfigDev.class);
@Autowired
Environment env;
@Bean(destroyMethod = "disconnect")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public IPConnection ipConnection() throws AlreadyConnectedException, IOException {
String hostname = env.getRequiredProperty("ipconnection.hostname");
int port = env.getProperty("ipconnection.port", Integer.class, 4223); // 4223 is the standard port
// LOG.info("Creating IPConnection to {}:{}", hostname, port);
IPConnection ipConnection = new IPConnection();
ipConnection.connect(hostname, port);
// LOG.info("Successfully connected to {}:{}", hostname, port);
return ipConnection;
}
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public BrickletLEDStrip brickletLEDStrip(IPConnection ipConnection) throws TimeoutException, NotConnectedException {
BrickletLEDStrip brickletLEDStrip = new BrickletLEDStrip(env.getRequiredProperty("brickletledstrip.uid"),
ipConnection);
brickletLEDStrip.setFrameDuration(10);
brickletLEDStrip.setChipType(2812);
return brickletLEDStrip;
}
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public OutputLEDStrip outputLEDStrip(BrickletLEDStrip brickletLEDStrip) {
return new OutputLEDStrip(brickletLEDStrip, env.getRequiredProperty("outputledstrip.length", Integer.class));
}
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public SingleStatusOutput singleStatusOutput(OutputLEDStrip outputLEDStrip) {
return new SingleStatusOnLEDStrip(outputLEDStrip);
}
}
这里的所有东西都可以工作,显然是为了@EnableAspectJAutoProxy。
这是我用来运行它的测试:OutputLEDStripTest.java
package org.synyx.sybil.out;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.synyx.sybil.config.SpringConfigDev;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { SpringConfigDev.class })
public class OutputLEDStripTest {
@Autowired
private OutputLEDStrip outputLEDStrip;
@After
public void close() { // throws NotConnectedException {
outputLEDStrip.setBrightness(1.0);
outputLEDStrip.setColor(Color.BLACK);
outputLEDStrip.updateDisplay();
}
@Test
public void testSetColor() throws Exception {
outputLEDStrip.setColor(new Color(16, 32, 8));
outputLEDStrip.updateDisplay();
Color pixel = outputLEDStrip.getPixel(0);
assertEquals("Pixel 0.red should be 16", 16, pixel.getRed());
assertEquals("Pixel 0.green should be 32", 32, pixel.getGreen());
assertEquals("Pixel 0.blue should be 8", 8, pixel.getBlue());
}
@Test
public void testSetPixel() throws Exception {
Color color = new Color(16, 35, 77);
outputLEDStrip.setPixel(1, color);
outputLEDStrip.updateDisplay();
Color pixel0 = outputLEDStrip.getPixel(0);
Color pixel1 = outputLEDStrip.getPixel(1);
assertEquals("Pixel 0.red should be 0", 0, pixel0.getRed());
assertEquals("Pixel 0.green should be 0", 0, pixel0.getGreen());
assertEquals("Pixel 0.blue should be 0", 0, pixel0.getBlue());
assertEquals("Pixel 1.red should be 16", 16, pixel1.getRed());
assertEquals("Pixel 1.green should be 35", 35, pixel1.getGreen());
assertEquals("Pixel 1.blue should be 77", 77, pixel1.getBlue());
}
@Test
public void testSetBrightnessHalf() throws Exception {
outputLEDStrip.setColor(Color.WHITE);
outputLEDStrip.setBrightness(.5);
outputLEDStrip.updateDisplay();
Color pixel = outputLEDStrip.getPixel(0);
assertTrue("Pixel 0 should be half as bright as a full white (127, 127, 127).",
pixel.getRed() == (short) (127 * .5) && pixel.getGreen() == (short) (127 * .5)
&& pixel.getBlue() == (short) (127 * .5));
}
@Test
public void testSetBrightnessFull() throws Exception {
outputLEDStrip.setColor(Color.WHITE);
outputLEDStrip.setBrightness(1);
outputLEDStrip.updateDisplay();
Color pixel = outputLEDStrip.getPixel(0);
assertTrue("Pixel 0 should be full white (127, 127, 127).",
pixel.getRed() == 127 && pixel.getGreen() == 127 && pixel.getBlue() == 127);
}
@Test
public void testSetBrightnessDouble() throws Exception {
outputLEDStrip.setColor(Color.WHITE);
outputLEDStrip.setBrightness(2);
outputLEDStrip.updateDisplay();
Color pixel = outputLEDStrip.getPixel(0);
assertTrue("Pixel 0 should be double as bright as a full white (127, 127, 127).",
pixel.getRed() == (short) (127 * 2) && pixel.getGreen() == (short) (127 * 2)
&& pixel.getBlue() == (short) (127 * 2));
}
}
除非你安装了Tinkerforge brickd damon并且有一个Tinkerforge砖与一个连接的LED Strip bricklet并且为它设置了SpringConfigDev.properties文件,否则你将无法成功运行这个项目。 但,如果日志记录有效,您应该会在日志文件中看到错误消息。
答案 0 :(得分:1)
Spring配置提示都没关系,但我猜他们无济于事,因为切入点说:
execution(* com.tinkerforge.IPConnection.connect(String, int))
问题是:我假设Tinkerforge类不是Spring组件,即它们不能被Spring AOP execution()
切入点作为目标。因此,要么切换到full AspectJ并使用call()
切入点(在Spring AOP中不可用),要么将解决方法更改为切入点,以定位具有相同信息的Spring组件的方法之一。的; - )强>