我想使用@AutoWired将配置了@Component的非托管bean注入托管bean.我很确定我的配置是正确的,但由于某种原因,我一直得到例外:
No unique bean of type [foo.Baz] is defined: Unsatisfied dependency of type [class foo.Baz]: expected at least 1 matching bean
Run Code Online (Sandbox Code Playgroud)
根据错误,我猜它无法找到Baz类,但我不确定为什么.我的理解是上下文:XML配置中的spring配置元素应该允许我这样做.我还确保包含适当的jar文件(spring-weaving.jar和aspectjweaver.jar).
这是我设置的一个简单示例.
我的XML配置:
<beans ...>
...
<context:annotation-config/>
<context:spring-configured/>
<context:component-scan base-package="foo"/>
<bean id="bar" class="foo.Bar"/>
...
</beans>
Run Code Online (Sandbox Code Playgroud)
我有一个托管bean:
package foo;
public class Bar {
@Autowired
private Baz baz;
public void setBaz(Baz baz) {
this.baz = baz;
}
...
}
Run Code Online (Sandbox Code Playgroud)
还有一个非托管bean:
package foo;
@Component
public class Baz {
...
}
Run Code Online (Sandbox Code Playgroud)
有什么我想念的吗?
编辑:日志列出了它实例化的bean,而foo.Baz不是其中之一.我不知道为什么它没有拿起@Component注释类.
我正在使用Spring 3.x,Java 6.
我有一个带有以下连接点的@Around方面:
@Around("execution(public * my.service.*.*Connector.*(..))")
Run Code Online (Sandbox Code Playgroud)
因此,我基本上对拦截所有对类的公共方法的调用感兴趣,类名以"Connector"结尾.到现在为止还挺好.
现在,在我的方面,我想访问方法的实际参数名称:
public doStuff(String myarg, Long anotherArg)
Run Code Online (Sandbox Code Playgroud)
myarg和另一个Arg
我明白使用:
CodeSignature signature = (CodeSignature)jointPoint.getSignature();
return signature.getParameterNames();
Run Code Online (Sandbox Code Playgroud)
将实际工作,但只有我用" -g "标志(完全调试)编译代码,我宁愿不这样做.
有没有其他方法可以访问这种运行时信息.
谢谢L
我已经通过perf4J网站的以下链接,并做了同样的事情:http://perf4j.codehaus.org/devguide.html#Using_Spring_AOP_to_Integrate_Timing_Aspects
在我的spring.xml中添加了以下内容.
<aop:aspectj-autoproxy/>
<bean id="timingAspect" class="org.perf4j.log4j.aop.TimingAspect"/>
<bean id="wscClientBase" class="com.xyz.csa.core.common.WscClientBase"/>
Run Code Online (Sandbox Code Playgroud)
在类WscClientBase中,我使用@Profiled注释的以下方法.
@Profiled(tag = "SOAPCALLTEST")
public Object sendMessage(Object message) {
String msg = message.toString();
if (msg.indexOf(' ') > 1) {
msg = msg.substring(1, msg.indexOf(' '));
}
try {
Object ret = marshalSendAndReceive(message);
return ret;
} catch (RuntimeException ex) {
throw ex;
}
}
Run Code Online (Sandbox Code Playgroud)
我没有在应用程序日志中看到perf4j TimingLogger语句.但是,如果我使用它(如下没有注释),我会成功地看到日志语句.
public Object sendMessage(Object message) {
String msg = message.toString();
if (msg.indexOf(' ') > 1) {
msg = msg.substring(1, msg.indexOf(' '));
}
StopWatch stopWatch …Run Code Online (Sandbox Code Playgroud) 我有一个基于Spring 3的java应用程序构建.这个项目有另一个jar作为依赖.
这个依赖包含一个@org.aspectj.lang.annotation.Aspect类(比如说com.aspectprovider.aspects.MyAspect).有一个@Before建议从实现接口的类编织方法Foo.就像是:
@Before("execution(* com.project.Foo.save(..))")
Run Code Online (Sandbox Code Playgroud)
该Foo接口可以是"项目"内或另一罐子.这个例子没关系.
我的项目包含实现的类Foo.当然,那些是我希望它被编织的类.
我的Spring应用程序上下文配置文件(applicationContext.xml)包含以下行:
<aop:aspectj-autoproxy />
Run Code Online (Sandbox Code Playgroud)
我还将方面声明为bean,并注入一些属性:
<bean id="myAspect" class="com.aspectprovider.aspects.MyAspect"
factory-method="aspectOf" >
<property name="someproperty" value="somevalue" />
</bean>
Run Code Online (Sandbox Code Playgroud)
通过日志记录,我可以看到MyAspect实例化并注入了属性.但是方法保存没有被截获.这就是问题.
如果我将方面类从jar复制到具有Spring的应用程序,它可以工作.当这些方面包含在外部jar中时,方法save不会被截获.有线索吗?
编辑:我如何调用Foo的保存方法:
//in a JSF managed bean
@Inject
private Foo myFoo; //there's a implementation of Foo in a package that spring is looking at. So it is injected correctly.
public String someAction() {
myFoo.save("something"); //the @Before advice is only called if the …Run Code Online (Sandbox Code Playgroud) 我有一个spring应用程序 ,我按如下方式编写测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/META-INF/spring/applicationContext.xml" })
@TransactionConfiguration(defaultRollback = true)
@Transactional
public class MyTest {
}
Run Code Online (Sandbox Code Playgroud)
当我尝试创建另一个测试类并尝试运行该应用程序时,我在新的测试类上得到以下异常:
ERROR [main] (TestContextManager.java:324) - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecut
r@22e85825] to prepare test instance [Testing.MyTest2@43f2f70a]
org.springframework.aop.framework.AopConfigException: Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.
at org.springframework.aop.framework.DefaultAopProxyFactory.createAopProxy(DefaultAopProxyFactory.java:67)
at org.springframework.aop.framework.ProxyCreatorSupport.createAopProxy(ProxyCreatorSupport.java:104)
at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:112)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:476)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:362)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:322)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1426)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:386)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:111)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
at …Run Code Online (Sandbox Code Playgroud) 每次我阅读官方的Spring AOP文档(链接)时,我都会对使用RuntimeExceptions的建议行为感到困惑.有人会检查我的未成年人是否适合以下建议?
@之前
@AfterReturning接口
@AfterThrowing
@后
@周围
我需要帮助解决Spring和代理问题.
org.springframework.beans.factory.BeanNotOfRequiredTypeException:名为'fooAPIService'的bean必须是[com.foo.clientapi.service.FooAPIService]类型,但实际上是[com.sun.proxy.$ Proxy110]类型
org.springframework.beans.factory.BeanCreationException:创建名为'activityController'的bean时出错:资源依赖注入失败; 嵌套异常是org.springframework.beans.factory.BeanNotOfRequiredTypeException:名为'fooAPIService'的bean必须是[com.foo.clientapi.service.FooAPIService]类型,但实际上是[com.sun.proxy.$ Proxy110]类型
Webapp项目 - >
春天上下文
<context:annotation-config/>
<context:component-scan base-package="com.foo.controller"/>
<aop:aspectj-autoproxy />
<aop:config proxy-target-class="true"/>
<mvc:annotation-driven/>
Run Code Online (Sandbox Code Playgroud)
ActivityController.class
import com.foo.clientapi.service.FooAPIService;
...
@Controller
@RequestMapping(value = "/toto")
public class ActivityController {
@Resource
private FooAPIService fooAPIService;
...
}
Run Code Online (Sandbox Code Playgroud)
另一个项目(微服务) - >
FooAPIService.class
@Path("/foos")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public interface FooAPIService {
...
}
Run Code Online (Sandbox Code Playgroud)
Jaxrs配置:
<jaxrs:client id="fooAPIService"
address="${toto}"
threadSafe="true"
serviceClass="com.foo.clientapi.service.FooAPIService"
inheritHeaders="true">
...
</jaxrs:client>
Run Code Online (Sandbox Code Playgroud)
版本:aspectjweaver:1.6.10 aspectjrt:1.6.11 cglib:2.2 Spring 3.2.2
所以我对使用以下表达式声明的方法有一个方面:
@Before("execution(* aaa.bbb.ccc.*.*(..))")
Run Code Online (Sandbox Code Playgroud)
这适用于包中的所有类aaa.bbb.ccc.但是,现在,我想要捕获所有类aaa.bbb,包括那些类aaa.bbb.ccc.所以我试着把它备份到这里:
@Before("execution(* aaa.bbb.*.*(..))")
Run Code Online (Sandbox Code Playgroud)
aaa.bbb但是,这只会从中获取类,并忽略来自的类aaa.bbb.ccc.有没有办法让表达式递归搜索所有子包?
我正在尝试测试@AsyncSpring的注释是否在我的项目中按预期工作.但事实并非如此.
我有这个测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = GlobalConfiguration.class)
public class ActivityMessageListenerTest {
@Autowired
private ActivityMessageListener activityMessageListener;
private Long USER_ID = 1l;
private Long COMPANY_ID = 2l;
private Date DATE = new Date(10000000);
private String CLASSNAME = "className";
private Long CLASSPK = 14l;
private Integer TYPE = 22;
private String EXTRA_DATA = "extra";
private Long RECIVED_USER_ID = 99l;
@Before
public void setup() throws Exception {
}
@Test
public void testDoReceiveWithException() throws Exception {
System.out.println("Current thread " + Thread.currentThread().getName());
Map<String, Object> values …Run Code Online (Sandbox Code Playgroud) 假设我有一个如下所示的程序:
@Component
public class MainAction {
public void doTheAction() {
System.out.println("Now doing the action");
}
}
@Aspect
@Component
public class BeforeAspect {
@Autowired
private Logger logger;
@Before("execution(* thepackagename.MainAction.*(..))")
public void doBefore() {
logger.log("The @Before advice has run");
}
}
@Component
public class Logger {
public void log(String s) {
System.out.println(s);
}
}
Run Code Online (Sandbox Code Playgroud)
如果我通过的Eclipse(main方法调用esentially运行它,这是工作的罚款mainAction.doTheAction()后,mainAction被Spring创建).
现在我想编写一个测试,确保在log调用时正确doTheAction调用该方法.我们正在使用JMockit进行测试.(这是我实际遇到的问题的一个非常简化的情况;通过AOP方面调用更复杂的记录器,并且正在记录错误的某些值.在进行修复之前,我正在尝试编写一个测试以确保记录的值是正确的.)
这就是我的(简化)测试目前的样子:
@RunWith(JMockit.class)
@ContextConfiguration(locations = {"classpath:Beans.xml"})
public class MainActionTest {
@Tested
private MainAction mainAction;
@Test
public void testThatLoggerIsCalled(@Injectable Logger …Run Code Online (Sandbox Code Playgroud)